fix: patch tidyMarkdown

This commit is contained in:
Yanlong Wang 2024-05-09 11:06:20 +08:00
parent de22127d2f
commit 4bee36ed4a
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -1,39 +1,37 @@
export function tidyMarkdown(markdown: string): string { export function tidyMarkdown(markdown: string): string {
const lines = markdown.split('\n');
const processedLines = lines.map((line) => {
// Remove leading spaces from each line
line = line.trimStart();
// Step 1: Handle complex broken links with text and optional images spread across multiple lines // Handle complex broken links with text and optional images
let normalizedMarkdown = markdown.replace(/\[\s*([^\]\n]+?)\s*\]\s*\(\s*([^)]+)\s*\)/g, (match, text, url) => { line = line.replace(/\[\s*([^\]\n!]*?)\s*(?:!\[([^\]]*)\]\((.*?)\))?\s*\]\s*\(\s*([^)\n]+)\s*\)/g, (match, text, alt, imgUrl, linkUrl) => {
// Remove internal new lines and excessive spaces within the text text = text.replace(/\s+/g, ' ').trim();
text = text.replace(/\s+/g, ' ').trim(); alt = alt ? alt.replace(/\s+/g, ' ').trim() : '';
url = url.replace(/\s+/g, '').trim(); imgUrl = imgUrl ? imgUrl.replace(/\s+/g, '').trim() : '';
return `[${text}](${url})`; linkUrl = linkUrl.replace(/\s+/g, '').trim();
if (imgUrl) {
return `[${text} ![${alt}](${imgUrl})](${linkUrl})`;
} else {
return `[${text}](${linkUrl})`;
}
});
// Normalize regular links that may be broken across lines
line = line.replace(/\[\s*([^\]\n]+)\]\s*\(\s*([^)\n]+)\s*\)/g, (match, text, url) => {
text = text.replace(/\s+/g, ' ').trim();
url = url.replace(/\s+/g, '').trim();
return `[${text}](${url})`;
});
return line;
}); });
normalizedMarkdown = normalizedMarkdown.replace(/\[\s*([^\]\n!]*?)\s*\n*(?:!\[([^\]]*)\]\((.*?)\))?\s*\n*\]\s*\(\s*([^)]+)\s*\)/g, (match, text, alt, imgUrl, linkUrl) => { // Join the processed lines back together
// Normalize by removing excessive spaces and new lines let normalizedMarkdown = processedLines.join('\n');
text = text.replace(/\s+/g, ' ').trim();
alt = alt ? alt.replace(/\s+/g, ' ').trim() : '';
imgUrl = imgUrl ? imgUrl.replace(/\s+/g, '').trim() : '';
linkUrl = linkUrl.replace(/\s+/g, '').trim();
if (imgUrl) {
return `[${text} ![${alt}](${imgUrl})](${linkUrl})`;
} else {
return `[${text}](${linkUrl})`;
}
});
// Step 2: Normalize regular links that may be broken across lines // Replace more than two consecutive empty lines with exactly two empty lines
normalizedMarkdown = normalizedMarkdown.replace(/\[\s*([^\]]+)\]\s*\(\s*([^)]+)\)/g, (match, text, url) => {
text = text.replace(/\s+/g, ' ').trim();
url = url.replace(/\s+/g, '').trim();
return `[${text}](${url})`;
});
// Step 3: Replace more than two consecutive empty lines with exactly two empty lines
normalizedMarkdown = normalizedMarkdown.replace(/\n{3,}/g, '\n\n'); normalizedMarkdown = normalizedMarkdown.replace(/\n{3,}/g, '\n\n');
// Step 4: Remove leading spaces from each line
normalizedMarkdown = normalizedMarkdown.replace(/^[ \t]+/gm, '');
return normalizedMarkdown.trim(); return normalizedMarkdown.trim();
} }