From bffe1e14125220d465a94cc629260e19bff48e0c Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 24 Jul 2026 00:16:44 +0530 Subject: [PATCH] feat(markdown): allow disabling table `tabindex` attribute Moves the inline table_open rule into its own plugin file and adds a `markdown.tableTabIndex` option (default true) to disable it. Co-Authored-By: Claude Fable 5 --- src/node/markdown/markdown.ts | 15 ++++++++------- src/node/markdown/plugins/table.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/node/markdown/plugins/table.ts diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index 189fe424..fb6638dc 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -38,6 +38,7 @@ import { linkPlugin } from './plugins/link' import { preWrapperPlugin } from './plugins/preWrapper' import { restoreEntities } from './plugins/restoreEntities' import { snippetPlugin } from './plugins/snippet' +import { tablePlugin } from './plugins/table' export type { Header } from '../shared' @@ -216,6 +217,11 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions { * @see https://vitepress.dev/guide/markdown#github-flavored-alerts */ gfmAlerts?: boolean + /** + * Add `tabindex="0"` to tables so keyboard users can focus and scroll them. + * @default true + */ + tableTabIndex?: boolean /** * Allows disabling the CJK-friendly plugin. * This plugin adds support for emphasis marks (**bold**) in Japanese, Chinese, and Korean text. @@ -291,13 +297,8 @@ export async function createMarkdownRenderer( ) lineNumberPlugin(md, options.lineNumbers) - const tableOpen = md.renderer.rules.table_open - md.renderer.rules.table_open = function (tokens, idx, options, env, self) { - const token = tokens[idx] - if (token.attrIndex('tabindex') < 0) token.attrPush(['tabindex', '0']) - return tableOpen - ? tableOpen(tokens, idx, options, env, self) - : self.renderToken(tokens, idx, options) + if (options.tableTabIndex !== false) { + tablePlugin(md) } if (options.gfmAlerts !== false) { diff --git a/src/node/markdown/plugins/table.ts b/src/node/markdown/plugins/table.ts new file mode 100644 index 00000000..edc06339 --- /dev/null +++ b/src/node/markdown/plugins/table.ts @@ -0,0 +1,14 @@ +import type { MarkdownItAsync } from 'markdown-it-async' + +// adds tabindex="0" to tables so they are focusable and can be +// scrolled with the keyboard when they overflow horizontally +export const tablePlugin = (md: MarkdownItAsync) => { + const tableOpen = md.renderer.rules.table_open + md.renderer.rules.table_open = function (tokens, idx, options, env, self) { + const token = tokens[idx] + if (token.attrIndex('tabindex') < 0) token.attrPush(['tabindex', '0']) + return tableOpen + ? tableOpen(tokens, idx, options, env, self) + : self.renderToken(tokens, idx, options) + } +}