Skip to content

Commit e943f9d

Browse files
authored
Merge pull request #19 from mintlify/sam/remove-replace-line-highlight-patterns
remove line-highlight pattern replacement
2 parents 86958ae + a9dfe42 commit e943f9d

File tree

2 files changed

+1
-69
lines changed

2 files changed

+1
-69
lines changed

packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ import {
1818
DEFAULT_THEMES,
1919
DEFAULT_LANGS,
2020
} from './shiki-constants.js';
21-
import {
22-
getLanguage,
23-
getLinesToHighlight,
24-
lineHighlightPattern,
25-
LINE_HIGHLIGHT_CLASS,
26-
} from './utils.js';
21+
import { getLanguage } from './utils.js';
2722

2823
export type RehypeSyntaxHighlightingOptions = {
2924
theme?: ShikiTheme;
@@ -118,8 +113,6 @@ const traverseNode = (
118113
) => {
119114
try {
120115
const code = toString(node);
121-
const lines = code.split('\n');
122-
let linesToHighlight = getLinesToHighlight(node, lines.length);
123116

124117
const hast = highlighter.codeToHast(code, {
125118
lang: lang ?? DEFAULT_LANG,
@@ -139,36 +132,11 @@ const traverseNode = (
139132
if (!codeElement) return;
140133

141134
let lineNumber = 0;
142-
visit(codeElement, 'element', (span, spanIndex, spanParent) => {
143-
if (
144-
!spanParent ||
145-
spanParent.type !== 'element' ||
146-
spanParent.tagName !== 'code' ||
147-
span.tagName !== 'span' ||
148-
(!span.children.length && spanIndex === spanParent.children.length - 1) ||
149-
(typeof span.properties.class !== 'string' && !Array.isArray(span.properties.class)) ||
150-
!span.properties.class.includes('line')
151-
) {
152-
return;
153-
}
154-
155-
lineNumber++;
156-
if (linesToHighlight.includes(lineNumber)) {
157-
if (typeof span.properties.class === 'string') {
158-
span.properties.class += ' ' + LINE_HIGHLIGHT_CLASS;
159-
} else {
160-
span.properties.class = [...span.properties.class, LINE_HIGHLIGHT_CLASS];
161-
}
162-
}
163-
});
164135

165136
const preChild = codeElement.children[0] as Element;
166137
const numberOfLines = lineNumber;
167138

168139
node.data = node.data ?? {};
169-
if (node.data.meta) {
170-
node.data.meta = node.data.meta.replace(lineHighlightPattern, '').trim();
171-
}
172140
codeElement.data = node.data;
173141
codeElement.properties.numberOfLines = numberOfLines;
174142
if (preChild) {

packages/mdx/src/plugins/rehype/utils.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import type { Element } from 'hast';
22

33
import { type ShikiLang } from './shiki-constants.js';
44

5-
export const lineHighlightPattern = /\{(.*?)\}/;
6-
export const LINE_HIGHLIGHT_CLASS = 'line-highlight';
7-
85
export function classNameOrEmptyArray(element: Element): string[] {
96
const className = element.properties.className;
107
if (Array.isArray(className) && className.every((el) => typeof el === 'string')) return className;
@@ -26,36 +23,3 @@ export function getLanguage(
2623

2724
return undefined;
2825
}
29-
30-
export function getLinesToHighlight(node: Element, maxLines: number): number[] {
31-
const meta =
32-
typeof node.data?.meta === 'string'
33-
? node.data.meta
34-
: classNameOrEmptyArray(node).reduce((acc, item) => acc + ' ' + item, '');
35-
if (!meta) return [];
36-
37-
const content = meta.match(lineHighlightPattern)?.[1]?.trim();
38-
if (!content) return [];
39-
40-
const lineNumbers = new Set<number>();
41-
42-
content.split(',').forEach((part) => {
43-
const [start, end] = part.split('-').map((num) => {
44-
const trimmed = num.trim();
45-
if (!/^\d+$/.test(trimmed)) return undefined;
46-
const parsed = parseInt(trimmed, 10);
47-
return parsed > maxLines ? maxLines : parsed;
48-
});
49-
50-
if (!start) return;
51-
const endLine = end ?? start;
52-
53-
if (endLine < start) return;
54-
const max = Math.min(endLine, maxLines);
55-
for (let i = start; i <= max; i++) {
56-
lineNumbers.add(i);
57-
}
58-
});
59-
60-
return Array.from(lineNumbers).sort((a, b) => a - b);
61-
}

0 commit comments

Comments
 (0)