Skip to content

Commit 85da49b

Browse files
Fix expandables
1 parent d7e2cbb commit 85da49b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
116116
if (!codeElement) return;
117117

118118
let lineNumber = 0;
119-
visit(codeElement, 'element', (span, _, spanParent) => {
119+
visit(codeElement, 'element', (span, spanIndex, spanParent) => {
120120
if (
121121
!spanParent ||
122122
spanParent.type !== 'element' ||
123123
spanParent.tagName !== 'code' ||
124124
span.tagName !== 'span' ||
125+
(!span.children.length && spanIndex === spanParent.children.length - 1) ||
125126
(typeof span.properties.class !== 'string' && !Array.isArray(span.properties.class)) ||
126127
!span.properties.class.includes('line')
127128
) {
@@ -138,11 +139,19 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
138139
}
139140
});
140141

141-
if (node.data?.meta) {
142+
const preChild = codeElement.children[0] as Element;
143+
const numberOfLines = lineNumber;
144+
145+
node.data = node.data ?? {};
146+
if (node.data.meta) {
142147
node.data.meta = node.data.meta.replace(lineHighlightPattern, '').trim();
143148
}
144149
codeElement.data = node.data;
145-
if (codeElement.children[0]) codeElement.children[0].data = node.data;
150+
codeElement.properties.numberOfLines = numberOfLines;
151+
if (preChild) {
152+
preChild.data = node.data;
153+
preChild.properties.numberOfLines = numberOfLines;
154+
}
146155
parent.children.splice(index, 1, codeElement);
147156
} catch (err) {
148157
if (options.ignoreMissing && /Unknown language/.test((err as Error).message)) {

0 commit comments

Comments
 (0)