Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin]: Add an option to limit line length in output #5

Open
devinus opened this issue Feb 7, 2016 · 13 comments
Open

[plugin]: Add an option to limit line length in output #5

devinus opened this issue Feb 7, 2016 · 13 comments

Comments

@devinus
Copy link

devinus commented Feb 7, 2016

Safely limits line length in resulting output by adding linebreaks.

E.g.

<some super="long" ... thing>

Becomes

<some super="long" ...\n
thing>
@voischev
Copy link
Member

voischev commented Feb 7, 2016

👍 known problem of all parsers ;)

@voischev
Copy link
Member

voischev commented Feb 7, 2016

what options we would like to see?

@devinus
Copy link
Author

devinus commented Feb 7, 2016

We currently just use html-minifier's maxLineLength option. It could be more customizable to enable breaking at different places, but I think by default it just breaks on tags and between attributes but not within.

@voischev
Copy link
Member

voischev commented Feb 8, 2016

Maybe create posthtml plugin for this?

@devinus
Copy link
Author

devinus commented Feb 8, 2016

How could a plugin control rendering?

@voischev
Copy link
Member

voischev commented Feb 8, 2016

It is impossible to use the posthtml-render at full power. :(
Since posthtmltree is very simple tree, which does not have the saving character positions or line numbers.

htmlparser2 - also can not properly form the tree and take into account all of this. :(
For example: htmlparser2 miss inside \n in tags

<tag key="val1\n 
    val2"\n 
    key2="val"\n
    key3="val"\n
>\n
!!!\n
</tag>

htmlpaser2 skip all '\n' in tag. Most parsers too not able to this
result:

<tag key="val1\n 
 val2" key2="val" key3="val">\n
!!!\n
</tag>

and it is very important!

@voischev
Copy link
Member

voischev commented Feb 8, 2016

can you give an more examples of how you would like to work with line breaks?

@devinus
Copy link
Author

devinus commented Feb 8, 2016

It just breaks where it can break in HTML without introducing significant whitespace, e.g. not in pre and between attributes but not within attribute strings, etc.

@voischev
Copy link
Member

voischev commented Feb 8, 2016

in plugin, we can control the text nodes and attribute value.

tree
    .match({ attrs: true }, node => {
        node.attrs.style = node.attrs.style.replace(';', ';\n'); // for example
        return node;
    })
    .walk(node => {
        if (node.hasOwnProperty('tag')) return node;
        return node.replace(/(\w+)\s/g, '$1\n ');
    })

or vice versa
this is enough?

@devinus
Copy link
Author

devinus commented Feb 8, 2016

@voischev OK, this is walking string nodes? As long as we make sure it's not in <pre> tags we should be good.

@voischev
Copy link
Member

voischev commented Feb 9, 2016

@devinus safe \n in pre content expample:

var posthtml = require('posthtml');
var html = '<div style="sdf \n">sdf\n \n \nsdfg\n</div><pre style="ss\n"> pre1  \npre2 \n<b>pre3\n</b>    \n<i>pre4\n</i></pre><div> aaaa1\n</div>';


function plugin(tree) {
    var isPreContent;
    tree
        .match({ tag: true, content: true }, node => {

            if (node.tag === 'pre') {
                node.content = tree
                    .match
                    .bind(node.content)(
                        { tag: true },
                        node => {
                            node.isPreContent = true;
                            return node;
                        }
                    );
            }
            return node;
        })
        .match({ attrs: { style: true }, isPreContent: false }, node => {
            // TODO: parse all attrs
            node.attrs.style = node.attrs.style.replace(/\n/g, '');
            return node;
        })
        .walk(node => {
            if (node.hasOwnProperty('tag')) {
                isPreContent = node.isPreContent;
                // clear plugin meta
                delete node.isPreContent;
                return node;
            }
            return !isPreContent ? node.replace(/\n/g, '') : node;
        });
}
posthtml([plugin]).process(html).then(result => {
    console.log(JSON.stringify(result.tree));
});

🎉

@Scrum Scrum self-assigned this Dec 11, 2017
@Scrum Scrum removed their assignment Jan 18, 2018
@Scrum Scrum changed the title Add an option to limit line length in output [plugin]: Add an option to limit line length in output Feb 28, 2018
@Scrum Scrum transferred this issue from posthtml/posthtml-render Feb 15, 2019
@maltsev
Copy link
Member

maltsev commented Feb 15, 2019

@Scrum why you transferred this issue to posthtml-plugins repository? The issue doesn't seem to be related to this project.

@Scrum
Copy link
Member

Scrum commented Feb 15, 2019

@maltsev Hi, this is not a problem, but ideas that can be implemented into a plugin

@Scrum Scrum transferred this issue from posthtml/posthtml-plugins Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants