fix(frontend): render single tilde literally instead of GFM strikethrough (#3963)
* fix(markdown): 单个波浪号不再被解析成删除线 GFM 会把区间标记(如 2020~2035)里的单个 `~` 误解析成删除线。 覆盖 marked 的 del tokenizer:`~~...~~` 仍走默认删除线规则, 单个 `~` 则按字面保留;并新增单测覆盖时间/区间场景。 * fix(markdown): register the literal single-tilde tokenizer once doc-content.vue and WikiBrowser.vue called marked.use() for the del tokenizer override inside <script setup>, so every mount wrapped the global marked tokenizer one more time. Move the extension into utils/markedLiteralTilde.ts and register it on the global marked behind a once-only guard; the preview Marked instance reuses the same extension object. Add a test that repeated registration does not re-wrap tokenizer.del. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>main
parent
336f84c3be
commit
4dd19b5356
|
|
@ -17,6 +17,7 @@ import {
|
|||
import { MessagePlugin } from "tdesign-vue-next";
|
||||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages, isValidURL } from '@/utils/security';
|
||||
import { normalizeSpuriousTablePrefixes } from '@/utils/markdownTableNormalize';
|
||||
import { ensureLiteralSingleTildeOnGlobalMarked } from '@/utils/markedLiteralTilde';
|
||||
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
|
||||
import { diffWikiLines, type WikiDiffLine } from '@/utils/wikiLineDiff';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
|
@ -514,6 +515,7 @@ marked.use({
|
|||
gfm: true, // 启用 GitHub Flavored Markdown
|
||||
});
|
||||
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||
ensureLiteralSingleTildeOnGlobalMarked();
|
||||
|
||||
const preprocessMathDelimiters = (rawText: string): string => {
|
||||
if (!rawText || typeof rawText !== 'string') {
|
||||
|
|
|
|||
|
|
@ -649,3 +649,19 @@ test('collapseStandaloneCitationParagraphs merges citations across empty paragra
|
|||
assert.match(out, /Steps:.*citation-kb/s)
|
||||
assert.doesNotMatch(out, /<p><\/p>/)
|
||||
})
|
||||
|
||||
test('renderChatMarkdown keeps ~~strikethrough~~ while leaving single tildes literal', () => {
|
||||
const renderer = createChatMarkdownRenderer()
|
||||
const options = {
|
||||
renderer,
|
||||
escapeMarkdown: (text: string) => text,
|
||||
sanitizeHtml: (html: string) => html,
|
||||
streaming: false,
|
||||
}
|
||||
const html = renderChatMarkdown('~~a~~ and ~b~ and 2020~2035, 8:00~12:00.', options)
|
||||
assert.equal((html.match(/<del>/g) || []).length, 1)
|
||||
assert.match(html, /<del>a<\/del>/)
|
||||
assert.match(html, /~b~/)
|
||||
assert.match(html, /2020~2035/)
|
||||
assert.match(html, /8:00~12:00/)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import markedKatex from 'marked-katex-extension'
|
|||
import type { Tokens } from 'marked'
|
||||
|
||||
import type { CachedMermaidSvgHtml } from './mermaidStreaming.ts'
|
||||
import { ensureLiteralSingleTildeOnGlobalMarked } from './markedLiteralTilde.ts'
|
||||
import { normalizeSandboxArtifactRefs } from './sandboxArtifactRefs.ts'
|
||||
import {
|
||||
collapseStandaloneCitationParagraphs,
|
||||
|
|
@ -49,6 +50,7 @@ export type RenderChatMarkdownOptions = {
|
|||
export function configureMarkedForChatMarkdown(): void {
|
||||
if (markedConfigured) return
|
||||
marked.use({ breaks: true, gfm: true })
|
||||
ensureLiteralSingleTildeOnGlobalMarked()
|
||||
marked.use(markedKatex({ throwOnError: false, nonStandard: true }))
|
||||
markedConfigured = true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { Marked, Renderer } from 'marked'
|
|||
import markedKatex from 'marked-katex-extension'
|
||||
|
||||
import { domPurifyAllowedUriRegexp } from './markdownDomPurify.ts'
|
||||
import { literalSingleTildeExtension } from './markedLiteralTilde.ts'
|
||||
import { escapeHTML, safeMarkdownToHTML, sanitizeDocumentPreviewHTML } from './security.ts'
|
||||
|
||||
const PREVIEW_IMAGE_ATTRIBUTES = 'loading="lazy" decoding="async" fetchpriority="low"'
|
||||
|
|
@ -50,6 +51,7 @@ const previewMarked = new Marked({
|
|||
renderer: createPreviewMarkdownRenderer(),
|
||||
})
|
||||
previewMarked.use(markedKatex({ throwOnError: false, nonStandard: true }))
|
||||
previewMarked.use(literalSingleTildeExtension)
|
||||
|
||||
function preprocessMathDelimiters(rawText: string): string {
|
||||
if (!rawText || typeof rawText !== 'string') return ''
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { Marked, marked } from 'marked'
|
||||
|
||||
import {
|
||||
ensureLiteralSingleTildeOnGlobalMarked,
|
||||
literalSingleTildeExtension,
|
||||
} from './markedLiteralTilde.ts'
|
||||
|
||||
test('literalSingleTildeExtension keeps ~~strikethrough~~ and leaves single tildes literal', () => {
|
||||
const instance = new Marked({ gfm: true })
|
||||
instance.use(literalSingleTildeExtension)
|
||||
const html = instance.parse('~~a~~ and ~b~ and 2020~2035', { async: false }) as string
|
||||
assert.equal((html.match(/<del>/g) || []).length, 1)
|
||||
assert.match(html, /<del>a<\/del>/)
|
||||
assert.match(html, /~b~/)
|
||||
assert.match(html, /2020~2035/)
|
||||
})
|
||||
|
||||
test('ensureLiteralSingleTildeOnGlobalMarked registers on the global marked only once', () => {
|
||||
ensureLiteralSingleTildeOnGlobalMarked()
|
||||
// Each marked.use wraps tokenizer.del again; a stable reference means no re-wrap.
|
||||
const del = marked.defaults.tokenizer?.del
|
||||
assert.equal(typeof del, 'function')
|
||||
ensureLiteralSingleTildeOnGlobalMarked()
|
||||
ensureLiteralSingleTildeOnGlobalMarked()
|
||||
assert.equal(marked.defaults.tokenizer?.del, del)
|
||||
})
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import { marked, type MarkedExtension } from 'marked'
|
||||
|
||||
// marked's GFM `del` tokenizer matches a lone `~` (e.g. the range
|
||||
// `2020~2035`), turning it into strikethrough. Override it so a `~~`-prefixed
|
||||
// run returns `false` and falls back to the built-in rule, while anything
|
||||
// else returns `undefined` and renders the `~` as literal text.
|
||||
export const literalSingleTildeExtension: MarkedExtension = {
|
||||
tokenizer: {
|
||||
del(src: string) {
|
||||
if (src.startsWith('~~')) return false
|
||||
return undefined
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
let registeredOnGlobalMarked = false
|
||||
|
||||
// Every `marked.use` call wraps the global tokenizer once more, so components
|
||||
// that share the global `marked` must not register this per mount.
|
||||
export function ensureLiteralSingleTildeOnGlobalMarked(): void {
|
||||
if (registeredOnGlobalMarked) return
|
||||
marked.use(literalSingleTildeExtension)
|
||||
registeredOnGlobalMarked = true
|
||||
}
|
||||
|
|
@ -797,6 +797,7 @@ import { useMenuStore } from '@/stores/menu'
|
|||
import { useSettingsStore } from '@/stores/settings'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { marked } from 'marked'
|
||||
import { ensureLiteralSingleTildeOnGlobalMarked } from '@/utils/markedLiteralTilde'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { RecycleScroller } from 'vue-virtual-scroller'
|
||||
import { hydrateProtectedFileImages, sanitizeMarkdownHTML } from '@/utils/security'
|
||||
|
|
@ -842,6 +843,8 @@ const route = useRoute()
|
|||
const menuStore = useMenuStore()
|
||||
const settingsStore = useSettingsStore()
|
||||
|
||||
ensureLiteralSingleTildeOnGlobalMarked()
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
const props = defineProps<{
|
||||
|
|
|
|||
Loading…
Reference in New Issue