Skip to content

Commit

Permalink
chore: improve showLineNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Aug 28, 2024
1 parent 8c1a2c3 commit 003f49d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
47 changes: 23 additions & 24 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ function apply(

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

if (!(isElement(pre) && pre.properties)) {
return [];
}

const code = pre.children[0];
const [code] = pre.children;

// Remove extraneous classes
if (
Expand Down Expand Up @@ -353,11 +353,12 @@ export function rehypePrettyCode(
if (!isElement(codeElement)) return;
const [textElement] = codeElement.children;

const { title, caption, meta, lang } = parseBlockMetaString(
codeElement,
filterMetaString,
defaultCodeBlockLang,
);
const { title, caption, meta, lang, showLineNumbers } =
parseBlockMetaString(
codeElement,
filterMetaString,
defaultCodeBlockLang,
);

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

Expand Down Expand Up @@ -434,25 +435,23 @@ export function rehypePrettyCode(

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
visit(codeTree, 'element', (element) => {
if (element.tagName === 'code') {
const showLineNumbers = /(?:^|\s)showLineNumbers(?:\s|$)/.test(
meta,
if (
(element.tagName === 'code' || element.tagName === 'pre') &&
showLineNumbers
) {
if (element.properties) {
element.properties['data-line-numbers'] = '';
}

const lineNumbersStartAtMatch = meta.match(
/showLineNumbers=(\d+)/i,
);
if (showLineNumbers) {
const startNumberString = lineNumbersStartAtMatch?.[1];
if (startNumberString) {
const startAt = Number(startNumberString) - 1;
lineNumbersMaxDigits = startAt;
if (element.properties) {
element.properties['data-line-numbers'] = '';
}

const lineNumbersStartAtMatch = meta.match(
/showLineNumbers=(\d+)/,
);
const startNumberString = lineNumbersStartAtMatch?.[1];
if (startNumberString) {
const startAt = Number(startNumberString) - 1;
lineNumbersMaxDigits = startAt;
if (element.properties) {
element.properties.style = `counter-set: line ${startAt};`;
}
element.properties.style = `counter-set: line ${startAt};`;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function parseBlockMetaString(
let meta = filter(
(element.data?.meta ?? element.properties?.metastring ?? '') as string,
);
const showLineNumbers = /showLineNumbers/i.test(meta);
meta = meta.replace(/showLineNumbers/i, '').trim();

const titleMatch = meta.match(/title="([^"]*)"/);
const title = titleMatch?.[1] ?? null;
Expand All @@ -85,6 +87,7 @@ export function parseBlockMetaString(
caption,
lang,
meta,
showLineNumbers,
};
}

Expand Down

0 comments on commit 003f49d

Please sign in to comment.