Skip to content

Changes to accept every Dataview metadata format#92

Merged
chhoumann merged 2 commits intochhoumann:masterfrom
theofbonin:master
Mar 2, 2023
Merged

Changes to accept every Dataview metadata format#92
chhoumann merged 2 commits intochhoumann:masterfrom
theofbonin:master

Conversation

@theofbonin
Copy link
Copy Markdown
Contributor

Changes to also parse for "(field:: value)" and "[field:: value]",
as opposed to only "field:: value".

Changes to also parse for "(field:: value)" and "[field:: value]",
as opposed to only "field:: value".
Copy link
Copy Markdown
Owner

@chhoumann chhoumann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @theofbonin!

Thanks for the PR!

I've been testing for a bit using the Dataview docs material on inline fields:

Basic Field:: Some random Value
**Bold Field**:: Nice!
I would rate this a [rating:: 9]! It was [mood:: acceptable].
- [ ] Send an mail to David about the deadline [due:: 2022-04-05].
This will not show the (longKeyIDontNeedWhenReading:: key).

I get the following results:
image

And zooming in on rating gives
image

Which does seem to provide unexpected results. I'd expect it to prompt me for just rating with a pre-filled value of 9, rather than splitting the line.

Regarding your regex in parseInlineFields: I'd use a * after the \s rather than ? so the second match group doesn't get spaces, in case there are more than one. E.g. [mood: acceptable] would ignore the many spaces.
image

It also seems to be the cause of the unexpected results I mentioned above. Using the following leads to normal results:
Using const regex = /[\[\(]?([^\n\r\(\[]*)::\s?([^\)\]\n\r]*)[\]\)]?/g;
The only thing I added was [\[\(]? before the first group (key) and [\]\)]? after the second group (value).
Result:
image

…o handle special characters in properties.

Updated "parser.ts" regex to handle properties without values and newlines after `::`.
Also added "escapeSpecialCharacters" method to handle special regex characters in properties, and updated regexes on "metaController.ts".
@theofbonin
Copy link
Copy Markdown
Contributor Author

theofbonin commented Mar 2, 2023

Hey @chhoumann!

I made some changes and now it seems to be working for every case. I fixed some bugs that I found and made it work with some other cases that I had thought about.

Screenshot 2023-03-02 at 09 53 04

Screenshot 2023-03-02 at 09 53 50

Screenshot 2023-03-02 at 10 00 55


I changed the regex in "parser.ts" to a new one that conforms to the review:
/[\[\(]?([^\n\r\(\[]*)::[ ]*([^\)\]\n\r]*)[\]\)]?/g
Instead of using \s*, I used [ ]*, because using \s* would cause problems with properties that are declared without a value.

Screenshot 2023-03-02 at 11 47 17


I had to create the "escapeSpecialCharacters" method because properties with special regex characters were causing problems when editing the value. For instance, **Bold Field** was causing:
Uncaught (in promise) SyntaxError: Invalid regular expression: /**Bold Field**:{1,2}/

I changed the regexes with ${property.key} to ${this.escapeSpecialCharacters(property.key)}.

I also changed the regexes in "metaController.ts" to conform to the review.


Known issues:
When there is more than one of the same field on the same file, it appears many times on the Metaedit screen (you can see this on the second screenshot with the field mood). This shouldn't be an issue because the "right" way to declare fields on dataview is using lists or arrays:

field1:: [value1, value2, value3]
field2::
    - value1
    - value2
    - ...

Even though it does accept many of the same field, like:

field:: value1
field:: value2
field:: value3

This will be considered by dataview as field:: [value1, value2, value3]. Another problem related to this same thing is that when you modify the value of a field, it changes all occurrences of it in the file.

This issue was already happening even without accepting [field:: value] and (field:: value), so it is not really related. However, I could try to figure out a way to work with many of the same field in the same file if you think that would be necessary.

@chhoumann
Copy link
Copy Markdown
Owner

This is fantastic, @theofbonin! Everything looks great. Awesome job. You even fixed #54! Thank you for the PR!
Tagging here to close:
fix #54

I'd love to see issues where you document your findings on the known issues you mention. I think those should be fixed, but I agree that this PR isn't the place to do it. It's good to avoid bloating change requests with too many things at once.

@chhoumann chhoumann merged commit ef3efb0 into chhoumann:master Mar 2, 2023
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 2, 2023

🎉 This PR is included in version 1.8.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants