feat(notes): show line numbers on non-markdown file views
All checks were successful
CI / verify (push) Successful in 2m8s

CSS counters on the per-line spans, scoped to .code-file so fenced
blocks inside notes stay untouched. The gutter width adapts to the
file's line count and numbers are excluded from text selection.
This commit is contained in:
Julien Calixte
2026-07-03 16:57:09 +02:00
parent 84a252e926
commit 5386fa66ea
3 changed files with 74 additions and 2 deletions

View File

@@ -421,10 +421,22 @@ export const renderCodeFile = async ({
}): Promise<string> => {
await useShikiji()
const heading = filename ? `# ${filename}\n\n` : ""
// Drop the trailing newline so the final empty line isn't numbered.
const lines = rawContent.replace(/\n$/, "").split("\n")
// Sized gutter so line numbers stay right-aligned past 3 digits.
const wrapper = `<div class="code-file" style="--line-number-width:${String(lines.length).length}ch">`
if (lang !== null) {
return renderMarkdown(`${heading}\`\`\`\`${lang}\n${rawContent}\n\`\`\`\``)
const rendered = renderMarkdown(
`${heading}\`\`\`\`${lang}\n${lines.join("\n")}\n\`\`\`\``
)
// Shikiji always appends an empty line span; drop it so it isn't numbered.
return `${wrapper}${rendered.replace('\n<span class="line"></span></code>', "</code>")}</div>`
}
return `${renderMarkdown(heading)}<pre><code>${md.utils.escapeHtml(rawContent)}</code></pre>`
// Mirror Shikiji's per-line spans so the line-number CSS applies uniformly.
const code = lines
.map((line) => `<span class="line">${md.utils.escapeHtml(line)}</span>`)
.join("\n")
return `${wrapper}${renderMarkdown(heading)}<pre><code>${code}</code></pre></div>`
}
export const markdownBuilder = (defaultPrefix?: Ref<string> | string) => {