Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';
/** Prefix used by Markdown backslash escapes. */const MARKDOWN_ESCAPE_PREFIX = '\\';
/** HTML text characters whose Markdown escape marker must not survive into raw HTML. */const MARKDOWN_HTML_TEXT_ESCAPE_TARGETS: ReadonlySet<string> = new Set([ '|', '&', '<', '>', '"', "'",],);
/** Whether one character is meaningful after a Markdown escape marker when cell text is moved into raw HTML. @param character - character being checked because only HTML-sensitive Markdown escapes need normalization @returns whether Markdown escape marker should be consumed before HTML escaping @example ```ts isMarkdownHtmlTextEscapeTarget('<'); // true ``` */function isMarkdownHtmlTextEscapeTarget(character: string,): boolean { return MARKDOWN_HTML_TEXT_ESCAPE_TARGETS.has(character,);}
/** Markdown cell text with HTML-sensitive backslash escapes converted to their intended literal characters. Browsers do not treat Markdown backslashes as escapes inside raw HTML, so the marker must be consumed before HTML escaping. @param markdownCellText - source cell text because micromark leaves Markdown escapes in place @returns Markdown cell text ready for HTML text escaping @example ```ts normalizeMarkdownEscapes(String.raw`\<img>`); // '<img>' ``` */function normalizeMarkdownEscapes(markdownCellText: string,): string { /** Source text scanned by UTF-16 index; only ASCII escape sentinels matter. */ const characters = markdownCellText; /** Text after Markdown escape markers have been consumed. */ const normalized: string[] = [];
for (let loopIndex = 0; loopIndex < characters.length; loopIndex += 1) { /** Current character under the cursor. */ const character = nonNullishOrThrow(characters[loopIndex],); /** Next character, used to decide whether a backslash is an escape marker. */ const nextCharacter = characters[loopIndex + 1];
if (character !== MARKDOWN_ESCAPE_PREFIX) { normalized.push(character,); continue; }
if (nextCharacter === undefined) { normalized.push(character,); continue; }
if (!isMarkdownHtmlTextEscapeTarget(nextCharacter,)) { normalized.push(character,); continue; }
normalized.push(nextCharacter,); loopIndex += 1; }
return normalized.join('',);}
/** HTML text entity for one character. @param character - character being emitted because raw HTML text context treats markup characters specially @returns escaped HTML text for that character @example ```ts htmlTextCharacter('&'); // '&' ``` */function htmlTextCharacter(character: string,): string { if (character === '&') { return '&'; } if (character === '<') { return '<'; } if (character === '>') { return '>'; } if (character === '"') { return '"'; } if (character === "'") { return '''; } return character;}
/** Escape text for an HTML text node. @param text - normalized text because raw HTML cells must not receive markup directly @returns text safe for interpolation between `<th>` or `<td>` tags @example ```ts escapeHtmlText('<img>'); // '<img>' ``` */function escapeHtmlText(text: string,): string { /** Characters escaped for HTML text context. */ const escaped: string[] = []; for (const character of text) { escaped.push( htmlTextCharacter(character,), ); } return escaped.join('',);}
/** Convert Markdown table cell source to safe HTML table cell text. Normalizes escapes with {@link normalizeMarkdownEscapes}, then escapes the result with {@link escapeHtmlText}. @param markdownCellText - source cell text because autofix persists it inside raw HTML @returns safe HTML cell text, preserving literal pipes while neutralizing HTML markup @example ```ts htmlTableCellText(String.raw`a \| \<img>`); // 'a | <img>' ``` */export function htmlTableCellText(markdownCellText: string,): string { return escapeHtmlText( normalizeMarkdownEscapes(markdownCellText,) .trim(), );}