threedaymonk / htmlbeautifier

A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.
MIT License
325 stars 59 forks source link

Allow custom elements #74

Open inkstak opened 1 year ago

inkstak commented 1 year ago

We use lookbook which relies on htmlbeautifier to output pretty HTML and we have some custom block elements (such as <turbo-frame>) which are not rendered indented.

This PR replace the constants from HtmlBeautifier::HtmlParser by methods which allows to specify additional elements:

HtmlBeautifier::HtmlParser.block_elements << "main"
HtmlBeautifier::HtmlParser.block_elements << "turbo-frame"
HtmlBeautifier::HtmlParser.block_elements << "whatever"

The downside is that mappings is compiled each time HtmlParser is initialized. If this is an issue, I guess I can add a method to compile and cache the mappings only once.

I've add tests in a second commit but:

threedaymonk commented 4 months ago

Sorry for neglecting this for so long! I think this is a good feature, but I'm not keen on the class attributes. I think I'd prefer to have these as attributes of the particular instance of HtmlParser.

Perhaps the best implementation would be to expose the list of elements, and let you inject a parser, so that you could then subclass the parser with whatever elements you like. I'll think about it.