Skip to content

Instantly share code, notes, and snippets.

@GitMurf
Last active August 13, 2021 05:12
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/fc58fe8e8e531d19fe4f51e25aba856b to your computer and use it in GitHub Desktop.
Save GitMurf/fc58fe8e8e531d19fe4f51e25aba856b to your computer and use it in GitHub Desktop.
<%*
//v0.3 - Allow for Include and Exclude variables to have multiple criteria -- Define Qty of results
//VARIABLES TO SET
//How many results to return
const howMany = 1;
//Include the "#" for tags and brackets "[" for pages
const pageOrTag = "#quote";
//The Including and Excluding variables are arrays []. To add multiple values use format: ["one","two"]
//This is a plain text filter of the resulting blocks
const including = [""];
//This is a plain text EXCLUDE filter of the resulting blocks
const excluding = [""];
//Alias to use for the page link (leave blank "" to show the link normally)
const alias = "Page Source";
//END OF VARIABLES
const files = await app.vault.getMarkdownFiles();
const matchedBlocks = [];
for (const eachFile of files) {
const mdCache = app.metadataCache.getFileCache(eachFile);
const links = mdCache.links;
const tags = mdCache.tags;
if(links || tags) {
const fileCont = await app.vault.cachedRead(eachFile);
if(links) {
for (const eachLink of links) {
if(eachLink.displayText === pageOrTag || eachLink.link === pageOrTag || eachLink.original === pageOrTag) {
const lineNum = eachLink.position.start.line;
const blockCont = fileCont.split('\n')[lineNum];
matchedBlocks.push([blockCont,eachFile]);
}
}
}
if(tags) {
for (const eachTag of tags) {
//if(eachTag.tag === pageOrTag) {
if(eachTag.tag.startsWith(pageOrTag)) {
const lineNum = eachTag.position.start.line;
const blockCont = fileCont.split('\n')[lineNum];
matchedBlocks.push([blockCont,eachFile]);
}
}
}
}
}
let filteredBlocks = matchedBlocks.filter(eachBlock => !eachBlock[0].includes('#random'));
filteredBlocks = filteredBlocks.filter(eachBlock => !eachBlock[0].includes('const pageOrTag ='));
filteredBlocks = filteredBlocks.filter(eachBlock => !eachBlock[0].includes('const including ='));
filteredBlocks = filteredBlocks.filter(eachBlock => !eachBlock[0].includes('const excluding ='));
if(including[0] != "") {
including.forEach(eachInclude => {
filteredBlocks = filteredBlocks.filter(eachBlock => eachBlock[0].includes(eachInclude));
});
}
if(excluding[0] != "") {
excluding.forEach(eachExclude => {
filteredBlocks = filteredBlocks.filter(eachBlock => !eachBlock[0].includes(eachExclude));
});
}
let finalResult = "";
if(filteredBlocks.length > 0) {
for (let ctr = 1; ctr <= howMany; ctr++) {
if(filteredBlocks.length > 0) {
let foundRandomArr = filteredBlocks[Math.floor(Math.random() * filteredBlocks.length)];
let foundRandomOrig = foundRandomArr[0];
let foundRandom = foundRandomArr[0];
foundRandom = foundRandom.replace(/^#+ /,'');
foundRandom = foundRandom.replace(/^ *[-*] \[ \] /,'');
foundRandom = foundRandom.replace(/^ *[-*] \[x\] /,'');
foundRandom = foundRandom.replace(/^ *[-*] /,'').trim();
let sourceLink = "";
if(alias === "") {
sourceLink = ' - [' + `[${foundRandomArr[1].basename}]]`;
} else {
sourceLink = ' - [' + `[${foundRandomArr[1].basename}|${alias}]]`;
}
foundRandom = foundRandom + ' - #random' + sourceLink;
if(ctr > 1){foundRandom = '\n\n' + foundRandom};
finalResult += foundRandom;
filteredBlocks = filteredBlocks.filter(eachBlock => !eachBlock[0].includes(foundRandomOrig));
}
}
} else {
finalResult = 'No results for your Random Block search!';
}
%><% finalResult %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment