Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontmatter does not keep track of the fields' order #672

Open
rmenai opened this issue Jul 30, 2024 · 1 comment
Open

Frontmatter does not keep track of the fields' order #672

rmenai opened this issue Jul 30, 2024 · 1 comment
Labels

Comments

@rmenai
Copy link

rmenai commented Jul 30, 2024

🐛 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 rmenai added the bug Something isn't working label Jul 30, 2024
@rmenai rmenai changed the title Frontmatter does not keep the given order Frontmatter does not keep track of the fields' order Jul 30, 2024
@epwalsh
Copy link
Owner

epwalsh commented Jul 30, 2024

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 epwalsh added contributions welcome Extra attention is needed feature request and removed bug Something isn't working labels Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants