Forked from GitMurf/obsidian.templater.create-and-copy-block-ref-id.js
Created
August 22, 2022 19:06
-
-
Save serpro69/6c4b1e3998cbef0991787c273406a22d to your computer and use it in GitHub Desktop.
Credit to: @shabegom - Add a block ref to the current line by adding a ^uniqueId on the end and copying to the clipboard the embed reference link so you can quickly paste the block ref somewhere else in your vault. Similar to Roam ctrl + click and drag to create block ref.
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.1: Changed to using the new "view.editor" instead of deprecated "view.sourceMode.cmEditor" | |
let cmEditorAct = this.app.workspace.activeLeaf.view.editor; | |
let curLine = cmEditorAct.getCursor().line; | |
cmEditorAct.setSelection({ line: curLine, ch: 0 }, { line: curLine, ch: 9999 }); | |
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 block = `![[${tp.file.title}#^${id}]]`.split("\n").join(""); | |
navigator.clipboard.writeText(block).then(text => text); | |
tR = tp.file.selection() + ` ^${id}`.split("\n").join(""); | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment