Template for a date stamp pointing at today's daily note


.action{/*Insert a ref linking to today's daily note*/}
.action{$yyyymmdd:= now |date "20060102"}
.action{$d := first (queryBlocks "SELECT * FROM blocks WHERE ial LIKE '%custom-dailynote-?%' limit 1;" $yyyymmdd)}
((.action{$d.ID} '.action{$d.Content}'))

While figuring this out, I learnt a few things about SiYuan, the Go template syntax and the Sprig add-ons that SiYuan supports that I would like to share:

  • In the second line, the function now returns a time stamp that is passed via | to the date formatting function, which uses 20060102 as an exemplary formatting string telling it that the desired format is yyyymmdd. The output of this pipeline is assigned to the variable $yyyymmdd, which we can use...

  • in the third line, where we run a SQL query against the blocks table. Blocks and spans, via queryBlocks and querySpans respectively, seem to be the only tables you can query from templates. The query is parameterized with today's date, and leverages the fact that daily notes are blocks that have an attribute custom-dailynote-<yyyymmdd> in their internal attributes list (in short, ial). We expect to find only one but emphasize that by limiting the output to just one result (LIMIT 1). Nevertheless, queryBlocks returns a list, so we apply the Sprig function first to its output to extract the first (and only) element in the output.

  • In the fourth line, we access the ID and Content of the block (the case of attributes matters, see e.g. here). We have several options here, for example, we could create an external link combining the Markdown syntax for links and the SiYuan protocol, [.action{$d.Content}](siyuan://blocks/.action{$d.ID}). This would work, but I think that a block reference or ref is better suited for an internal link.

    • with refs, we lose the ability to provide a title that will typically appear on hover
    • but we can specify whether the text that will be displayed is static (use double quotation marks, e.g. ".action{$d.Content}") or dynamic (single quotes, as in the listing)
    • dynamic anchors follow changes in the naming of the target, but they are refreshed only when the index is rebuilt. This can be done manually via the document tree ... menu. [I ignore if something else can trigger this (startup?), please add a comment if you know!] For example, we could have written 'today' and the text would stay there until the index is rebuilt, potentially after some days, at which point it would become 2024-05-30.

    That's it! You can place the code in a file named e.g. ref-today.md at <workspace>/data/templates/ or a subdirectory of that path, and it will appear when you type /template as one of the available options. This can be streamlined, see @frostime's insert time plugin to get something like /date to insert a datestamp (albeit without link to the corresponding daily note).

EDIT: typos, clarity

    1 Operate
    alvorithm updated this article at 2024-05-31 00:10:52

    Welcome to here!

    Here we can learn from each other how to use SiYuan, give feedback and suggestions, and build SiYuan together.

    Signup About
    Please input reply content ...