corpus icon indicating copy to clipboard operation
corpus copied to clipboard

Sorting Fontmatter

Open kkharji opened this issue 3 years ago • 6 comments

Hey @wincent, I'm currently starting out with Corpus as a daily note taking interface to Obsidian, and so far I like the autotitle feature. I wonder if there's a way to stop Corpus from sorting the fontmatter when writing the file, I have many fontmatter fields for linking and organization purposes, and it would be nice to have them as the same order in which I defined them.

I will try to do some telescope integration some time next week, as I'd like to have the fontmatter field showing in the telescope picker, and hopefully contribute to the repo

Thanks ❤️

kkharji avatar May 31 '21 20:05 kkharji

I wonder if there's a way to stop Corpus from sorting the fontmatter when writing the file

Not presently. It is just iterating over the keys in the metadata using Vim's keys() function, so it emits/freshens the frontmatter based on whatever criteria Vim uses for ordering the keys.

So this would be a feature request. Basically, we'd need to stop using keys() and instead use an array of tuples, I think; eg. instead of:

{
  'foo': 'bar',
  'baz': 'qux',
}

this:

[
  ['foo', 'bar'],
  ['baz', 'qux'],
]

Would probably want to make this optional, as I expect some folks prefer the sortedness.

wincent avatar May 31 '21 20:05 wincent

Thanks for your quick reply, I'm working on a patch right now to make it optional.

kkharji avatar May 31 '21 20:05 kkharji

I tried doing something like

        if !get(l:config, 'autosort_fontmatter', 0)
          "....
          call add(l:metadata, [l:match[1], l:match[2]])
          " ...
        endif

and set autosort_fontmatter to 1 in my config, but no matter what I do, the case never result to true. Any ideas

kkharji avatar May 31 '21 21:05 kkharji

In general, you need a g: prefix to make the config option globally visible (l: makes it local to the script in which it appears).

If you're talking about the l:config that you see in various places like this one, that's coming in via a layer of indirection from the CorpusDirectories setting, that you might have set up something like this (that's from my dotfiles). Something like that should work, and you can see it working elsewhere in the plug-in, so there is probably some small detail that you haven't got quite right.

wincent avatar May 31 '21 21:05 wincent

Yah 😆 I'm using l:config as I see it in the file and I suppose it access CorpusDirectories keys. is it okay if I port the part of handling the metadata to lua. I bet I can solve this much faster with lua 🤣 .

Also, is sort random? not sure how to auto sort it like before.

kkharji avatar May 31 '21 21:05 kkharji

is it okay if I port the part of handling the metadata to lua.

I'm fine with anything going to Lua. Corpus originally started as Vimscript and I ported the performance-critical bits to Lua to make it faster; but Vimscript is maddening and I would be happy to see less Vimscript in the plug-in. The only reason I didn't port more was because I didn't want to risk introducing regressions for no user-visible benefit.

Also, is sort random? not sure how to auto sort it like before.

Well there was no explicit sort done before, but keys obviously returns keys in lexicographical order. So you would need to sort the array by the first item of the tuple.

wincent avatar May 31 '21 21:05 wincent