Action
Add Backlinks
Posted by corkstu,
Last update
10 months ago
Action to find all the drafts with wikilinks referring to this one (i.e. backlinks) and list wikilinks to those drafts under a “Backlinks” heading. This action can be run again to update the backlinks when required.
Steps
-
script
let title = draft.displayTitle; if (title.length == 0) { app.displayErrorMessage("This Draft has no title!"); } // query for other drafts with the link to this one... let pattern = `"[[${title}]]"`; let drafts = Draft.query(pattern, "all", [], [], "modified", true); // didn't find any! if (drafts.length == 0) { app.displayInfoMessage("No backlinked drafts found."); context.cancel(); }else{ let backlinks = []; for(let draft of drafts){ //Add with a "d:" otherwise the inserted backlink matches as a wikilink and around the circle we go... backlinks.push("[[d:" + draft.displayTitle + "]]"); } let result = draft.content.match(/((#+\sBacklinks)[\s\S]\s*(\[\[.*?\]\][\s\S])*)/); if(result == null){ draft.content = draft.content + "\n## Backlinks\n\n" + backlinks.join("\n"); draft.update(); }else{ let tobereplaced = result[1]; let blTitle = result[2]; draft.content = draft.content.replace(tobereplaced,blTitle + "\n\n" + backlinks.join("\n") + "\n"); draft.update(); } }
Options
-
After Success Nothing Notification None Log Level None
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.