Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/utils/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="/1-1" target="_blank">1-1</a>';
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', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,16 @@ 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
*/
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) => `<a href="${`/${match}`}" target="_blank">${match}</a>`,
);
};
Expand Down