/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/marked/extensions/superSubscript.ts
66 строк
2 KB
Ran Luo
feat(muya): migrate TS rewrite to packages/muya alongside legacy muyajs (#4314)
29 май 2026, 15:35
Не верифицирован
29 май 2026, 15:35
0d86641
Код
Авторство
О чём код?
const SUB_START_REG = /(?:\s|^)(~)(?!\1)/; const SUB_REG = /^(~)((?:[^~\s]|(?<=\\)\1|(?<=\\) )+?)(?<!\\)\1(?!\1)/; // TODO: why \S in sup??? const SUP_START_REG = /(?:\S|^)(\^)(?!\1)/; const SUP_REG = /^(\^)((?:[^^\s]|(?<=\\)\1|(?<=\\) )+?)(?<!\\)\1(?!\1)/; interface IScriptToken { type: string; raw: string; text: string; marker: string; } const START_HASH = { superscript: SUP_START_REG, subscript: SUB_START_REG, }; const REG_HASH = { superscript: SUP_REG, subscript: SUB_REG, }; export default function () { return { extensions: [getExtension('superscript'), getExtension('subscript')], }; } function getExtension(name: 'superscript' | 'subscript') { return { name, level: 'inline' as const, start(src: string) { const match = src.match(START_HASH[name]); if (!match) return; const index = (match.index || 0) + match[1].length; const possibleSubSup = src.substring(index); if (SUP_REG.test(possibleSubSup) || SUB_REG.test(possibleSubSup)) return index; }, tokenizer(src: string) { const match = src.match(REG_HASH[name]); if (match) { return { type: name, raw: match[0], text: match[2].trim(), marker: match[1], }; } }, renderer(token: IScriptToken) { const { text, marker } = token; const tagName = marker === '^' ? 'sup' : 'sub'; return `<${tagName}>${text}</${tagName}>`; }, }; }