fix(wiki): skip ambiguous single-Han auto-links (#2860)

main
JYao-Chen 2026-09-21 13:08:04 +08:00 committed by GitHub
parent 2f2bcc7aa7
commit e3d52e8353
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -47,6 +47,14 @@ func linkifyContent(content string, refs []linkRef, selfSlug string) (string, bo
if r.slug == selfSlug {
continue
}
// A single Han character has no reliable token boundary in unsegmented
// Chinese prose. Substring-linking it would turn words such as 风沙、
// 栏目 and 核心 into broken links. Keep one-character Wiki pages and
// any explicit [[...]] links authored by the generator, but never
// inject a new cross-link for such a surface form.
if isSingleHanRune(r.matchText) {
continue
}
sorted = append(sorted, r)
}
sort.SliceStable(sorted, func(i, j int) bool {
@ -125,6 +133,15 @@ func hasASCIILetterEdge(s string) bool {
return isASCIIWordRune(first) || isASCIIWordRune(last)
}
// isSingleHanRune reports whether s consists of exactly one Han character.
func isSingleHanRune(s string) bool {
if s == "" {
return false
}
r, size := utf8.DecodeRuneInString(s)
return size == len(s) && unicode.Is(unicode.Han, r)
}
func isASCIIWordRune(r rune) bool {
if r > unicode.MaxASCII {
return false

View File

@ -37,6 +37,34 @@ func TestLinkifyContent_LongerNameWinsOverSubstring(t *testing.T) {
}
}
func TestLinkifyContent_SkipsSingleHanRuneAutoLink(t *testing.T) {
refs := []linkRef{
{slug: "entity/feng", matchText: "风"},
}
in := "穿越河西走廊体验大漠风沙。"
got, changed := linkifyContent(in, refs, "")
if changed {
t.Fatalf("single Han rune must not be auto-linked: %q", got)
}
if got != in {
t.Fatalf("content changed: got %q, want %q", got, in)
}
}
func TestLinkifyContent_PreservesExplicitSingleHanRuneLink(t *testing.T) {
refs := []linkRef{
{slug: "entity/feng", matchText: "风"},
}
in := "寓言中,[[entity/feng|风]]羡慕目。"
got, changed := linkifyContent(in, refs, "")
if changed {
t.Fatalf("explicit single-character link must be preserved without reinjection: %q", got)
}
if got != in {
t.Fatalf("content changed: got %q, want %q", got, in)
}
}
func TestLinkifyContent_ASCIIWordBoundary(t *testing.T) {
refs := []linkRef{
{slug: "ai", matchText: "AI"},