From a897b2a8c932de7f601e7976efaa703e3a7ec8bd Mon Sep 17 00:00:00 2001 From: dks333 Date: Tue, 10 Jun 2025 16:25:37 -0700 Subject: [PATCH 1/2] remove line-highlight pattern replacement --- packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts b/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts index 8745b8d..f2cdc51 100644 --- a/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts +++ b/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts @@ -166,9 +166,6 @@ const traverseNode = ( const numberOfLines = lineNumber; node.data = node.data ?? {}; - if (node.data.meta) { - node.data.meta = node.data.meta.replace(lineHighlightPattern, '').trim(); - } codeElement.data = node.data; codeElement.properties.numberOfLines = numberOfLines; if (preChild) { From a9dfe426540711854ad63144509ba59c812aee6a Mon Sep 17 00:00:00 2001 From: dks333 Date: Tue, 10 Jun 2025 18:10:38 -0700 Subject: [PATCH 2/2] remove line highlight --- .../rehype/rehypeSyntaxHighlighting.ts | 31 +--------------- packages/mdx/src/plugins/rehype/utils.ts | 36 ------------------- 2 files changed, 1 insertion(+), 66 deletions(-) diff --git a/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts b/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts index f2cdc51..e618bc1 100644 --- a/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts +++ b/packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts @@ -18,12 +18,7 @@ import { DEFAULT_THEMES, DEFAULT_LANGS, } from './shiki-constants.js'; -import { - getLanguage, - getLinesToHighlight, - lineHighlightPattern, - LINE_HIGHLIGHT_CLASS, -} from './utils.js'; +import { getLanguage } from './utils.js'; export type RehypeSyntaxHighlightingOptions = { theme?: ShikiTheme; @@ -118,8 +113,6 @@ const traverseNode = ( ) => { try { const code = toString(node); - const lines = code.split('\n'); - let linesToHighlight = getLinesToHighlight(node, lines.length); const hast = highlighter.codeToHast(code, { lang: lang ?? DEFAULT_LANG, @@ -139,28 +132,6 @@ const traverseNode = ( if (!codeElement) return; let lineNumber = 0; - visit(codeElement, 'element', (span, spanIndex, spanParent) => { - if ( - !spanParent || - spanParent.type !== 'element' || - spanParent.tagName !== 'code' || - span.tagName !== 'span' || - (!span.children.length && spanIndex === spanParent.children.length - 1) || - (typeof span.properties.class !== 'string' && !Array.isArray(span.properties.class)) || - !span.properties.class.includes('line') - ) { - return; - } - - lineNumber++; - if (linesToHighlight.includes(lineNumber)) { - if (typeof span.properties.class === 'string') { - span.properties.class += ' ' + LINE_HIGHLIGHT_CLASS; - } else { - span.properties.class = [...span.properties.class, LINE_HIGHLIGHT_CLASS]; - } - } - }); const preChild = codeElement.children[0] as Element; const numberOfLines = lineNumber; diff --git a/packages/mdx/src/plugins/rehype/utils.ts b/packages/mdx/src/plugins/rehype/utils.ts index 46f67d1..c04d62f 100644 --- a/packages/mdx/src/plugins/rehype/utils.ts +++ b/packages/mdx/src/plugins/rehype/utils.ts @@ -2,9 +2,6 @@ import type { Element } from 'hast'; import { type ShikiLang } from './shiki-constants.js'; -export const lineHighlightPattern = /\{(.*?)\}/; -export const LINE_HIGHLIGHT_CLASS = 'line-highlight'; - export function classNameOrEmptyArray(element: Element): string[] { const className = element.properties.className; if (Array.isArray(className) && className.every((el) => typeof el === 'string')) return className; @@ -26,36 +23,3 @@ export function getLanguage( return undefined; } - -export function getLinesToHighlight(node: Element, maxLines: number): number[] { - const meta = - typeof node.data?.meta === 'string' - ? node.data.meta - : classNameOrEmptyArray(node).reduce((acc, item) => acc + ' ' + item, ''); - if (!meta) return []; - - const content = meta.match(lineHighlightPattern)?.[1]?.trim(); - if (!content) return []; - - const lineNumbers = new Set(); - - content.split(',').forEach((part) => { - const [start, end] = part.split('-').map((num) => { - const trimmed = num.trim(); - if (!/^\d+$/.test(trimmed)) return undefined; - const parsed = parseInt(trimmed, 10); - return parsed > maxLines ? maxLines : parsed; - }); - - if (!start) return; - const endLine = end ?? start; - - if (endLine < start) return; - const max = Math.min(endLine, maxLines); - for (let i = start; i <= max; i++) { - lineNumbers.add(i); - } - }); - - return Array.from(lineNumbers).sort((a, b) => a - b); -}