Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -139,36 +132,11 @@ 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;

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) {
Expand Down
36 changes: 0 additions & 36 deletions packages/mdx/src/plugins/rehype/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<number>();

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);
}