/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/marked/getHighlightHtml.ts
88 строк
3 KB
Ran Luo
fix(muya): treat CJK as punctuation for strong/em flanking (#4307) (#4401)
08 июн 2026, 12:52
Не верифицирован
08 июн 2026, 12:52
79a2926
Код
Авторство
О чём код?
import type { ILexOption } from './types'; import { Marked } from 'marked'; import { markedHighlight } from 'marked-highlight'; import Prism from 'prismjs'; import cjkEmStrongExtension from './extensions/cjkEmStrong'; import emojiExtension from './extensions/emoji'; import footnoteExtension from './extensions/footnote'; import mathExtension from './extensions/math'; import superSubScriptExtension from './extensions/superSubscript'; import fm, { frontMatterRender } from './frontMatter'; import { DEFAULT_OPTIONS } from './options'; import walkTokens from './walkTokens'; const DIAGRAM_TYPE = [ 'mermaid', 'plantuml', 'vega-lite', 'flowchart', 'sequence', ]; function highlight(code: string, lang: string) { // Language may be undefined (GH#591) if (!lang) return code; if (DIAGRAM_TYPE.includes(lang)) return code; const grammar = Prism.languages[lang]; if (!grammar) { console.warn(`Unable to find grammar for "${lang}".`); return code; } return Prism.highlight(code, grammar, lang); } export function getHighlightHtml(src: string, options: ILexOption = {}) { options = Object.assign({}, DEFAULT_OPTIONS, options); const { footnote, frontMatter, math, isGitlabCompatibilityEnabled, superSubScript } = options; // Build a fresh Marked instance per call. `Marked.use({ walkTokens })` // chains rather than replaces, so reusing a module-level singleton would // cause walkTokens to fire N times after N invocations and corrupt token // state (e.g. wiping `lang` on subsequent runs). const marked = new Marked(markedHighlight({ highlight })); marked.use({ walkTokens: walkTokens({ math, isGitlabCompatibilityEnabled }), }); // Treat CJK characters as punctuation for emphasis/strong flanking so // `中文**"加粗"**中文` bolds (marktext/marktext#4307). Additive override — // never regresses spec-conformant Latin emphasis. marked.use(cjkEmStrongExtension()); marked.use(emojiExtension({ isRenderEmoji: true })); if (math) { marked.use( mathExtension({ throwOnError: false, useKatexRender: true, }), ); } if (superSubScript) marked.use(superSubScriptExtension()); if (footnote) marked.use(footnoteExtension()); let html = ''; if (frontMatter) { const { token, src: newSrc } = fm(src); if (token) { html = frontMatterRender(token); src = newSrc; } } html += marked.parse(src); return html; }