farling42 / obsidian-import-json

Plug-in for Obsidian.md which will create Notes from JSON files
MIT License
85 stars 5 forks source link

#match with a regexp doesn't seem to work #18

Closed ckennedy666 closed 2 years ago

ckennedy666 commented 2 years ago

I'm loading TSV data and when I attempt to use match from Handlebar-Helpers I get errors. What I'm trying to do is process a field into an array using a regexp then process the element of the array into a list using #each. I get nothing but errors when I try to use the following code:

Stellar: 
{{#each (match (Stars) (toRegex "[OBAFGKM]\d\s\w+|D|BD|BH|NS|PSR")) ~}}
- {{this}}
{{/each ~}}

The field Stars contains data like:

G0 V D
F1 V D
M3 V
K6 II M2 V

The regex is to unstring the multiple stars in the data field and put them on separate lines.

farling42 commented 2 years ago

I'm not at my computer right now, but why do you have a tilde before the closing brackets "~}}" ? 

ckennedy666 commented 2 years ago

I'm not at my computer right now, but why do you have a tilde before the closing brackets "~}}" ? 

To suppress white space generation at those points.

farling42 commented 2 years ago

Where can I get hold of a TSV file (traveller star map) so that I can test your markdown template?

farling42 commented 2 years ago

In a quick test, you should not have the parentheses around the "Stars" name immediately after match

farling42 commented 2 years ago

The match function is better handled by a syntax like:

Stellar: 
{{#each (match Stars "[OBAFGKM]\d\s\w+|D|BD|BH|NS|PSR" "regex: true") ~}}
- {{this}}
{{/each ~}}
farling42 commented 2 years ago

Having looked at the match function, I don't think it does what we thought it does.

It will look for matches in the array of strings passed as the first parameter for any entries which match the second parameter.

What we need is a function that will break a string based on a regex.

farling42 commented 2 years ago

I've just created 0.18.0 which adds a new function 'strsplit' which splits a single string into multiple parts.

The following seems to work with the new helper function:

Stellar: {{#each (strsplit Stars (toRegex "([OBAFGKM]\d)")) ~}}
{{#ifOdd @index }}

- {{/ifOdd}}{{this}}{{/each}}

(The separator is put into the output array as a separate element, and the first element is a blank string, thus the use of #ifOdd to put the dash on a newline before the star's main classification.

ckennedy666 commented 2 years ago

Hmm... Tried to update to the latest, even using BRAT, and I'm not getting the strsplit helper included. Also the version still identifies as 0.17 instead of 0.18.

farling42 commented 2 years ago

Ah, I forgot to update several files with the new version number. I'll fix it

farling42 commented 2 years ago

0.19.0 should be marked properly, and should have the strsplit helper available.

ckennedy666 commented 2 years ago

I'll test tonight. Thanks for all the work.

ckennedy666 commented 2 years ago

Working perfectly (once I adjusted my regex). Thanks so much!