obsidian.nvim icon indicating copy to clipboard operation
obsidian.nvim copied to clipboard

fix: erroneous title in daily note creation from template

Open zDonik1 opened this issue 1 year ago • 4 comments

The {{title}} template parameter would get incorrectly replaced with the alias, which is a big number, presumably being the creation timestamp. The expected behavior is to replace it with filename.

Example

The template:

# {{title}}

The created daily note (filename: 2024-03-16.md):

# 1710608450

zDonik1 avatar Mar 16 '24 17:03 zDonik1

@zDonik1 I'm confused why your alias ends up as a timestamp and not a formatted date. Can you share your config? In particular I'm curious what you have set for daily_notes.alias_format.

epwalsh avatar Mar 18 '24 16:03 epwalsh

I don't have anything set for the daily_notes.alias_format.

Here is my entire config:

require("obsidian").setup({
    sort_by = "accessed",
    disable_frontmatter = true,
    workspaces = {
        {
            name = "personal",
            path = "~/iCloudDrive/iCloud~md~obsidian/SecondBrain",
        },
    },
    daily_notes = {
        folder = "periodic_notes",
        template = "daily_template.md",
    },
    templates = {
        subdir = "templates",
        time_format = "%X",
        substitutions = {
            ["time:HH:mm:ss"] = function()
                return os.date("%X")
            end,
        },
    },
    completion = {
        nvim_cmp = true,
        min_chars = 1,
    },
    new_notes_location = "current_dir",
    note_id_func = function(title)
        return title
    end,
    wiki_link_func = "use_path_only",
    mappings = {
        ["gf"] = {
            action = function()
                return require("obsidian").util.gf_passthrough()
            end,
            opts = { noremap = false, expr = true, buffer = true },
        },
        ["<leader>oh"] = {
            action = function()
                return require("obsidian").util.toggle_checkbox()
            end,
            opts = { buffer = true },
        },
    },
    callbacks = {
        ---@param client obsidian.Client
        ---@param workspace obsidian.Workspace
        post_set_workspace = function(client, workspace)
            client.log.info("Changing directory to %s", workspace.path)
            vim.cmd.cd(tostring(workspace.path))
        end,
    },
    ui = {
        hl_groups = {
            ObsidianTodo = { bold = true, fg = mocha.peach },
            ObsidianDone = { bold = true, fg = mocha.sapphire },
            ObsidianRightArrow = { bold = true, fg = mocha.peach },
            ObsidianTilde = { bold = true, fg = mocha.red },
            ObsidianBullet = { bold = true, fg = mocha.sky },
            ObsidianRefText = { underline = true, fg = mocha.mauve },
            ObsidianExtLinkIcon = { fg = mocha.mauve },
            ObsidianTag = { italic = true, fg = mocha.blue },
            ObsidianHighlightText = { bg = mocha.flamingo },
        },
    },
})

zDonik1 avatar Mar 19 '24 01:03 zDonik1

I have done some tests, and it seems that the alias is getting set to a timestamp because lua doesn't understand what is going on when doing date printing. It seems like there is an extra - in the day section. Here:

https://github.com/epwalsh/obsidian.nvim/blob/a6b5e97234a642f9f7446091fe08638f067479f2/lua/obsidian/client.lua#L1895-L1900

When I removed it, the alias was properly getting initialized.

The only thing is that it still doesn't solve the issue with the wrong {{title}} replacement. That is why I believe the change in this PR is also valid.

zDonik1 avatar Mar 19 '24 01:03 zDonik1

It doesn't have to be the Id. Obsidian replaces {{title}} with the file name, while obsidian.nvim doesn't. Thats the core of the issue. We should make the documentation more obvious of this fact if the behavior will not be changed.

zDonik1 avatar Mar 19 '24 03:03 zDonik1