Skip to content

Commit

Permalink
Better handling of wikilink-like text that is not a wikilink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Apr 29, 2022
1 parent 66bf8a2 commit cccf71d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class DigitalGarden extends Plugin {
publishModal: PublishModal;

async onload() {
this.appVersion = "2.9.2";
this.appVersion = "2.9.3";

console.log("Initializing DigitalGarden plugin v" + this.appVersion);
await this.loadSettings();
Expand Down Expand Up @@ -78,6 +78,7 @@ export default class DigitalGarden extends Plugin {
return;
}

new Notice("Publishing note...");
const publisher = new Publisher(vault, metadataCache, this.settings);
const publishSuccessful = await publisher.publish(currentFile);

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "digitalgarden",
"name": "Digital Garden",
"version": "2.9.2",
"version": "2.9.3",
"minAppVersion": "0.12.0",
"description": "Publish your notes to a digital garden for others to enjoy.",
"author": "Ole Eskild Steensen",
Expand Down
43 changes: 37 additions & 6 deletions src/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,42 @@ export default class Publisher {

async convertLinksToFullPath(text: string, filePath: string): Promise<string> {
let convertedText = text;
const links = this.metadataCache.getCache(filePath).links;
if (links && links.length) {
for (const link of links) {

const linkedFileRegex = /\[\[(.*?)\]\]/g;
const linkedFileMatches = text.match(linkedFileRegex);

const codeFenceRegex = /`(.*?)`/g;
const codeFences = text.match(codeFenceRegex);

const codeBlockRegex = /```.*?\n[\s\S]+?```/g;
const codeBlocks = text.match(codeBlockRegex);

const excaliDrawRegex = /:\[\[(\d*?,\d*?)\],.*?\]\]/g;
const excalidrawings = text.match(excaliDrawRegex);

if (linkedFileMatches) {
for (const linkMatch of linkedFileMatches) {
try {
const textInsideBrackets = link.original.substring(link.original.indexOf('[') + 2, link.original.indexOf(']'));
const insideCodeBlockIndex = codeBlocks ? codeBlocks.findIndex(codeBlock => codeBlock.includes(linkMatch)) : -1;
if(insideCodeBlockIndex>-1) {
codeBlocks.splice(insideCodeBlockIndex, 1);
continue;
}

const insideCodeFenceIndex = codeFences ? codeFences.findIndex(codeFence => codeFence.includes(linkMatch)) : -1;
if(insideCodeFenceIndex>-1) {
codeFences.splice(insideCodeFenceIndex, 1);
continue;
}

const excalidrawIndex = excalidrawings ? excalidrawings.findIndex(excalidraw => excalidraw.includes(linkMatch)) : -1;
if(excalidrawIndex>-1) {
excalidrawings.splice(excalidrawIndex, 1);
continue;
}


const textInsideBrackets = linkMatch.substring(linkMatch.indexOf('[') + 2,linkMatch.lastIndexOf(']')-1);
let [linkedFileName, prettyName] = textInsideBrackets.split("|");

prettyName = prettyName || linkedFileName;
Expand All @@ -274,11 +305,11 @@ export default class Publisher {
const fullLinkedFilePath = getLinkpath(linkedFileName);
const linkedFile = this.metadataCache.getFirstLinkpathDest(fullLinkedFilePath, filePath);
if(!linkedFile){
convertedText = convertedText.replace(link.original, `[[${linkedFileName}${headerPath}|${prettyName}]]`);
convertedText = convertedText.replace(linkMatch, `[[${linkedFileName}${headerPath}|${prettyName}]]`);
}
if (linkedFile?.extension === "md") {
const extensionlessPath = linkedFile.path.substring(0, linkedFile.path.lastIndexOf('.'));
convertedText = convertedText.replace(link.original, `[[${extensionlessPath}${headerPath}|${prettyName}]]`);
convertedText = convertedText.replace(linkMatch, `[[${extensionlessPath}${headerPath}|${prettyName}]]`);
}
} catch(e){
console.log(e);
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"2.9.3": "0.12.0",
"2.9.2": "0.12.0",
"2.9.1": "0.12.0",
"2.9.0": "0.12.0",
Expand Down

0 comments on commit cccf71d

Please sign in to comment.