kgar / foundry-vtt-more-handlebars-helpers

A Foundry VTT Module which registers a some additional handlebars helpers.
MIT License
2 stars 0 forks source link

RegExp helper please #6

Closed farling42 closed 11 months ago

farling42 commented 11 months ago

A user wants to strip out blank lines, but that requires a RegExp to be used.

A simple handler such as:

function hb_regexp(text) {
    return new RegExp(text, "g");
}

Handlebars.registerHelper('hb-regexp', hb_regexp);

This will allow a user to use an expression to remove break lines from the HTML of item/actor descriptions, such as:

{{{more-handlebars-helpers-sanitize (more-handlebars-helpers-replaceAll system.description.value (more-handlebars-helpers-regexp '\n<hr />') '') }}}

(The "\n" isn't recognised by the basic replaceAll function)

kgar commented 11 months ago

You got it! This is a really cool orthogonal helper to put in.

Would you like the flag to be optional with a default to "g"? Asking on behalf of the "i" flag.

kgar commented 11 months ago

I ran a test with this helper, and here's what I'm seeing:

The helper:

export default function (text) {
  return new RegExp(text, 'g');
}
Handlebars.compile(`{{ more-handlebars-helpers-sanitize (more-handlebars-helpers-replaceAll (more-handlebars-helpers-regexp value '\n<hr />') ' ') }}`)({ value: "Hello,\n<hr />there,\n<hr />world!"})

Result:

'/Hello,\\n&lt;hr \\/&gt;there,\\n&lt;hr \\/&gt;world!/g'

I'll keep digging to see why it's not working like we'd expect. If you see something in my test code above I could do differently, let me know.

kgar commented 11 months ago

Putting my bad test here in the public helped me fix my bad test 🤣

Updated test:

Handlebars.compile(`{{ more-handlebars-helpers-sanitize (more-handlebars-helpers-replaceAll value (more-handlebars-helpers-regexp '\n<hr />') ' ') }}`)({ value: "Hello,\n<hr />there,\n<hr />world!"})

Result:

'Hello, there, world!'

🎉

farling42 commented 11 months ago

Concerning optional, I guess the RegExp could take a second optional parameter, which would be the flags passed as the second argument for the RegExp constructor. Much better for those who like more than just "g" :-)

kgar commented 11 months ago

Version 1.2.0 has released. Give it a go, and if things are working, go ahead and close the issue.

farling42 commented 11 months ago

It is working just fine. Thanks for the rapid turn-around!

ObsidianTTRPGProject commented 11 months ago

Thank you both! We have rendering stat-blocks!

image

farling42 commented 11 months ago

It just needs the Trigger/Effect text better formatted now 😉

ObsidianTTRPGProject commented 11 months ago

Baby-steps lol.