fix(openai): keep citation indexes one-based

This commit is contained in:
NgoQuocViet2001
2026-04-29 15:43:09 +07:00
parent 27eb73d48b
commit 0cbc2c875d
3 changed files with 45 additions and 2 deletions

View File

@@ -54,3 +54,31 @@ func TestReplaceCitationMarkersWithLinksSupportsReferenceZeroBased(t *testing.T)
t.Fatalf("expected %q, got %q", want, got)
}
}
func TestReplaceCitationMarkersWithLinksKeepsCitationOneBasedWithZeroBasedReference(t *testing.T) {
raw := "引用[citation:1],来源[reference:0],后续[reference:1]。"
links := map[int]string{
1: "https://example.com/first",
2: "https://example.com/second",
}
got := replaceCitationMarkersWithLinks(raw, links)
want := "引用[1](https://example.com/first),来源[0](https://example.com/first),后续[1](https://example.com/second)。"
if got != want {
t.Fatalf("expected %q, got %q", want, got)
}
}
func TestReplaceCitationMarkersWithLinksDetectsSpacedReferenceZeroBased(t *testing.T) {
raw := "来源[reference: 0] 与 [reference: 1]。"
links := map[int]string{
1: "https://example.com/first",
2: "https://example.com/second",
}
got := replaceCitationMarkersWithLinks(raw, links)
want := "来源[0](https://example.com/first) 与 [1](https://example.com/second)。"
if got != want {
t.Fatalf("expected %q, got %q", want, got)
}
}

View File

@@ -13,7 +13,7 @@ func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string {
if strings.TrimSpace(text) == "" || len(links) == 0 {
return text
}
zeroBased := strings.Contains(strings.ToLower(text), "[reference:0]")
zeroBasedReference := hasZeroBasedReferenceMarker(text)
return citationMarkerPattern.ReplaceAllStringFunc(text, func(match string) string {
sub := citationMarkerPattern.FindStringSubmatch(match)
if len(sub) < 3 {
@@ -24,7 +24,7 @@ func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string {
return match
}
lookupIdx := idx
if zeroBased {
if strings.EqualFold(sub[1], "reference") && zeroBasedReference {
lookupIdx = idx + 1
}
url := strings.TrimSpace(links[lookupIdx])
@@ -34,3 +34,16 @@ func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string {
return fmt.Sprintf("[%d](%s)", idx, url)
})
}
func hasZeroBasedReferenceMarker(text string) bool {
for _, sub := range citationMarkerPattern.FindAllStringSubmatch(text, -1) {
if len(sub) < 3 || !strings.EqualFold(sub[1], "reference") {
continue
}
idx, err := strconv.Atoi(strings.TrimSpace(sub[2]))
if err == nil && idx == 0 {
return true
}
}
return false
}