This is a most excellent project. Thanks for making it available to us.
For my use-case I’ll cross-post from another thread
I took a slightly different approach to this problem by reusing code from the custom macros that replace themselves thread.
My use case
- I wanted a scheduled task to automatically appear on my journal page each day reminding me to take some vitamins.
- I use Logseq on both mobile and desktop
So I wrote a macro {{schedule-date-today}
}that replaces itsself with SCHEDULED: <todays date>
when and only when it is evaluated on a journal page. Otherwise it just behaves as a normal macro.
Environment
- I use
:default-templates { :journals }
to load a template to the daily journal. - I use kits to run custom javascript.
configuration.edn
:default-templates { :journals "daily-journal" }
:macros {
:schedule-date-today "<div class='kit' data-kit='expandmacro'>||scheduled today||</div>"
}
templates.md
- daily journal template
template:: daily-journal
template-including-parent:: false
- TODO {{icon ef63}} Take vitamins
{{schedule-date-today}}
The :schedule-date-today macro directs kits to run the javascript in the code block contained in the first child block of the page expandmacro.
expandmacro.md
```javascript
logseq.kits.setStatic(function expandmacro(div){
const blockId = div.closest(".ls-block").getAttribute("blockid");
const block = logseq.api.get_block(blockId);
const pageId = block.page.id;
const page = logseq.api.get_page(pageId);
const pageIsJournal = page['journal?'];
if (!pageIsJournal) { console.log('is not journal'); return; }
// This date is in the org-mode SCHEDULED format
const date = new Date();
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const year = date.getFullYear();
const dayOfWeek = date.toLocaleString('en-US', { weekday: 'short' });
const formattedDate = `${year}-${month}-${day} ${dayOfWeek}`;
const content = block.content;
const macroStart = content.indexOf("{{" + div.closest(".macro").dataset.macroName);
const macroEnd = content.indexOf("}}", macroStart) + 2;
logseq.api.update_block(blockId, content.slice(0, macroStart) + `SCHEDULED: <${formattedDate}>` + content.slice(macroEnd));
});
// {{schedule-date-today}}
\```