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

Frontmatter does not keep track of the fields' order

Open rmenai opened this issue 1 year ago • 2 comments

🐛 Describe the bug

When I try to generate my own frontmatter, I noticed that it does not keep track of the order of the fields. I want to sort my fields based on the order of importance so I want to have

---
title: ...
date: ...
tags: []
---

But instead I get this

---
tags: []
date: ...
title: ...
---

Config

local function generate_frontmatter(note)
  local date = os.date("%Y-%m-%d %H:%M")
  local out = { title = note.title, date = date, tags = note.tags }

  if note.metadata ~= nil and not vim.tbl_isempty(note.metadata) then
    for k, v in pairs(note.metadata) do
      out[k] = v
    end
  end

  return out
end

Environment

NVIM v0.10.0
Build type: RelWithDebInfo
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

Obsidian.nvim v3.9.0 (d1ca121353681e8f7dc8a69c760b950e12bcde22)
Status:
  • buffer directory: nil
  • working directory: /home/rami/.config/nvim/lua/plugins
Workspaces:
  ✓ active workspace: Workspace(name='notes', path='/home/rami/Documents/Vaults/notes', root='/home/rami/Documents/Vaults/notes')
Dependencies:
  ✓ plenary.nvim: a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683
  ✓ nvim-cmp: d818fd0624205b34e14888358037fb6f5dc51234
Integrations:
  ✓ picker: TelescopePicker()
  ✓ completion: enabled (nvim-cmp) ✗ refs, ✗ tags, ✗ new
    all sources:
      • nvim_lsp
Tools:
  ✓ rg: ripgrep 13.0.0
Environment:
  • operating system: Wsl
Config:
  • notes_subdir: nil

rmenai avatar Jul 30 '24 15:07 rmenai

Hey @rmenai I can definitely see the appeal of this. I tried implemented a Lua ordered table (see here) which would make this trivial, but it doesn't work with Lua 5.1 unfortunately. This isn't a complete blocker though, we could just frontmatter function return some other class that acts like ordered mapping, albeit without the default table API.

So, if someones interested in tackling this, here's what I would suggest:

  1. Create an ordered mapping class in lua/obsidian/collections.lua.
  2. Check for instances of that class in yaml.dump_lines and handle accordingly.

Alternatively we could allow generator functions to be returned from frontmatter_func, and handle those accordingly in yaml.dump_lines.

epwalsh avatar Jul 30 '24 22:07 epwalsh

Hey @rmenai I can definitely see the appeal of this. I tried implemented a Lua ordered table (see here) which would make this trivial, but it doesn't work with Lua 5.1 unfortunately. This isn't a complete blocker though, we could just frontmatter function return some other class that acts like ordered mapping, albeit without the default table API.

So, if someones interested in tackling this, here's what I would suggest:

1. Create an ordered mapping class in `lua/obsidian/collections.lua`.

2. Check for instances of that class in [`yaml.dump_lines`](https://github.com/epwalsh/obsidian.nvim/blob/b833db80659066661100b6397d30ea179861bdbd/lua/obsidian/yaml/init.lua#L101-L103) and handle accordingly.

Alternatively we could allow generator functions to be returned from frontmatter_func, and handle those accordingly in yaml.dump_lines.

Won't this work for the OrderedDict? https://github.com/Nyapaw/lua-ordered-dict

uroybd avatar Mar 03 '25 09:03 uroybd