fix(outboundtext): keep resource handles intact when normalizing formulas

This commit is contained in:
姚章浩
2026-09-18 17:14:45 +08:00
parent 48b741984f
commit 1bbaf2f07a
6 changed files with 84 additions and 21 deletions
+13 -13
View File
@@ -11,19 +11,19 @@ import (
// well-known math commands, so ordinary prose is left byte-identical.
var (
mathBlockDelimRE = regexp.MustCompile(`\$\$([\s\S]+?)\$\$`)
mathInlineDelimRE = regexp.MustCompile(`\$([^$\n]{1,400})\$`)
parenDelimRE = regexp.MustCompile(`\\\(([\s\S]+?)\\\)`)
bracketDelimRE = regexp.MustCompile(`\\\[([\s\S]+?)\\\]`)
sqrtRE = regexp.MustCompile(`\\sqrt\s*(?:\[([^\]]{1,20})\])?\s*\{([^{}]{1,200})\}`)
fracRE = regexp.MustCompile(`\\frac\s*\{([^{}]{1,200})\}\s*\{([^{}]{1,200})\}`)
supBracedRE = regexp.MustCompile(`\^\s*\{([^{}]{1,40})\}`)
subBracedRE = regexp.MustCompile(`_\s*\{([^{}]{1,40})\}`)
supCharRE = regexp.MustCompile(`\^([0-9A-Za-z+\-=()])`)
subCharRE = regexp.MustCompile(`_([0-9A-Za-z+\-=()])`)
commandRE = regexp.MustCompile(`\\([A-Za-z]+)`)
braceDelimRE = regexp.MustCompile(`\{([^{}]{0,200})\}`)
blankRunRE = regexp.MustCompile(`[ \t]{2,}`)
mathBlockDelimRE = regexp.MustCompile(`\$\$([\s\S]+?)\$\$`)
mathSpanRE = regexp.MustCompile(`(?s)\$\$.+?\$\$|\\\[.+?\\\]|\\\(.+?\\\)|\$[^$\n]{1,400}\$`)
parenDelimRE = regexp.MustCompile(`\\\(([\s\S]+?)\\\)`)
bracketDelimRE = regexp.MustCompile(`\\\[([\s\S]+?)\\\]`)
sqrtRE = regexp.MustCompile(`\\sqrt\s*(?:\[([^\]]{1,20})\])?\s*\{([^{}]{1,200})\}`)
fracRE = regexp.MustCompile(`\\frac\s*\{([^{}]{1,200})\}\s*\{([^{}]{1,200})\}`)
supBracedRE = regexp.MustCompile(`\^\s*\{([^{}]{1,40})\}`)
subBracedRE = regexp.MustCompile(`_\s*\{([^{}]{1,40})\}`)
supCharRE = regexp.MustCompile(`\^([0-9A-Za-z+\-=()])`)
subCharRE = regexp.MustCompile(`_([0-9A-Za-z+\-=()])`)
commandRE = regexp.MustCompile(`\\([A-Za-z]+)`)
braceDelimRE = regexp.MustCompile(`\{([^{}]{0,200})\}`)
blankRunRE = regexp.MustCompile(`[ \t]{2,}`)
)
var mathCommands = map[string]string{
+27 -6
View File
@@ -6,14 +6,36 @@ func NormalizeMath(markdown string) string {
if !carriesMath(markdown) {
return markdown
}
content := markdown
content := mathSpanRE.ReplaceAllStringFunc(markdown, convertMathSpan)
// Outside math spans only LaTeX commands are converted. Identifiers and URLs
// (resource://…_pltQ) must never be rewritten: a mangled reference can no
// longer be published, and the outbound sanitizer then drops the whole image.
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
content = blankRunRE.ReplaceAllString(content, " ")
return content
}
func convertMathSpan(span string) string {
inner := span
switch {
case strings.HasPrefix(inner, "$$"):
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$$"), "$$")
return "\n" + convertMath(inner) + "\n"
case strings.HasPrefix(inner, `\[`):
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\[`), `\]`)
case strings.HasPrefix(inner, `\(`):
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\(`), `\)`)
default:
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$"), "$")
}
return convertMath(inner)
}
func convertMath(inner string) string {
content := inner
for from, to := range escapedPunctuation {
content = strings.ReplaceAll(content, from, to)
}
content = mathBlockDelimRE.ReplaceAllString(content, "\n$1\n")
content = parenDelimRE.ReplaceAllString(content, "$1")
content = bracketDelimRE.ReplaceAllString(content, "$1")
content = mathInlineDelimRE.ReplaceAllString(content, "$1")
for i := 0; i < 3; i++ {
next := sqrtRE.ReplaceAllStringFunc(content, expandRoot)
next = fracRE.ReplaceAllString(next, "($1)/($2)")
@@ -28,7 +50,6 @@ func NormalizeMath(markdown string) string {
}
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
content = braceDelimRE.ReplaceAllString(content, "$1")
content = blankRunRE.ReplaceAllString(content, " ")
return content
}
+2
View File
@@ -12,6 +12,8 @@ func TestNormalizeMath(t *testing.T) {
{"nth root", `$\sqrt[3]{V}$`, `√[3](V)`},
{"no math untouched", `C:\new\folder 与 100% 覆盖`, `C:\new\folder 与 100% 覆盖`},
{"dollar free prose", `价格是 5 元, 无公式`, `价格是 5 元, 无公式`},
{"resource url untouched", `![图](resource://efVTXx0gY9Lk2vuZM_pltQ) 公式 $r = \sqrt{X^2 + Y^2}$`, `![图](resource://efVTXx0gY9Lk2vuZM_pltQ) 公式 r = √(X² + Y²)`},
{"code span untouched", "`x_1` 与 $x_1$", "`x_1` 与 x₁"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {