Writing a script
import * as obsidian from 'obsidian';
const runJS = app.plugins.plugins["runjs"];
const dataviewAPI = runJS.app.plugins.plugins["dataview"].api;
const list_pages_all = dataviewAPI.pagePaths().values;
let list_pages = filterBySubstring(list_pages_all, "books/details");
let books_list = await runJS.suggest('Select book', list_pages);
let current_book = dataviewAPI.page(books_list).file;
const list_operation = ['Start of reading', 'End of reading']
let choice = await runJS.suggest('Select action', list_operation, 'Start of reading');
operation = choice === "Start of reading" ? "start" : "end"
runJS.app.fileManager.processFrontMatter(current_book, (frontMatter) => {
const startDate = frontMatter[operation];
if (isEmptyVariable(startDate)) {
let datenow = formatDate(new Date());
frontMatter[operation] = datenow;
}
})
function isEmptyVariable(variable) {
if (variable === undefined || variable === null) {
return true;
}
if (typeof variable === 'string' && variable.trim() === '') {
return true;
}
return false;
}
function formatDate(date) {
let dayOfMonth = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
year = year.toString();
month = month < 10 ? '0' + month : month;
dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;
return `${year}-${month}-${dayOfMonth}`
}
function filterBySubstring(array, substring) {
return array.filter(function(item) {
return item.includes(substring);
});
}
Stuck on a line
runJS.app.fileManager.processFrontMatter(current_book, (frontMatter) => {
because I must be passing the wrong type of data to the function. How can I fix it?
And the second question - how can I put some functions in a separate module, as they are repeated in different scripts?