diff --git a/src/conversion/find_and_replace_text.ts b/src/conversion/find_and_replace_text.ts index 486d0d92..638ca8e9 100644 --- a/src/conversion/find_and_replace_text.ts +++ b/src/conversion/find_and_replace_text.ts @@ -56,20 +56,25 @@ export default function findAndReplaceText( } -export function replaceTextNotInCodeBlocks(text: string, toReplace: string | RegExp, replaceWith: string) { +export function replaceTextNotInCodeBlocks(text: string, toReplace: string | RegExp, replaceWith: string, links?: boolean) { let regexWithString: string ; let regex: RegExp; + if (toReplace instanceof RegExp) { - regexWithString = "```[\\s\\S]*?```|`[^`]*`|" + toReplace.source; + regexWithString = "```[\\s\\S]*?```|`[^`]*`|"; + if (links) regexWithString += "\\\\?!?"; + regexWithString += toReplace.source; regex = new RegExp(regexWithString, `g${toReplace.flags}`); } else { - regexWithString = "```[\\s\\S]*?```|`[^`]*`|" + escapeRegex(toReplace); + regexWithString = "```[\\s\\S]*?```|`[^`]*`|\\\\?!?"; + if (links) regexWithString += "\\\\?!?"; + regexWithString += escapeRegex(toReplace); regex = new RegExp(regexWithString, "g"); } return text.replace(regex, (match) => { - if (match.match(/`[^`]*`/)) { + if (match.match(/`[^`]*`/) || match.match(/```[\s\S]*?```/)) { return match; - } else if (match.match(/```[\s\S]*?```/)) { + } else if (links && match.match(/^\\/)) { return match; } else { return match.replace(toReplace, replaceWith); diff --git a/src/conversion/links.ts b/src/conversion/links.ts index 0a567c8f..09717fd0 100644 --- a/src/conversion/links.ts +++ b/src/conversion/links.ts @@ -129,7 +129,7 @@ export function convertWikilinks( ) { linkCreator = ""; } - fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator); + fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator, true); } else if (!fileName.startsWith("http")) { const altMatch = wikiMatch.match(/(\|).*(]])/); @@ -170,7 +170,7 @@ export function convertWikilinks( ) { linkCreator = ""; } - fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator); + fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator, true); } } } @@ -293,7 +293,7 @@ export async function convertLinkCitation( newLink = `[${altText}](${pathInGithub})`; } newLink = addAltText(newLink, linkedFile); - fileContent = replaceTextNotInCodeBlocks(fileContent, link, newLink); + fileContent = replaceTextNotInCodeBlocks(fileContent, link, newLink, true); } } }