From 5e08336909ef3a94a838e86122ef0f523987425c Mon Sep 17 00:00:00 2001 From: mahmoodhamdi Date: Tue, 12 May 2026 12:47:59 +0300 Subject: [PATCH] fix: don't link hyphen-only numeric ranges as verse references The verse-reference regex used by FootnoteText accepted either ":" or "-" as the chapter:verse separator, so an isolated range like "(7-10)" in a translation footnote was rewritten to /7-10 and routed to Surah 7 even when the surrounding text referenced a different Surah. Restrict the chapter:verse separator to ":" so only conventional references (1:1, 1:1-3, 1:1-2:3) become links. Bare hyphen-separated numbers are left as plain text. Closes #3267 --- src/utils/string.test.ts | 11 +++++++---- src/utils/string.ts | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils/string.test.ts b/src/utils/string.test.ts index f21bd3c004..3f8e5a774c 100644 --- a/src/utils/string.test.ts +++ b/src/utils/string.test.ts @@ -385,11 +385,14 @@ describe('Test formatVerseReferencesToLinks', () => { expect(formatVerseReferencesToLinks(input)).toEqual(expected); }); - // Additional test cases that match current behavior - it('should convert partial verse references', () => { + it('should not convert partial or hyphen-only references', () => { const input = 'See 1: and :1 and 1-1'; - const expected = 'See 1: and :1 and 1-1'; - expect(formatVerseReferencesToLinks(input)).toEqual(expected); + expect(formatVerseReferencesToLinks(input)).toEqual(input); + }); + + it('should not convert a hyphen-only numeric range inside parentheses', () => { + const input = 'See Surah As-Saffat, verses (7-10) for context'; + expect(formatVerseReferencesToLinks(input)).toEqual(input); }); it('should convert dates that look like verse references', () => { diff --git a/src/utils/string.ts b/src/utils/string.ts index 5eb7c9ea7b..5230cdbb4d 100644 --- a/src/utils/string.ts +++ b/src/utils/string.ts @@ -186,6 +186,8 @@ export const cleanTranscript = (text: string): string => { /** * Converts verse references in text to clickable links. * Example: "1:1" or "1:1-2" or "1:1-2:3" will be converted to HTML anchor tags. + * The chapter:verse separator must be ":"; a bare hyphen-only range like + * "7-10" is ambiguous and is left as plain text. * * @param {string} text - The text containing verse references * @returns {string} The text with verse references converted to links @@ -193,7 +195,7 @@ export const cleanTranscript = (text: string): string => { export const formatVerseReferencesToLinks = (text: string): string => { if (!text) return ''; return text.replace( - /(\d{1,3}[:-]\d{1,3}(?:-\d{1,3}(?::\d{1,3})?)?)(?![^<]*<\/a>)/g, + /(\d{1,3}:\d{1,3}(?:-\d{1,3}(?::\d{1,3})?)?)(?![^<]*<\/a>)/g, (match) => `${match}`, ); };