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

[Feature Request] Sorting Notes with specific Metadata Tag #23

Closed
blaettlerio opened this issue Nov 1, 2022 · 4 comments · Fixed by #26
Closed

[Feature Request] Sorting Notes with specific Metadata Tag #23

blaettlerio opened this issue Nov 1, 2022 · 4 comments · Fixed by #26
Assignees
Labels
enhancement New feature or request

Comments

@blaettlerio
Copy link

Wondering if it would be possible to enhance the functionality of the plugin with the ability to sort notes in a folder with the help of a specific Metadata Tag?

Use Case:
If you are creating notes based on books I enter the the page number of the book within the metadata of the note.
Therefore it would be beneficial to have the option to sort the notes with that specific tag (e.g. Pages).

@SebastianMC SebastianMC added the enhancement New feature or request label Nov 2, 2022
@SebastianMC SebastianMC self-assigned this Nov 2, 2022
@SebastianMC
Copy link
Owner

Hi @blaettlerio!

the idea of the suggested feature seems promising!

I've done some initial evaluation of how the implementation could behave and I've created a working prototype locally. This resulted in a bunch of questions. I don't want to overcomplicate the solution, at the same time I don't to oversimplify it

Please take a look below points and let me know what you think:

1. First and foremost, do I understand how the feature should work?

A note N1 contains YAML frontmatter with a metadata like:

---
Pages: 5
---

A note N2 at the same time contains:

---
Pages: 1
---

And you want to be able to tell the plugin (via custom sorting specification) to use the value of the metadata Pages to sort.

In result, if the sorting specified is alphabetical, the note N2 should go before N1. For reverse alphabetical order, N2 goes before N1.

2. If my undestanding in 1. is correct, then...

... the potential syntax of specification could look like:

sorting-spec: |
  target-folder: Book notes
  /:files metadata: Pages
    < a-z

The above specification could be read as: in the folder Book notes take the files which contain the Pages metadata in their YAML. Group them together and sort alphabetically by the actual value of the metadata Pages

3. If 1. and 2. are fine, the intentional limitations of the implementation could be:

  • 3.1. only support alphabetical and reverse alphabetical sorting by the metadata tag value. It means, in particular, that simple numbers will be sorted correctly (e.g. 1 goes before 9, then 10, after which goes 90). At the same time, compound numbers would not work correctly (e.g. 1.10 is not guaranteed to go after 1.1).
  • 3.2. if the metadata tag value is the same for two notes, their relative order is undefined (or random). Theoretically a fallback could be introduced to sort by note name, yet this looks like unnecessary overcomplicating
  • 3.3. for folders, take into account the metadata of the folder note (if it exists)
  • 3.4. from performance perspective, there would be no automatic (re)sorting of notes after editing their metadata. An explicit click on the ribbon button of the plugin would be required to re-apply the sorting with the updated metadata values consumed.
    • the technical rationale is that, as of now, the automatic (re)sorting of file explorer items happens on the selected set of events: adding or removing a file or folder, renaming or moving a file or folder. These are relatively rare events. Note content change (including note metadata change) doesn't trigger automatic re-sorting. Hooking up the plugin to re-apply sorting after each change of note's metadata seems like an overkill. Attempting to add some logic to check if the changed metadata should actually trigger the re-sorting is like a double overkill and risk of bugs. That's why my thinking towards the simple approach: 'click the plugin ribbon button after the change of note's metadata, if you want to refresh the sorting'

Let me know what you think :-)

SebastianMC added a commit that referenced this issue Nov 3, 2022
- added support for grouping items by the presence of specified metadata
  - new keyword `metadata:` introduced for that purpose in lexer
  - if metadata field name is omitted, the default `sort-index-value` is used
- on top of the above, added support for sorting by the value of the specified metadata
- unit tests of sorting spec processor extended accordingly
SebastianMC added a commit that referenced this issue Nov 3, 2022
SebastianMC added a commit that referenced this issue Nov 3, 2022
SebastianMC added a commit that referenced this issue Nov 3, 2022
- readme update
- minor finetuning of syntax for metadata
@blaettlerio
Copy link
Author

Hi @SebastianMC

Thank you for your answer.

  1. Yes you got my idea exactly. :-)

  2. From a syntax perspective I think that would be perfect. And would fit into the plugins existing plugin functionality.

3.1. Think the a-z ascending or descending would be a neat starting point.
3.2. At least for me unnecessary...but wouldn't it be possible to define/combine those to options via yaml manually or via inheritance? As I would think that some people might have the expectation to sort a-z if two notes have the same metadata tag but I could be wrong?!

First sort priority: Metadata
Second sort priority: note name

3.3. Good idea....did not think about that although I use folder notes too ;-)

3.4. Agree....constantly monitoring changes on a file level would be overkill.

Let me know if you have other questions.

SebastianMC added a commit that referenced this issue Nov 9, 2022
- more advanced version of implementation: with-metadata and by-metadata support
- readme update
SebastianMC added a commit that referenced this issue Nov 9, 2022
- code comments update
SebastianMC added a commit that referenced this issue Nov 9, 2022
- code example in README.md - indentation adjusted
@SebastianMC SebastianMC linked a pull request Nov 9, 2022 that will close this issue
@SebastianMC
Copy link
Owner

SebastianMC commented Nov 9, 2022

A bit more advanced implementation is completed in the branch and ready to merge in the PR #26.
The tricky additions are fully covered by unit tests.
Some additional live tests have to be done with fresh head before merge

Highlights of this new feature:

  • new keyword added with-metadata: to allow grouping notes by presence of the specified metadata
  • new keyword added by-metadata: to tell that sorting should take the value of specified metadata, and not the name of note or folder
  • the two above keywords can be used together or separately, they serve two different purposes
  • for folders, their 'folder notes' are examined for metadata
  • if two items have the same metadata value, their names are compared as a fallback
  • items not having the specified metadata go below the ones containing the metadata (both in alphabetical and reverse alphabetical, intentionally)
  • if metadata of notes change, to refresh the notes order a click to the plugin ribbon button is necessary (re-parse the configuration, re-scan metadata of notes and sort)

The simplest sorting config example discussed in previous notes would look like:

sorting-spec: |
  target-folder: Book notes
  < a-z by-metadata: Pages

SebastianMC added a commit that referenced this issue Nov 11, 2022
- extended to also support true alphabetical on metadata fields
SebastianMC added a commit that referenced this issue Nov 11, 2022
- release unnecessary references after sorting completed
SebastianMC added a commit that referenced this issue Nov 12, 2022
SebastianMC added a commit that referenced this issue Nov 12, 2022
* #23 - support for sorting by metadata

- added support for grouping items by the presence of specified metadata
  - new keyword `with-metadata:` introduced for that purpose in lexer
  - if metadata field name is omitted, the default `sort-index-value` is used
- added support for sorting items by notes and folders metadata
  - new keyword 'by-metadata:' introduced for that purpose
  - if metadata field name is omitted, the default `sort-index-value` is used (or metadata name inheritance is used)
- unit tests of sorting spec processor extended accordingly
- documentation and code example in README.md 
- extended to also support true alphabetical on metadata fields
- release unnecessary references after sorting completed
@blaettlerio
Copy link
Author

blaettlerio commented Nov 13, 2022

Tested it! Works like a charm!

Thank you for the fast implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants