/* eslint-disable max-len */ import type {MarkdownItPluginCb} from './typings'; import type {IDGenerator} from './utils'; import {escapeHtml} from 'markdown-it/lib/common/utils'; import {generateID as globalGenerateID} from './utils'; const wrapInFloatingContainer = ( element: string | undefined, id: number, lineWrapping: boolean, hasWrap: boolean, ) => { const buttonClassNames = [ 'g-button', 'g-button_view_flat', 'g-button_size_m', 'g-button_pin_round-round', 'g-md-viewer-code-button', 'yfm-code-button', 'yfm-wrapping-button', ]; if (hasWrap) { buttonClassNames.push('g-button_selected'); } const wrappingButton = lineWrapping ? `` : ''; return `
${element}
${wrappingButton}
`; }; interface EnvTerm { terms: { [keys: string]: string; }; } function buildTermRegExp(env: EnvTerm, escape: (str: string) => string): RegExp | null { const termKeys = Object.keys(env.terms); if (!termKeys.length) { return null; } const regTerms = termKeys .map((el) => el.slice(1)) .map(escape) .join('|'); // The title must stay on a single line: a multi-line match would be collapsed // into a one-word placeholder and shift line numbers and prompt flags. return new RegExp(String.raw`\[([^\[\n]+)\](\(\*(${regTerms})\))`, 'g'); } /** * @param title - Term title, already HTML-escaped. * @param termKey - Raw term key from the markdown source. * @param generateID - ID generator for the term title element. * @returns Term title markup with escaped attribute values. */ function renderTerm(title: string, termKey: string, generateID: IDGenerator): string { const key = escapeHtml(termKey); const id = escapeHtml(generateID(termKey)); return `${title}`; } const TERM_PLACEHOLDER_PREFIX = 'yfmterm'; const TERM_PLACEHOLDER_END = 'z'; const TERM_PLACEHOLDER_DIGITS = 'abcdefghij'; interface TermPlaceholders { prefix: string; placeholders: Map; } /** * Encodes a non-negative integer with letters `a`-`j` (one per decimal digit) * so the resulting token contains neither digits nor the terminator letter. * * @param value - Non-negative integer. * @returns Letter-only representation of `value`. */ function encodeIndex(value: number): string { return String(value) .split('') .map((digit) => TERM_PLACEHOLDER_DIGITS[Number(digit)]) .join(''); } /** * Replaces `[title](*key)` term references in the raw fence content with * letter-only placeholders BEFORE the content reaches the highlighter. * * Term markup is replaced on the raw source rather than on the highlighted * HTML because highlight.js splits the pattern with `` tags for many * grammars (e.g. `[` `]` and `(*key)` get different token classes in yaml, * keywords like `type` get wrapped in typescript). A placeholder made of * lowercase letters only is tokenized as a single word by most grammars, and * {@link restoreTermPlaceholders} tolerates the rare splits (e.g. `$y` char * literals in erlang) by matching across `` boundaries. * * @param content - Raw fence content. * @param reg - Term reference pattern built from the defined term keys. * @param generateID - ID generator for the term title element. * @returns Content with placeholders and the data needed to restore them. */ function replaceTermsWithPlaceholders( content: string, reg: RegExp, generateID: IDGenerator, ): {content: string} & TermPlaceholders { // Make sure the prefix never collides with something already in the code. let prefix = TERM_PLACEHOLDER_PREFIX; while (content.includes(prefix)) { prefix += 'x'; } const placeholders = new Map(); reg.lastIndex = 0; const replaced = content.replace( reg, (_match: string, title: string, _p2: string, key: string) => { const placeholder = prefix + encodeIndex(placeholders.size) + TERM_PLACEHOLDER_END; placeholders.set(placeholder, renderTerm(escapeHtml(title), key, generateID)); return placeholder; }, ); return {content: replaced, prefix, placeholders}; } const HTML_TAG_RE = /<[^>]*>/g; /** * Re-emits the tags found inside a split placeholder around the term markup * so that the surrounding spans stay balanced: spans closed inside the * placeholder are closed before the term, spans opened inside it are reopened * after the term, and spans fully contained in it are dropped. * * @param fragment - Matched HTML fragment of a placeholder with tags inside. * @param markup - Term markup to insert in place of the placeholder. * @returns Replacement HTML keeping the span nesting intact. */ function wrapTermWithSplitTags(fragment: string, markup: string): string { const closeBefore: string[] = []; const openAfter: string[] = []; for (const tag of fragment.match(HTML_TAG_RE) ?? []) { if (!tag.startsWith(']*>)*'; const placeholderRE = new RegExp( prefix.split('').join(tags) + tags + `(?:[${TERM_PLACEHOLDER_DIGITS}]${tags})+` + TERM_PLACEHOLDER_END, 'g', ); return html.replace(placeholderRE, (fragment) => { const placeholder = fragment.replace(HTML_TAG_RE, ''); const markup = placeholders.get(placeholder); if (!markup) { return fragment; } return placeholder === fragment ? markup : wrapTermWithSplitTags(fragment, markup); }); } /** * Fallback for fence renderers that do not render `token.content` verbatim: * replaces term references that are still intact in the rendered HTML. */ function termReplace(str: string, reg: RegExp, generateID: IDGenerator): string { reg.lastIndex = 0; return str.replace(reg, (_match: string, title: string, _p2: string, key: string) => renderTerm(title, key, generateID), ); } const SPAN_TAG_RE = /]*>|<\/span>/g; /** * Balances hljs span tags within each line so that every line is a * self-contained HTML fragment with no unclosed or unopened spans. * * highlight.js can produce spans that cross multiple lines (e.g. multiline strings). * Before: * `['\'line1', 'line2\'']` * After: * `['\'line1', 'line2\'']` * @param lines - Highlighted HTML split into separate lines. * @returns Lines with balanced `` tags on each line. */ function balanceSpansPerLine(lines: string[]): string[] { const openTagStack: string[] = []; return lines.map((line) => { const prefix = openTagStack.join(''); SPAN_TAG_RE.lastIndex = 0; let match: RegExpExecArray | null; while ((match = SPAN_TAG_RE.exec(line)) !== null) { if (match[0] === '') { openTagStack.pop(); } else { openTagStack.push(match[0]); } } const suffix = ''.repeat(openTagStack.length); return prefix + line + suffix; }); } /** * Strips the prompt prefix from the raw fence content BEFORE it reaches the highlighter. * * @param content - Raw fence content. * @param prompt - Raw prompt string (e.g. `$`, `#`, `>>>`). * @returns Stripped content (same number of lines) and a per-line prompt flag array. */ function stripPrompt(content: string, prompt: string): {content: string; flags: boolean[]} { const hasTrailingNewline = content.endsWith('\n'); const lines = content.split('\n'); const linesToProcess = hasTrailingNewline ? lines.slice(0, -1) : lines; const flags: boolean[] = []; const stripped = linesToProcess.map((line) => { const trimmed = line.trimStart(); const leadingWs = line.slice(0, line.length - trimmed.length); if (trimmed.startsWith(prompt + ' ')) { flags.push(true); return leadingWs + trimmed.slice(prompt.length + 1); } if (trimmed === prompt) { flags.push(true); return leadingWs; } flags.push(false); return line; }); return { content: stripped.join('\n') + (hasTrailingNewline ? '\n' : ''), flags, }; } function postProcessCode( code: string, { showLineNumbers, lineWrapping, promptFlags, promptEscaped, }: { showLineNumbers: boolean; lineWrapping: boolean; promptFlags?: boolean[]; promptEscaped?: string; }, ): string { const hasTrailingNewline = code.endsWith('\n'); const lines = code.split('\n'); const linesToProcess = hasTrailingNewline ? lines.slice(0, -1) : lines; const normalized = balanceSpansPerLine(linesToProcess); const maxDigits = String(normalized.length).length; return ( normalized .map((line, index) => { // The prompt was already stripped from the raw content before // highlighting, so `line` here is the highlighted command only. // Re-insert the prompt as a dedicated, non-selectable span after // any leading whitespace. `promptFlags` marks which lines carried // a prompt in the original source. if (promptFlags?.[index] && promptEscaped !== undefined) { const trimmed = line.trimStart(); const leadingWs = line.slice(0, line.length - trimmed.length); line = `${leadingWs}` + `` + `${trimmed}`; } if (showLineNumbers) { const lineNumber = String(index + 1).padStart(maxDigits, ' '); line = lineWrapping ? `${lineNumber}${line}` : `${lineNumber}${line}`; } return line; }) .join('\n') + (hasTrailingNewline ? '\n' : '') ); } /** * @param info - Raw fence info string (e.g. `js showLineNumbers prompt="$"`). * @returns Parsed flags and the optional prompt value. */ function parseFenceInfo(info: string): { showLineNumbers: boolean; wrapLines: boolean; prompt?: string; } { const promptMatch = /(?:^|\s)prompt=(?:"([^"]*)"|'([^']*)')/.exec(info); const prompt = promptMatch?.[1] ?? promptMatch?.[2]; const rest = promptMatch ? info.replace(promptMatch[0], ' ') : info; return { showLineNumbers: /\bshowLineNumbers\b/.test(rest), wrapLines: /\bwrap\b/.test(rest), prompt, }; } type CodeOptions = { /** * Show button to toggle line wrapping of code. * @type {boolean} * @default false */ codeLineWrapping?: boolean; }; const code: MarkdownItPluginCb = (md, opts) => { const lineWrapping = opts?.codeLineWrapping || false; const generateID = opts?.generateID ?? globalGenerateID; const superCodeRenderer = md.renderer.rules.fence; md.renderer.rules.fence = function (tokens, idx, options, env, self) { const token = tokens[idx]; const {showLineNumbers, prompt, wrapLines} = parseFenceInfo(token.info); const shouldWrap = wrapLines; // Strip the prompt prefix from the raw content BEFORE handing it // to the highlighter, so language grammars never misinterpret prompt // symbols (e.g. `#` as a shell comment, `$` as a variable). The prompt is // re-inserted as a span in postProcessCode, driven by `promptFlags`. let promptFlags: boolean[] | undefined; let promptEscaped: string | undefined; const originalContent = token.content; if (prompt) { const stripped = stripPrompt(token.content, prompt); token.content = stripped.content; promptFlags = stripped.flags; promptEscaped = escapeHtml(prompt); } // Swap term references for placeholders BEFORE highlighting so that // language grammars cannot split `[title](*key)` into several spans. // The placeholders are restored after the HTML is rendered. const termRE = env?.terms ? buildTermRegExp(env, md.utils.escapeRE) : null; const termPlaceholders = termRE ? replaceTermsWithPlaceholders(token.content, termRE, generateID) : null; if (termPlaceholders) { token.content = termPlaceholders.content; } let superCode = superCodeRenderer?.(tokens, idx, options, env, self); // Restore the original content so the mutation does not leak to other // consumers of the token. token.content = originalContent; if (superCode && (showLineNumbers || prompt)) { // Extract the code content from the pre/code tags const codeMatch = superCode.match(/]*>]*>([\s\S]*?)<\/code><\/pre>/); if (codeMatch) { const codeContent = codeMatch[1]; const processedCode = postProcessCode(codeContent, { showLineNumbers, lineWrapping, promptFlags, promptEscaped, }); // Escape $ in replacement string: $$ becomes a literal $ in String.replace() const escapedReplacement = processedCode.replace(/\$/g, '$$$$'); superCode = superCode.replace(codeContent, escapedReplacement); } } // Attach the raw prompt value as a data attribute on so that the // copy widget can strip it from the plain-text content without relying on // any class names or DOM structure introduced by the plugin. if (superCode && (prompt || shouldWrap)) { superCode = superCode.replace(/(]*>]*)>/, (_match, open, attrs) => { // Add the wrap class if (shouldWrap) { attrs = /class="[^"]*"/.test(attrs) ? attrs.replace(/class="([^"]*)"/, 'class="$1 wrap"') : ` class="wrap"${attrs}`; } // Add the data-prompt attribute if (prompt) { attrs = ` data-prompt="${escapeHtml(prompt)}"${attrs}`; } return `${open}${attrs}>`; }); } if (superCode && termRE && termPlaceholders) { superCode = restoreTermPlaceholders(superCode, termPlaceholders); superCode = termReplace(superCode, termRE, generateID); } return wrapInFloatingContainer(superCode, idx, lineWrapping, shouldWrap); }; }; export = code;