Skip to content

Commit

Permalink
Updates mind map internal link rewriter to work with MD links as well…
Browse files Browse the repository at this point in the history
… as wikilinks
  • Loading branch information
lynchjames committed Dec 8, 2020
1 parent d76b7e1 commit 2988e1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const MM_VIEW_TYPE = 'mindmap';
export const MD_VIEW_TYPE = 'markdown';
export const INTERNAL_LINK_REGEX = /\[\[(.*)\]\]/g;

// https://regex101.com/r/gw85cc/2
export const INTERNAL_LINK_REGEX = /\[\[(?<wikitext>.*)\]\]|<a href="(?<mdpath>.*)">(?<mdtext>.*)<\/a>/gim;
18 changes: 13 additions & 5 deletions src/obsidian-markmap-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ export default class ObsidianMarkmap {

private replaceInternalLinks(node: INode){
const matches = this.parseValue(node.v);
matches.forEach(match => {
const linkText = match[1];
const url = `obsidian://vault/${this.vaultName}/${encodeURI(getLinkpath(linkText))}`;
console.log(node.v);
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const isWikiLink = match.groups['wikitext'];
const linkText = isWikiLink ? match.groups['wikitext'] : match.groups['mdtext'];
const linkPath = isWikiLink ? linkText : match.groups['mdpath'];
if(linkPath.startsWith('http')){
continue;
}
const url = `obsidian://open?vault=${this.vaultName}&file=${isWikiLink ? encodeURI(getLinkpath(linkPath)) : linkPath}`;
const link = `<a href=\"${url}\">${linkText}</a>`;
node.v = node.v.replace(/\[\[.*\]\]/, link);
});
console.log(match);
node.v = node.v.replace(match[0], link);
}
}

private parseValue(v: string) {
Expand Down

0 comments on commit 2988e1f

Please sign in to comment.