Skip to content

Commit

Permalink
fix(textReplacer): replace text only if not in code blocks
Browse files Browse the repository at this point in the history
fix #228
  • Loading branch information
Mara-Li committed Oct 5, 2023
1 parent ba85493 commit f3fd6de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 27 additions & 2 deletions src/conversion/find_and_replace_text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GitHubPublisherSettings } from "../settings/interface";
import { escapeRegex } from "./links";

/**
* Convert a string to a regex object when the string is in the form of a regex (enclosed by /)
Expand Down Expand Up @@ -39,11 +40,35 @@ export default function findAndReplaceText(
const replaceWith = censor.replace;
if (toReplace.match(/^\/.+\/[gimy]*$/)) {
const regex = createRegexFromText(toReplace, censor.flags);
text = text.replace(regex, replaceWith);
text = replaceTextNotInCodeBlocks(text, regex, replaceWith);
} else {
text = text.replaceAll(toReplace, replaceWith);
text = replaceTextNotInCodeBlocks(text, toReplace, replaceWith);
}
}
}
return text;
}


export function replaceTextNotInCodeBlocks(text: string, toReplace: string | RegExp, replaceWith: string) {
let regexWithString: string ;
let regex: RegExp;
if (toReplace instanceof RegExp) {
regexWithString = "```[\\s\\S]*?```|`[^`]*`|" + toReplace.source;
regex = new RegExp(regexWithString, `g${toReplace.flags}`);
} else {
regexWithString = "```[\\s\\S]*?```|`[^`]*`|" + escapeRegex(toReplace);
regex = new RegExp(regexWithString, "g");
}
return text.replace(regex, (match) => {
if (match.match(/`[^`]*`/)) {
return match;
} else if (match.match(/```[\s\S]*?```/)) {
return match;
} else {
return match.replace(toReplace, replaceWith);
}
});
}


9 changes: 6 additions & 3 deletions src/conversion/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
} from "../settings/interface";
import {isAttachment, noTextConversion} from "../utils/data_validation_test";
import { createRelativePath } from "./file_path";
import { replaceTextNotInCodeBlocks } from "./find_and_replace_text";

/**
* Convert wikilinks to markdown
* Pretty cursed
* @param {string} fileContent the text to convert
* @param {FrontmatterConvert} conditionConvert the frontmatter settings
* @param {GitHubPublisherSettings} settings global settings
Expand Down Expand Up @@ -127,7 +129,7 @@ export function convertWikilinks(
) {
linkCreator = "";
}
fileContent = fileContent.replace(wikiMatch, linkCreator);
fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator);

} else if (!fileName.startsWith("http")) {
const altMatch = wikiMatch.match(/(\|).*(]])/);
Expand Down Expand Up @@ -168,7 +170,7 @@ export function convertWikilinks(
) {
linkCreator = "";
}
fileContent = fileContent.replace(wikiMatch, linkCreator);
fileContent = replaceTextNotInCodeBlocks(fileContent, wikiMatch, linkCreator);
}
}
}
Expand Down Expand Up @@ -291,7 +293,7 @@ export async function convertLinkCitation(
newLink = `[${altText}](${pathInGithub})`;
}
newLink = addAltText(newLink, linkedFile);
fileContent = fileContent.replace(link, newLink);
fileContent = replaceTextNotInCodeBlocks(fileContent, link, newLink);
}
}
}
Expand Down Expand Up @@ -324,3 +326,4 @@ export function creatorAltLink(
}
return match.split("/").at(-1) as string; //alt text based on filename for other files
}

0 comments on commit f3fd6de

Please sign in to comment.