// markdown-it plugin for generating line numbers. // It depends on preWrapper plugin. import type MarkdownIt from 'markdown-it'; export const lineNumberPlugin = (md: MarkdownIt) => { const fence = md.renderer.rules.fence!; md.renderer.rules.fence = (...args) => { const rawCode = fence(...args); const code = rawCode.slice(rawCode.indexOf(''), rawCode.indexOf('')); const lines = code.split('\n'); const lineNumbersCode = [...Array(lines.length - 1)] .map((_line, index) => `${index + 1}
`) .join(''); const lineNumbersWrapperCode = `
${lineNumbersCode}
`; const finalCode = rawCode .replace(/<\/div>$/, `${lineNumbersWrapperCode}`) .replace(/"(language-\w+)"/, '"$1 line-numbers-mode"'); return finalCode; }; };