From 52b055d7698458e76751d03abfaf72dd1477e0b2 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 16 Sep 2024 09:31:49 +0200 Subject: [PATCH] feat: allow customize `tabindex`, close #778 --- packages/core/src/highlight/code-to-hast.ts | 7 ++++++- packages/shiki/test/hast.test.ts | 3 ++- packages/types/src/options.ts | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/core/src/highlight/code-to-hast.ts b/packages/core/src/highlight/code-to-hast.ts index 0eeb256c..69e0db7e 100644 --- a/packages/core/src/highlight/code-to-hast.ts +++ b/packages/core/src/highlight/code-to-hast.ts @@ -90,6 +90,7 @@ export function tokensToHast( const { structure = 'classic', + tabindex = '0', } = options let preNode: Element = { @@ -98,7 +99,11 @@ export function tokensToHast( properties: { class: `shiki ${options.themeName || ''}`, style: options.rootStyle || `background-color:${options.bg};color:${options.fg}`, - tabindex: '0', + ...(tabindex !== false && tabindex != null) + ? { + tabindex: tabindex.toString(), + } + : {}, ...Object.fromEntries( Array.from( Object.entries(options.meta || {}), diff --git a/packages/shiki/test/hast.test.ts b/packages/shiki/test/hast.test.ts index fcf7465c..ebe16052 100644 --- a/packages/shiki/test/hast.test.ts +++ b/packages/shiki/test/hast.test.ts @@ -48,6 +48,7 @@ it('hasfocus support', async () => { const code = await codeToHtml(snippet, { lang: 'php', theme: 'vitesse-light', + tabindex: false, transformers: [ { code(node) { @@ -67,7 +68,7 @@ it('hasfocus support', async () => { expect(code) .toMatchInlineSnapshot(` - "
$foo = "bar";
+      "
$foo = "bar";
       $test = "owo";
       $bar = "baz";
" `) diff --git a/packages/types/src/options.ts b/packages/types/src/options.ts index be8f194e..99c7173b 100644 --- a/packages/types/src/options.ts +++ b/packages/types/src/options.ts @@ -153,6 +153,15 @@ export interface CodeToHastOptionsCommon * @default 'classic' */ structure?: 'classic' | 'inline' + + /** + * Tab index of the root `
` element.
+   *
+   * Set to `false` to disable tab index.
+   *
+   * @default 0
+   */
+  tabindex?: number | string | false
 }
 
 export interface CodeOptionsMeta {