<%*
/*
Prepares Obsidian vault for publishing by:
1. Converting Dataview tables to static Markdown tables
2. Inserting `publish: true/false` in YAML frontmatter based on array config
*/
const dv = app.plugins.plugins["dataview"].api;
const openPublishPanel = app.commands.commands["publish:view-changes"].callback;
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
function getDatesInRange(startDateStr, currentDateStr) {
const startDate = new Date(startDateStr);
const currentDate = new Date(currentDateStr);
const datesArray = [];
while (startDate <= currentDate) {
datesArray.push(formatDate(startDate));
startDate.setDate(startDate.getDate() + 1);
}
const today = new Date();
return [...datesArray, formatDate(startDate), formatDate(currentDate), formatDate(today)];
}
// [ path, query, publishFlag ]
const fileAndQuery = [
[
"tables/achievements",
'TABLE title AS "Title", date AS "Date", description AS "Description" FROM "" WHERE contains(tags, "achievement")',
true
],
[
"tables/firehose",
`TABLE file.ctime AS "Date"
FROM ""
WHERE
publish = true
SORT file.mtime DESC`,
true
],
[
"learning/quickies/_index",
`TABLE file.ctime AS Date FROM "learning/quickies"`,
true
],
[
"tables/dashboard",
`table completed
from ""
where completed = false
and !contains(file.path, "templates/")
`,
false
]
];
for (const [filename, query, publishFlag] of fileAndQuery) {
const tFile = tp.file.find_tfile(filename) ?? await tp.file.create_new("", filename);
if (!tFile) {
new Notice(`Failed to create or find: ${filename}`);
continue;
}
const queryOutput = await dv.queryMarkdown(query);
const fileContent = `---\npublish: ${publishFlag}\ncompleted: true
\n---\n%% #Ignore update via "Update Publish Files" template %%\n\n${queryOutput.value}`;
try {
await app.vault.modify(tFile, fileContent);
new Notice(`✅ Updated ${tFile.basename} (publish: ${publishFlag})`);
} catch (error) {
console.error("Error updating file", filename, error);
new Notice("⚠️ ERROR updating! Skipped file: " + filename, 0);
}
}
openPublishPanel();
%>