-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
👍 known problem of all parsers ;) |
what options we would like to see? |
We currently just use |
Maybe create posthtml plugin for this? |
How could a plugin control rendering? |
It is impossible to use the htmlparser2 - also can not properly form the tree and take into account all of this. :( <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 <tag key="val1\n
val2" key2="val" key3="val">\n
!!!\n
</tag> and it is very important! |
can you give an more examples of how you would like to work with line breaks? |
It just breaks where it can break in HTML without introducing significant whitespace, e.g. not in |
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 |
@voischev OK, this is walking string nodes? As long as we make sure it's not in |
@devinus safe 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 why you transferred this issue to posthtml-plugins repository? The issue doesn't seem to be related to this project. |
@maltsev Hi, this is not a problem, but ideas that can be implemented into a plugin |
Safely limits line length in resulting output by adding linebreaks.
E.g.
Becomes
The text was updated successfully, but these errors were encountered: