Skip to content

Commit

Permalink
fix: fix regex convertor
Browse files Browse the repository at this point in the history
note: regex hell
  • Loading branch information
Mara-Li committed Jun 15, 2022
1 parent dbdbb47 commit ca0e8ff
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions mkdocsPublisher/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getReceiptFolder(file: TFile, settings:MkdocsPublicationSettings, metad
if (folderRoot.length > 0) {
folderRoot = folderRoot + "/";
}
if (frontmatter[settings.yamlFolderKey]) {
if (frontmatter && frontmatter[settings.yamlFolderKey]) {
const category = frontmatter[settings.yamlFolderKey]
let parentCatFolder = category.split('/').at(-1)
parentCatFolder = parentCatFolder.length === 0 ? category.split('/').at(-2) : parentCatFolder
Expand All @@ -122,7 +122,12 @@ function convertLinkCitation(fileContent: string, settings: MkdocsPublicationSet
let pathInGithub=linkedFile.linked.extension === 'md' ? getReceiptFolder(linkedFile.linked, settings, metadataCache) : getImageLinkOptions(linkedFile.linked, settings);
const sourcePath = getReceiptFolder(sourceFile, settings, metadataCache);
pathInGithub = createRelativePath(sourcePath, pathInGithub);
fileContent = fileContent.replace(linkedFile.linkFrom, pathInGithub)
const regexToReplace = new RegExp(`(\\[{2}.*${linkedFile.linkFrom}.*\\]{2})|(\\[.*\\]\\(.*${linkedFile.linkFrom}.*\\))`, 'g');
const matchedLink = fileContent.match(regexToReplace);
for (const link of matchedLink) {
const newLink = link.replace(linkedFile.linkFrom, pathInGithub);
fileContent = fileContent.replace(link, newLink);
}
}
return fileContent;
}
Expand Down

0 comments on commit ca0e8ff

Please sign in to comment.