Last active
May 5, 2021 22:43
-
-
Save GitMurf/146740dabfeb4582d07f901473c87407 to your computer and use it in GitHub Desktop.
Pull a line to the active file from a specified file without having to open it.
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
<%* | |
//v1.2: For block refs / embeds need to add line breaks above and below otherwise block ref extends to lines around it | |
//"pull" = default: removes the line, "pull-copy" leaves line, "pull-embed" leaves embed, "pull-ref" adds block ref | |
const pullMode = "pull"; | |
const pullFromFile = await tp.system.suggester((item) => item.path, this.app.vault.getMarkdownFiles(), false); | |
if(pullFromFile) { | |
let cmEditorAct = this.app.workspace.activeLeaf.view.editor; | |
const curContent = await this.app.vault.read(pullFromFile); | |
const arrayEachLine = curContent.split('\n'); | |
let arrayStrings = []; | |
let arrayValues = []; | |
let charPos = 0; | |
arrayEachLine.forEach(eachItem => { | |
if(eachItem != '') { | |
let charCtr = eachItem.length; | |
let linePos = [charPos, (charPos + charCtr)]; | |
arrayStrings.push(eachItem.slice(0,250)); | |
arrayValues.push(linePos); | |
charPos = charPos + charCtr; | |
} | |
charPos++; | |
}); | |
let selectLine = await tp.system.suggester(arrayStrings, arrayValues, false); | |
if(pullMode == "pull") { | |
let newContents = curContent.substring(0, selectLine[0]) + curContent.substring(selectLine[1] + 1); | |
await this.app.vault.modify(pullFromFile, newContents); | |
} else if(pullMode == "pull-embed" || pullMode == "pull-ref") { | |
function createBlockHash() { | |
let result = ''; | |
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < 7; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
let id = createBlockHash(); | |
let blockRef; | |
let newContents; | |
if(pullMode == "pull-embed") { | |
blockRef = `![[${tp.file.title}#^${id}]]`; | |
newContents = curContent.substring(0, selectLine[0]) + blockRef + '\n' + curContent.substring(selectLine[1] + 1); | |
tR = '\n' + curContent.substring(selectLine[0], selectLine[1]) + ` ^${id}` + '\n'; | |
} else if(pullMode == "pull-ref") { | |
blockRef = `![[${pullFromFile.basename}#^${id}]]`; | |
newContents = curContent.substring(0, selectLine[0]) + '\n' + curContent.substring(selectLine[0], selectLine[1]) + ` ^${id}` + '\n\n' + curContent.substring(selectLine[1] + 1); | |
tR = blockRef; | |
} | |
await this.app.vault.modify(pullFromFile, newContents); | |
} | |
if(pullMode == "pull" || pullMode == "pull-copy") { | |
tR = curContent.substring(selectLine[0], selectLine[1]); | |
} | |
} | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment