Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitMurf/f027ac2ada61d03007b973569e507627 to your computer and use it in GitHub Desktop.
Save GitMurf/f027ac2ada61d03007b973569e507627 to your computer and use it in GitHub Desktop.
Creates block ref on current line and copies to clipboard as asterisk alias link to be pasted somewhere else
<%*
const curSelection = tp.file.selection();
const cmEditorAct = app.workspace.activeLeaf.view.editor;
const curLineNum = cmEditorAct.getCursor().line;
const curLine = cmEditorAct.getLine(curLineNum);
cmEditorAct.setSelection({ line: curLineNum, ch: 0 }, { line: curLineNum, ch: 9999 });
const blockRef = curLine.match(/ \^(.*)/);
let id;
if (!blockRef) {
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;
}
id = createBlockHash();
tR = tp.file.selection() + ` ^${id}`.split("\n").join("");
} else {
id = blockRef[1];
tR = tp.file.selection();
}
const textAlias = (curSelection ? curSelection : `*`);
const block = `[[${tp.file.title}#^${id}|${textAlias}]]`.split("\n").join("");
navigator.clipboard.writeText(block).then(text => text);
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment