Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GitMurf/f23bb1810f025258431a9c3df518ab26 to your computer and use it in GitHub Desktop.
Save GitMurf/f23bb1810f025258431a9c3df518ab26 to your computer and use it in GitHub Desktop.
<%*
let startDate = await tp.system.prompt('Start Date');
let endDate = await tp.system.prompt('End Date');
const nlDatesPlugin = app.plugins.getPlugin('nldates-obsidian');
function getNldDate(nlDatesPlugin, dateString) {
let parseResultLink = null;
let parseResult = nlDatesPlugin.parseDate(dateString);
if(parseResult){ parseResultLink = parseResult.formattedString };
if(parseResultLink === "Invalid date") { parseResultLink = null }
if(parseResultLink) {
return parseResult.date;
} else {
return null;
}
}
function getDayRegex(dayStart, dayEnd) {
let dayRegex = "";
if(dayStart < 10 && dayEnd < 10) {
dayRegex = `0[${dayStart}-${dayEnd}]`;
} else if(dayStart < 10 && dayEnd < 20) {
dayRegex = `(0[${dayStart}-9]|1[0-${dayEnd - 10}])`;
} else if(dayStart < 10 && dayEnd < 30) {
dayRegex = `(0[${dayStart}-9]|1[0-9]|2[0-${dayEnd - 20}])`;
} else if(dayStart < 10 && dayEnd >= 30) {
dayRegex = `(0[${dayStart}-9]|1[0-9]|2[0-9]|3[0-${dayEnd - 30}])`;
} else if(dayStart < 20 && dayEnd < 20) {
dayRegex = `(1[${dayStart - 10}-${dayEnd - 10}])`;
} else if(dayStart < 20 && dayEnd < 30) {
dayRegex = `(1[${dayStart - 10}-9]|2[0-${dayEnd - 20}])`;
} else if(dayStart < 20 && dayEnd >= 30) {
dayRegex = `(1[${dayStart - 10}-9]|2[0-9]|3[0-${dayEnd - 30}])`;
} else if(dayStart < 30 && dayEnd < 30) {
dayRegex = `(2[${dayStart - 20}-${dayEnd - 20}])`;
} else if(dayStart < 20 && dayEnd >= 30) {
dayRegex = `(2[${dayStart - 20}-9]|3[0-${dayEnd - 30}])`;
} else if(dayStart >= 30 && dayEnd >= 30) {
dayRegex = `(3[${dayStart - 30}-${dayEnd - 30}])`;
}
return dayRegex;
}
function getMonthDayRegex(dayStart, dayEnd, monthStart, monthEnd, yearStart, yearEnd) {
let dayRegex = "";
let dateFilterRegex = "";
if(monthStart === monthEnd) {
let dayRegex = getDayRegex(dayStart, dayEnd);
dateFilterRegex = `file:(/${yearStart}-0?${monthStart}-${dayRegex}/)`;
} else if(monthStart < 10 && monthEnd < 10) {
let monthDayRegex = `(0${monthStart}-${getDayRegex(dayStart, 31)}`;
switch(monthEnd - monthStart) {
case 1:
break;
case 2:
monthDayRegex = `${monthDayRegex}|0${monthStart+1}-${getDayRegex(1, 31)}`;
break;
default:
monthDayRegex = `${monthDayRegex}|0[${monthStart+1}-${monthEnd-1}]-${getDayRegex(1, 31)}`;
break;
}
monthDayRegex = `${monthDayRegex}|0${monthEnd}-${getDayRegex(1, dayEnd)})`;
dateFilterRegex = `file:(/${yearStart}-${monthDayRegex}/)`;
} else if(monthStart < 10 && monthEnd >= 10) {
let monthDayRegex = `(0${monthStart}-${getDayRegex(dayStart, 31)}`;
switch(monthEnd - monthStart) {
case 1:
break;
case 2:
monthDayRegex = `${monthDayRegex}|0?${monthStart+1}-${getDayRegex(1, 31)}`;
break;
default:
if(monthStart + 1 < 10) {
monthDayRegex = `${monthDayRegex}|0[${monthStart+1}-9]-${getDayRegex(1, 31)}`;
if(monthEnd - 1 >= 10) { monthDayRegex = `${monthDayRegex}|1[0-${monthEnd-1-10}]-${getDayRegex(1, 31)}`; }
} else {
monthDayRegex = `${monthDayRegex}|1[${monthStart+1-10}-${monthEnd-1-10}]-${getDayRegex(1, 31)}`;
}
break;
}
monthDayRegex = `${monthDayRegex}|${monthEnd}-${getDayRegex(1, dayEnd)})`;
dateFilterRegex = `file:(/${yearStart}-${monthDayRegex}/)`;
} else if(monthStart >= 10 && monthEnd >= 10) {
let monthDayRegex = `(${monthStart}-${getDayRegex(dayStart, 31)}`;
switch(monthEnd - monthStart) {
case 1:
break;
case 2:
monthDayRegex = `${monthDayRegex}|11-${getDayRegex(1, 31)}`;
break;
}
monthDayRegex = `${monthDayRegex}|${monthEnd}-${getDayRegex(1, dayEnd)})`;
dateFilterRegex = `file:(/${yearStart}-${monthDayRegex}/)`;
}
return dateFilterRegex;
}
let momStart = null;
let momEnd = null;
if(startDate && endDate) {
startNld = getNldDate(nlDatesPlugin, startDate);
if(startNld) {
momStart = window.moment(startNld);
} else {
new Notice(`Start Date: "${startDate}" - Invalid date entered. Script exited.`, 20000);
return;
}
endNld = getNldDate(nlDatesPlugin, endDate);
if(endNld) {
momEnd = window.moment(endNld);
} else {
new Notice(`End Date: "${endDate}" - Invalid date entered. Script exited.`, 20000);
return;
}
} else {
new Notice("You did not provide both a start and end date. Script exited.", 20000);
return;
}
if(momStart === null || momEnd === null) {
new Notice("Something went wrong with the date conversions. Script exited.", 20000);
return;
}
const yearStart = momStart.year();
const monthStart = momStart.month() + 1;
const dayStart = momStart.date();
const yearEnd = momEnd.year();
const monthEnd = momEnd.month() + 1;
const dayEnd = momEnd.date();
let dateFilterRegex = "";
if(yearStart !== yearEnd) {
if(yearEnd - yearStart === 1) {
dateFilterRegex = getMonthDayRegex(dayStart, 31, monthStart, 12, yearStart, yearStart);
dateFilterRegex = `(${dateFilterRegex} OR ${getMonthDayRegex(1, dayEnd, 1, monthEnd, yearEnd, yearEnd)})`;
} else {
new Notice("Cannot calculate dates more than 2 years different. Script exited.", 20000);
return;
}
} else {
if(monthStart > monthEnd) {
new Notice("Start month cannot be AFTER the end month. Script exited.", 20000);
return;
}
if(monthStart === monthEnd && dayStart > dayEnd) {
new Notice("Start day cannot be AFTER the end day. Script exited.", 20000);
return;
}
dateFilterRegex = getMonthDayRegex(dayStart, dayEnd, monthStart, monthEnd, yearStart, yearEnd);
}
const search = app.workspace.getLeavesOfType("search")[0];
let keyword = await tp.system.prompt('Keyword', '[' + '[Journal');
keyword = keyword.replace(/(\[|\])/g, "\\$1");
const searchQuery = `${dateFilterRegex} (section:(/^#+ ${keyword}.*\\n+\\S[\\s\\S]*[^\\n]/) OR section:(/^#+ ${keyword}.*/ section:(/[\\s\\S]*[^\\n]/)))`;
//Collapse=true, Expand=false
search.view.setCollapseAll(false);
//Show Extra Context: true or false
search.view.setExtraContext(false);
//SortOrder Options: alphabeticalReverse, alphabetical, byModifiedTime, byModifiedTimeReverse, byCreatedTime, byCreatedTimeReverse
search.view.setSortOrder("byCreatedTime");
search.view.setQuery(searchQuery);
app.workspace.revealLeaf(search);
return;
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment