ok, got it working for both pages and journals. Although journals naming is configurable from config.edn and I could have make them match, I like the files naming to contain no spaces, commas or dots and the Logseq Page Name should be something easy on the eyes, like “Saturday, 02.12.2023”.
So, based on my specific situation, where the filename for journals on disk is 2023_12_03.md
, I needed to split the Logseq Page Name by the space, split by dot the remaining string, reverse it and replace dots with underscores and add an .md
termination. That is for filenames that contain a date so they mostlikely are journal files while page files just need the extension added to the Page Name string.
I ended up with this code:
const inputString = logseq.api.get_page(block.page.id).originalName
const pageName = inputString.split(" ")[1].match(/^\d{2}\.\d{2}\.\d{4}$/) ? inputString.split(" ")[1].split(".").reverse().join("_") + ".md" : inputString + ".md";
Now I get markdown Links in the desired form:
# for journals:
[Link](((6568d3a4-6874-4782-a70c-3f42de833d03)) "2023_11_25.md")
# for pages:
[Link](((656a5e14-afe9-4032-92af-287da1ceb7bd)) "ScratchPad.md")
I’ll put the whole code here so this thread feels finished:
logseq.kits.setStatic(function blockref(span){
const uuid = span.dataset.ref.slice(2, -2)
const block = logseq.api.get_block(uuid)
const inputString = logseq.api.get_page(block.page.id).originalName
const pageName = inputString.split(" ")[1].match(/^\d{2}\.\d{2}\.\d{4}$/) ? inputString.split(" ")[1].split(".").reverse().join("_") + ".md" : inputString + ".md";
if (!block) return
const length = span.dataset.length
const hasLength = length !== 'undefined' && length !== '$2';
var title = block.content.slice(0, (hasLength) ? length : block.content.length);
const index = title.indexOf("\n")
if (index > -1) title = title.slice(0, index)
const blockId = span.closest(".ls-block").getAttribute("blockid")
const sourceContent = logseq.api.get_block(blockId).content
const ref = span.dataset.ref
const target = "{{ref" + ref + (hasLength ? ", " + length : "") + " }}"
const markdown = title + "[Link]( " + ref + " \"" + pageName + "\")"
logseq.api.update_block(blockId, sourceContent.replace(target, markdown))
})
I’m pretty much done with this one, thanks for the help.