[fix] Fix the wrong logic that always do the replacement toward the whole text instead the rest part of the text.
This commit is contained in:
parent
6789f574b8
commit
215b259095
63
main.ts
63
main.ts
|
@ -147,21 +147,54 @@ function processLineBlock(source: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyFormatting(text: string): string {
|
function applyFormatting(text: string): string {
|
||||||
if (text.includes('**')) {
|
// Define the patterns and their replacements in order
|
||||||
// Replace **bold** with *bold* (with spaces)
|
const patterns = [
|
||||||
text = text.replace(/\*\*(.*?)\*\*/g, ' *$1* ');
|
{ regex: /\*\*(.*?)\*\*/, replacement: ' *$1* ' }, // bold
|
||||||
} else if (text.includes('*')) {
|
{ regex: /(?<!\*)\*(?!\*)(.*?)\*(?!\*)/, replacement: ' _$1_ ' }, // italic
|
||||||
// Replace *italic* with _italic_
|
{ regex: /~~(.*?)~~/, replacement: ' ~$1~ ' }, // strike
|
||||||
text = text.replace(/(?<!\*)\*(?!\*)(.*?)\*(?!\*)/g, ' _$1_ ');
|
{ regex: /==(.*?)==/, replacement: ' `$1` ' }, // emphasize
|
||||||
|
{ regex: /`(.*?)`/, replacement: ' {$1} ' }, // quote
|
||||||
|
];
|
||||||
|
|
||||||
|
let result = '';
|
||||||
|
let remainingText = text;
|
||||||
|
|
||||||
|
while (remainingText.length > 0) {
|
||||||
|
let earliestMatch: { pattern: any; match: RegExpExecArray; index: number } | null = null;
|
||||||
|
let earliestIndex = remainingText.length;
|
||||||
|
|
||||||
|
// Find the earliest match among the patterns
|
||||||
|
for (let pattern of patterns) {
|
||||||
|
pattern.regex.lastIndex = 0; // Reset regex index
|
||||||
|
let match = pattern.regex.exec(remainingText);
|
||||||
|
if (match && match.index < earliestIndex) {
|
||||||
|
earliestMatch = {
|
||||||
|
pattern: pattern,
|
||||||
|
match: match,
|
||||||
|
index: match.index,
|
||||||
|
};
|
||||||
|
earliestIndex = match.index;
|
||||||
}
|
}
|
||||||
// Replace ~~strike~~ with ~strike~
|
|
||||||
text = text.replace(/~~(.*?)~~/g, ' ~$1~ ');
|
|
||||||
if (text.includes('==')) {
|
|
||||||
// Replace ==emphasize== with `emphasize`
|
|
||||||
text = text.replace(/==(.*?)==/g, ' `$1` ');
|
|
||||||
} else if (text.includes('`')) {
|
|
||||||
// Replace `quote` with {quote}
|
|
||||||
text = text.replace(/`(.*?)`/g, ' {$1} ');
|
|
||||||
}
|
}
|
||||||
return text;
|
|
||||||
|
if (earliestMatch) {
|
||||||
|
// Append text before the match
|
||||||
|
result += remainingText.slice(0, earliestMatch.index);
|
||||||
|
|
||||||
|
// Apply the replacement
|
||||||
|
let replacedText = earliestMatch.match[0].replace(earliestMatch.pattern.regex, earliestMatch.pattern.replacement);
|
||||||
|
|
||||||
|
result += replacedText;
|
||||||
|
|
||||||
|
// Update remainingText
|
||||||
|
remainingText = remainingText.slice(earliestMatch.index + earliestMatch.match[0].length);
|
||||||
|
} else {
|
||||||
|
// No more matches, append the rest of the text
|
||||||
|
result += remainingText;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue