Last active
April 16, 2022 05:47
-
-
Save GitMurf/908325fb863658a91ebe7fc09f4c2148 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%* | |
| const addPageBrackets = false; //true = '[[page]]' ... false = 'page' | |
| const files = await app.vault.getMarkdownFiles(); | |
| const links = []; | |
| const finalLinkArr = []; | |
| files.forEach(file => { | |
| links.push(file.basename); | |
| finalLinkArr.push('๐ ' + file.basename); | |
| }); | |
| const unResLinks = Object.entries(app.metadataCache.unresolvedLinks); | |
| unResLinks.forEach((file) => { | |
| Object.entries(file[1]).forEach(eachItem => { | |
| if(!links.includes(eachItem[0])) { | |
| links.push(eachItem[0]); | |
| finalLinkArr.push('๐ ' + eachItem[0]); | |
| } | |
| }); | |
| }); | |
| let selection = await tp.system.suggester(finalLinkArr, links, false, 'Page Name to Link', 40); | |
| const pageLink = `[[${selection}]]`; | |
| if(addPageBrackets) { selection = pageLink } | |
| const cmEditorAct = app.workspace.activeLeaf.view.editor; | |
| const curLineNum = cmEditorAct.getCursor().line; | |
| const curLine = cmEditorAct.getLine(curLineNum); | |
| const curLineReg = curLine.replace(/(\[|\]|\^|\*|\||\(|\)|\.|\:)/g, '\\$1') | |
| const thisFile = app.workspace.getActiveFile(); | |
| const thisContent = await app.vault.cachedRead(thisFile); | |
| let newLineStr; | |
| if(curLine.contains(': [')) { | |
| if(curLine.contains("', ]")) { | |
| newLineStr = curLine.replace("', ]", '') + "', '" + selection + "']"; | |
| } else { | |
| newLineStr = curLine.replace(/\]$/, '') + ", '" + selection + "']"; | |
| } | |
| } else { | |
| newLineStr = `${curLine}['${selection}']`; | |
| } | |
| const regexp = new RegExp(`^(\\-\\-\\-(.|\\r|\\n)*?)(${curLineReg})((.|\\r|\\n)*?\\-\\-\\-)(\\n)?(%% YAML_LINKS:(( )?(.*)?)\\n)?`); | |
| let yamlContent = thisContent.replace(regexp, `$1${newLineStr}$4\n%% YAML_LINKS: ${pageLink}$8\n`); | |
| yamlContent = yamlContent.replace(/\n(\%\%.*\])\n/,'\n$1 %%\n'); | |
| //tR = `'${selection}'` | |
| await app.vault.modify(thisFile, yamlContent); | |
| %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment