improve(modal): improve to hex modal by creating a pre-process function

On branch development
 Changes to be committed:
	modified:   src/services/citationService.ts
This commit is contained in:
mpstaton 2025-07-08 02:39:49 +03:00
parent 9f6cbfd4c0
commit cf09cfa2c1

View file

@ -107,6 +107,26 @@ export class CitationService {
return Array.from(groups.values());
}
/**
* Preprocess content to clean up URL links after citations
* This handles cases like [1][text](url) or [1](url) and removes the URL part
*/
private preprocessCitations(content: string): string {
// First pass: Handle [number][text](url) pattern
let result = content.replace(
/\[(\d+)\]\s*\[[^\]]+\]\([^)]+\)/g,
'[$1]' // Keep just the [number] part
);
// Second pass: Handle [number](url) pattern
result = result.replace(
/\[(\d+)\]\([^)]+\)/g,
'[$1]' // Keep just the [number] part
);
return result;
}
/**
* Convert a specific citation to hex format
*/
@ -116,16 +136,18 @@ export class CitationService {
hexId?: string,
matchIndex: number = -1
): ConversionResult {
const groups = this.findCitations(content);
// First preprocess the content to clean up any URL links
const preprocessedContent = this.preprocessCitations(content);
const groups = this.findCitations(preprocessedContent);
const group = groups.find(g => g.number === citationNumber);
if (!group) {
return { content, changed: false, stats: { citationsConverted: 0 } };
return { content: preprocessedContent, changed: false, stats: { citationsConverted: 0 } };
}
// Generate or use provided hex ID
const targetHexId = hexId || this.generateHexId();
let updatedContent = content;
let updatedContent = preprocessedContent;
let citationsConverted = 0;
const url = group.matches[0]?.url;
@ -175,6 +197,8 @@ export class CitationService {
}
}
// No need for post-processing since we handle URLs in preprocess step
return {
content: updatedContent,
changed: citationsConverted > 0,