Skip to content

Commit 52b055d

Browse files
committed
feat: allow customize tabindex, close #778
1 parent 1be078f commit 52b055d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

packages/core/src/highlight/code-to-hast.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function tokensToHast(
9090

9191
const {
9292
structure = 'classic',
93+
tabindex = '0',
9394
} = options
9495

9596
let preNode: Element = {
@@ -98,7 +99,11 @@ export function tokensToHast(
9899
properties: {
99100
class: `shiki ${options.themeName || ''}`,
100101
style: options.rootStyle || `background-color:${options.bg};color:${options.fg}`,
101-
tabindex: '0',
102+
...(tabindex !== false && tabindex != null)
103+
? {
104+
tabindex: tabindex.toString(),
105+
}
106+
: {},
102107
...Object.fromEntries(
103108
Array.from(
104109
Object.entries(options.meta || {}),

packages/shiki/test/hast.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ it('hasfocus support', async () => {
4848
const code = await codeToHtml(snippet, {
4949
lang: 'php',
5050
theme: 'vitesse-light',
51+
tabindex: false,
5152
transformers: [
5253
{
5354
code(node) {
@@ -67,7 +68,7 @@ it('hasfocus support', async () => {
6768

6869
expect(code)
6970
.toMatchInlineSnapshot(`
70-
"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code class="language-php"><span class="line"><span style="color:#999999">$</span><span style="color:#B07D48">foo</span><span style="color:#999999"> =</span><span style="color:#B5695977"> "</span><span style="color:#B56959">bar</span><span style="color:#B5695977">"</span><span style="color:#999999">;</span></span>
71+
"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34"><code class="language-php"><span class="line"><span style="color:#999999">$</span><span style="color:#B07D48">foo</span><span style="color:#999999"> =</span><span style="color:#B5695977"> "</span><span style="color:#B56959">bar</span><span style="color:#B5695977">"</span><span style="color:#999999">;</span></span>
7172
<span class="line" data-has-focus="true"><span style="color:#999999">$</span><span style="color:#B07D48">test</span><span style="color:#999999"> =</span><span style="color:#B5695977"> "</span><span style="color:#B56959">owo</span><span style="color:#B5695977">"</span><span style="color:#999999">;</span><span style="color:#A0ADA0"></span></span>
7273
<span class="line"><span style="color:#999999">$</span><span style="color:#B07D48">bar</span><span style="color:#999999"> =</span><span style="color:#B5695977"> "</span><span style="color:#B56959">baz</span><span style="color:#B5695977">"</span><span style="color:#999999">;</span></span></code></pre>"
7374
`)

packages/types/src/options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ export interface CodeToHastOptionsCommon<Languages extends string = string>
153153
* @default 'classic'
154154
*/
155155
structure?: 'classic' | 'inline'
156+
157+
/**
158+
* Tab index of the root `<pre>` element.
159+
*
160+
* Set to `false` to disable tab index.
161+
*
162+
* @default 0
163+
*/
164+
tabindex?: number | string | false
156165
}
157166

158167
export interface CodeOptionsMeta {

0 commit comments

Comments
 (0)