Skip to content

Commit 4ed27d9

Browse files
committed
chore: improve showLineNumber
1 parent 1334e48 commit 4ed27d9

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

packages/core/src/index.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ function apply(
6868

6969
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
7070
element.children = [tree].flatMap((tree) => {
71-
const pre = tree.children[0];
71+
const [pre] = tree.children;
7272
const themeNames = getThemeNames(theme);
7373
const themeNamesString = themeNames.join(' ');
7474

7575
if (!(isElement(pre) && pre.properties)) {
7676
return [];
7777
}
7878

79-
const code = pre.children[0];
79+
const [code] = pre.children;
8080

8181
// Remove extraneous classes
8282
if (
@@ -352,11 +352,12 @@ export function rehypePrettyCode(
352352
if (!isElement(codeElement)) return;
353353
const [textElement] = codeElement.children;
354354

355-
const { title, caption, meta, lang } = parseBlockMetaString(
356-
codeElement,
357-
filterMetaString,
358-
defaultCodeBlockLang,
359-
);
355+
const { title, caption, meta, lang, showLineNumbers } =
356+
parseBlockMetaString(
357+
codeElement,
358+
filterMetaString,
359+
defaultCodeBlockLang,
360+
);
360361

361362
if (!lang || lang === 'math') return;
362363

@@ -433,25 +434,23 @@ export function rehypePrettyCode(
433434

434435
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
435436
visit(codeTree, 'element', (element) => {
436-
if (element.tagName === 'code') {
437-
const showLineNumbers = /(?:^|\s)showLineNumbers(?:\s|$)/.test(
438-
meta,
437+
if (
438+
(element.tagName === 'code' || element.tagName === 'pre') &&
439+
showLineNumbers
440+
) {
441+
if (element.properties) {
442+
element.properties['data-line-numbers'] = '';
443+
}
444+
445+
const lineNumbersStartAtMatch = meta.match(
446+
/showLineNumbers=(\d+)/i,
439447
);
440-
if (showLineNumbers) {
448+
const startNumberString = lineNumbersStartAtMatch?.[1];
449+
if (startNumberString) {
450+
const startAt = Number(startNumberString) - 1;
451+
lineNumbersMaxDigits = startAt;
441452
if (element.properties) {
442-
element.properties['data-line-numbers'] = '';
443-
}
444-
445-
const lineNumbersStartAtMatch = meta.match(
446-
/showLineNumbers=(\d+)/,
447-
);
448-
const startNumberString = lineNumbersStartAtMatch?.[1];
449-
if (startNumberString) {
450-
const startAt = Number(startNumberString) - 1;
451-
lineNumbersMaxDigits = startAt;
452-
if (element.properties) {
453-
element.properties.style = `counter-set: line ${startAt};`;
454-
}
453+
element.properties.style = `counter-set: line ${startAt};`;
455454
}
456455
}
457456
}

packages/core/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export function parseBlockMetaString(
5757
let meta = filter(
5858
(element.data?.meta ?? element.properties?.metastring ?? '') as string,
5959
);
60+
const showLineNumbers = /showLineNumbers/i.test(meta);
61+
meta = meta.replace(/showLineNumbers/i, '').trim();
6062

6163
const titleMatch = meta.match(/title="([^"]*)"/);
6264
const title = titleMatch?.[1] ?? null;
@@ -81,6 +83,7 @@ export function parseBlockMetaString(
8183
caption,
8284
lang,
8385
meta,
86+
showLineNumbers,
8487
};
8588
}
8689

0 commit comments

Comments
 (0)