farling42 / obsidian-import-json

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

Multiple replacereg on the same data item #25

Closed jshank closed 1 year ago

jshank commented 2 years ago

Is there a way to run multiple replacereg on the same item? I'm capturing a large body of text and need to replace a few items inside of it.

farling42 commented 1 year ago

You could write a complex regexp expression, or you could nest multiple helpers like:

{{replacereg (replacereg field1 regexp1 replace1) regexp2 replace2}}

At any time you should be able to use (...) instead of a single parameter to one of these helper functions.

jshank commented 1 year ago

Great! That solves the problem. Is there any way I can document that replacereg as a helper and call it from multiple places in the template or do I have to make a copy for each call?

farling42 commented 1 year ago

I'm not an expert on handlebars, but I don't think that it is possible to set variables, so you will have to copy the expression each time.

farling42 commented 1 year ago

Version 0.22.0 is just being released right now. It provides an additional helper function which might help you: {{setvar varName varValue}}

In the simplest case, varName will be a string wrapped in quotes and varValue would be an expression.

jshank commented 1 year ago

That will definitely help. I was looking to see if the current implementation supported block-helpers:

Something like this:

Handlebars.registerHelper('parse5eTools', function(text) {
  text = text.replace('/\{@(?:condition|spell|creature|class|race|skill|action|sense) (.+?)\}/g', '[[$1]]');
  text = text.replace('/\{@(?:dice) (.+?)\}/g', '`dice: $1` ($1)');
  text = text.replace('/\{@(?:filter|item) (.+?)\|.+?\}/g', '$1');
  return text;
});

I could then call {{{parse5eTools text}}} for each block that needs to decode text. https://handlebarsjs.com/guide/block-helpers.html

farling42 commented 1 year ago

I've already added the helpers that are described in the readme.

I'm sure there's a way for me to export the handlebars variable for other modules/add-ons to add their own helpers too. It would just require adding some sort of synchronisation to the obsidian modules.

(I don't really know how modules work in Obsidian - most of my work have been in writing modules for Foundry VTT.)

farling42 commented 1 year ago

With the new setvar function, you should be able to save the regexp expressions for later usage; so that you're not having to copy very long regexp strings for each usage.