import vocabulary from "../../../scripts/reader_appendix_language.rules.json"; type VocabularyRule = Readonly<{ pattern: string; ignoreCase?: boolean; replacement?: string; labels?: Readonly>; labelGroup?: number; prefix?: string; beforeGroups?: readonly number[]; afterGroups?: readonly number[]; }>; // One rule source for Python and TS; the JSON ships in both existing images. const chartFence = new RegExp(vocabulary.chartFencePattern, "m"); const replacements = (vocabulary.rules as readonly VocabularyRule[]).map((rule) => ({ rule, pattern: new RegExp(rule.pattern, rule.ignoreCase ? "gi" : "g"), })); export function cleanReaderAppendixMarkdown(markdown: string): string { return markdown.split(chartFence).map((part, index) => { if (index % 2) return part; return replacements.reduce((value, { pattern, rule }) => value.replace(pattern, (...match) => { if (rule.replacement !== undefined) return rule.replacement; return (rule.prefix ?? "") + (rule.beforeGroups ?? []).map((group) => match[group]).join("") + rule.labels![match[rule.labelGroup!]] + (rule.afterGroups ?? []).map((group) => match[group]).join(""); }), part); }).join(""); }