Function for Obsidian that returns the block link and embed of a task passed as a dataview task object, for example from the nextActions.js function. Generates random block IDs, if the task doesn't have one. You can also specify a custom block ID, instead of generating a random one.
Last active
February 28, 2025 13:39
-
-
Save FeralFlora/7c2efae833d4d5e2244edc4a61e236f8 to your computer and use it in GitHub Desktop.
Function for Obsidian that returns the block link and embed of a task passed as a dataview task object
This file contains 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
async function processFile(file, line, blockID) { | |
const cachedData = await app.vault.cachedRead(file); | |
return app.vault.process(file, (data) => { | |
if (cachedData !== data) { | |
data = cachedData; | |
} | |
let lines = data.split("\n"); | |
lines[line] += ` ^${blockID}`; | |
data = lines.join("\n"); | |
return data; | |
}); | |
} | |
function generateBlockID() { | |
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
let blockID = ""; | |
for (let i = 0; i < 6; i++) { | |
blockID += alphabet.charAt(Math.floor(Math.random() * alphabet.length)); | |
} | |
return blockID; | |
} | |
async function generateBlockEmbed(tp, item, customBlockID = null) { | |
let blockID = ""; | |
const line = item.line || item.position.line; | |
const fileName = item.file.name; | |
const file = tp.file.find_tfile(fileName); | |
if (item.blockId) { | |
blockID = item.blockId; | |
} else { | |
blockID = customBlockID || generateBlockID(); | |
} | |
await processFile(file, line, blockID); | |
const blockLink = `[[${fileName}#^${blockID}]]`; | |
const blockEmbed = `![[${fileName}#^${blockID}]]`; | |
return { blockLink, blockEmbed }; | |
} | |
module.exports = generateBlockEmbed; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment