Swaagie / minimize

Minimize HTML
MIT License
162 stars 18 forks source link

Ignore certain blocks from minification? #50

Open canebat opened 9 years ago

canebat commented 9 years ago

I have a large block of static markdown text that later gets parsed by my javascript.

ie,

<span id="code demo">
    **Test**

    blah blah blah
    ---
// This is how to print to console:
console.log(myvariable);
```

> He said, She said.



When I minify this, all the spacing gets removed and so the resulting markdown looks all squished when rendered.

Specifying `empty: true, loose: true, spare: true` does not help.

Is there a way I can keep my markdown text in HTML, but tell `minimize` to ignore this block?
Swaagie commented 9 years ago

You could use the defaults blocks of code and/or pre which will not be stripped from spaces. See, https://github.com/Moveo/minimize/blob/master/lib/list.js#L36-L39, the mentioned options only work on attributes. Let me know if this works, PR to improve the docs are always welcome :)

virgofx commented 8 years ago

@Swaagie I know you've mentioned in the README.md that PHP inline templates are not supported but adding functionality that would allow for certain start/end blocks to be ignored would solve that as well.

I was thinking about trying to implement this actually and perhaps it would require up to 2 passes where you don't actually ignore certain blocks but replace them with placeholders {{$1}} as necessary prior to the current HTML minification logic and restoring the map subsequently afterwards. Would this be possible? I'm desperately trying to achieve minification of PHTML for the HTML only portion (while ignoring PHP) --- <div>Hello <?php echo 'John'; ?></div>

Would this be possible or easy to integrate?

Swaagie commented 8 years ago

Sorry for the slow response, the only proper way to implement this would be by providing a custom DOM parser. Currently the domparser of htmlparser2 is used by default. This parser confuses the <?php ?> of PHP with HTML. So parsing PHP templates would require a custom parser that understands these PHP tags. Minimize already supports custom parsers as argument of the constructor. But I think the simpler route is probably processing the templates after the PHP parser has inserted the variables and just before the content is send of the wire to the browser.

edit: as for replacing, that could be done probably as {{$1}} would be seen as text by the parser. That is outside the scope of this module though. But it is something you could probably achieve reasonably simple yourself. By simply replacing the PHP tags before the template is run through minimize.