small changes
This commit is contained in:
@@ -182,6 +182,44 @@ function positionIsProtected(start, end, ranges) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Normalize link destinations that contain whitespace before Marked parses them. */
|
||||
function normalizeMarkdownLinkDestinations(markdown) {
|
||||
const lines = String(markdown || "").split("\n");
|
||||
const result = [];
|
||||
let fence = null;
|
||||
const linkPattern = /(!?\[[^\]\n]*\]\()([^\)\n]*)(\))/g;
|
||||
|
||||
for (const line of lines) {
|
||||
const fenceMatch = line.match(/^\s*(```+|~~~+)/);
|
||||
if (fenceMatch) {
|
||||
const marker = fenceMatch[1].charAt(0);
|
||||
fence = fence === null ? marker : (fence === marker ? null : fence);
|
||||
result.push(line);
|
||||
continue;
|
||||
}
|
||||
if (fence !== null || /^\s{4}/.test(line)) {
|
||||
result.push(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
const codeRanges = [];
|
||||
for (const match of line.matchAll(/`+[^`\n]*`+/g)) {
|
||||
codeRanges.push({ start: match.index, end: match.index + match[0].length });
|
||||
}
|
||||
result.push(line.replace(linkPattern, (match, before, body, after, offset) => {
|
||||
if (positionIsProtected(offset, offset + match.length, codeRanges)) return match;
|
||||
const trimmed = body.trim();
|
||||
if (!trimmed || trimmed.startsWith("<") || !/\s/.test(trimmed)) return match;
|
||||
|
||||
const titleMatch = trimmed.match(/^(.+?)(\s+(?:"[^"\n]*"|'[^'\n]*'|\([^\)\n]*\)))$/);
|
||||
const destination = titleMatch ? titleMatch[1] : trimmed;
|
||||
const title = titleMatch ? titleMatch[2] : "";
|
||||
return `${before}${encodeURI(destination)}${title}${after}`;
|
||||
}));
|
||||
}
|
||||
return result.join("\n");
|
||||
}
|
||||
|
||||
/** Prefix every unprotected CamelCase WikiWord with the literal escape marker. */
|
||||
function protectCamelCaseWikiWords(markdown) {
|
||||
const wikiWordPattern = /(?<![!\p{L}\p{N}._-])((?:\p{Lu}\p{Ll}+){2,})(?![\p{L}\p{N}_-])/gu;
|
||||
@@ -363,6 +401,7 @@ export {
|
||||
expandNamespacedMarkdownLinks,
|
||||
expandTodoMarkup,
|
||||
expandWikiMentions,
|
||||
normalizeMarkdownLinkDestinations,
|
||||
protectCamelCaseWikiWords,
|
||||
extractCmapEmbeds,
|
||||
restoreCmapEmbeds
|
||||
|
||||
Reference in New Issue
Block a user