Skip to content

Commit 003f49d

Browse files
committed
chore: improve showLineNumber
1 parent 8c1a2c3 commit 003f49d

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 (
@@ -353,11 +353,12 @@ export function rehypePrettyCode(
353353
if (!isElement(codeElement)) return;
354354
const [textElement] = codeElement.children;
355355

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

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

@@ -434,25 +435,23 @@ export function rehypePrettyCode(
434435

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

packages/core/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export function parseBlockMetaString(
6161
let meta = filter(
6262
(element.data?.meta ?? element.properties?.metastring ?? '') as string,
6363
);
64+
const showLineNumbers = /showLineNumbers/i.test(meta);
65+
meta = meta.replace(/showLineNumbers/i, '').trim();
6466

6567
const titleMatch = meta.match(/title="([^"]*)"/);
6668
const title = titleMatch?.[1] ?? null;
@@ -85,6 +87,7 @@ export function parseBlockMetaString(
8587
caption,
8688
lang,
8789
meta,
90+
showLineNumbers,
8891
};
8992
}
9093

0 commit comments

Comments
 (0)