Last active
August 19, 2024 05:28
-
-
Save GitMurf/c0500d8bcef2a0f83c0b526cf07fc265 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(""); | |
%> |
Line 6 tries to move to position 9999
of the line which is beyond the end of you file. If you change the 9999
to the word Infinity
If you change the
9999
to the wordInfinity
It works. Thanks
Hello, I found this template recently and decided to add a regex to simply copy the block when it already has a reference. Leaving it here in case anyone find it useful.
I ended up following shabegom guide so I will leave it here as reference for those that are writing scripts directly on the template file. https://shbgm.ca/blog/obsidian/how-to-use-templater-js-scripts
function createAndCopyBlockRefId(tp){
const editor = app.workspace.activeLeaf.view.editor
const cursorLine = editor.getCursor().line
editor.setSelection({line: cursorLine, ch: 0}, {line: cursorLine, ch: Infinity})
const textBlock = tp.file.selection();
const regex = /\^[a-zA-Z0-9-]+(?!.*\^[a-zA-Z0-9]+)$/
const match = textBlock.match(regex);
const id = match ? match[0] : `^${createBlockHash()}` ;
const block = `![[${tp.file.title}#${id}]]`.split("\n").join("");
navigator.clipboard.writeText(block).then(text => text);
return textBlock + (match ? '' : `^${id}`.split("\n").join(""));
}
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;
}
module.exports = createAndCopyBlockRefId
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
QUICK DEMO: https://user-images.githubusercontent.com/64155612/115738150-a3214380-a341-11eb-9793-c874cb2ed2f6.mp4
TIP: Copying and pasting into Obsidian as Plain Text seems to work best
(ctrl/cmd + shift + v)