A PostHTML plug-in that adds comments after HTML elements.
Before:
OMG
After:
OMG<!-- /.wow --><!-- /#wrapper.container -->
Install
npm install posthtml posthtml-comment-after --save-dev
Usage
const fs = ;const posthtml = ;const commentAfter = ;;
Options
- output
- targetAttribute
- match
output.sameline
You can specify whether to insert comments on the same line.
Default
- output.sameline:
true
Add option:
const option =output:sameline: false;;
Before:
OMG
After: comment is inserted after a line break.
OMG<!-- /.wow --><!-- /#wrapper.container -->
output.class
output.idYou can specify display / non-display of id
and class
name in comment.
Default
- output.id:
true
- output.class:
true
Add option:
const option =output:id: trueclass: false;;
Before:
OMG
After: id name is displayed, and class name is hidden.
OMG<!-- /#wrapper -->
Note: If both are set to false, comments will not be inserted.
output.afterText
output.beforeTextYou can specify the text to insert before and after the comment.
Default
- output.beforeText:
'/'
- output.afterText:
''
Add option:
const option =output:beforeText: 'End of 'afterText: ' !!!!';;
Before:
OMG
After:
OMG<!-- End of .wow !!!! --><!-- End of #wrapper.container !!!! -->
output.classTemplate
output.idTemplateYou can specify how id names and class names are displayed in comments by underscore template format.
Default
- output.idTemplate:
'#<%= attrs.id %>'
- output.classTemplate:
'.<% print(attrs.class.trim().replace(/\\s+/g, ".")); %>'
Note: The variables that can be used in the template are PostHTML AST Node properties.
Add option:
const option =output:idTemplate: ' id: <%= attrs.id.toUpperCase() %>'classTemplate: ' class: <%= attrs.class.trim().replace(/\\s+/g, ", ") %>';;
Before:
OMG
After:
OMG<!-- / class: wow, foooo --><!-- / id: WRAPPER class: container -->
output.template
You can specify the comment format freely by underscore template format.
Note: This option overrides
output.idTemplate
,output.classTemplate
,output.beforeText
, andoutput.afterText
.
Default
- output.template:
false
Add option:
const option =output:template: '<% if (attrs.id) { %>=== end of <%= attrs.id %> ===<% } %>';;
Before:
OMG
After:
OMG<!-- === end of wrapper === -->
Note: If the compiled text is empty, comments are not inserted.
output.compiler
You can freely customize the comment contents with the function you created.
Note: This option overrides
output.template
,output.idTemplate
,output.classTemplate
,output.beforeText
, andoutput.afterText
.
Default
- output.compiler:
false
Add option:
{return {if !nodeattrs || !nodeattrsclassreturn '';if nodeattrsclassreturn `👈 This Element has . !!!`;return '';};}const option =output:compiler:;;
Before:
OMG
After:
OMG<!-- 👈 This Element has .wow !!! -->
Note: If the compiled text is empty, comments are not inserted.
output.replaceAdjacentHyphens
You can specify whether to replace adjacent hyphens.
Default
- output.replaceAdjacentHyphens:
false
Note: In WHATWG 's HTML, it is now allowed to accept adjacent hyphens in comments. (Update commit of 2016-06-21)
Add option:
const option =output:replaceAdjacentHyphens: true;;
Before:
OMG
After: If true
is specified, it is replaced with '__'.
OMG<!-- /.btn.btn__large --><!-- #wrapper.container -->
Add option:
const option =output:replaceAdjacentHyphens: '~~';;
After:
OMG<!-- /.btn.btn~~large --><!-- #wrapper.container -->
targetAttribute
Insert comments only on elements with specified attributes.
Default
- targetAttribute:
false
Add option:
const option =targetAttribute: 'data-posthtml-comment-after';;
Before:
After:
<!-- /.block --><!-- /.block__elem -->
match
You can specify expression to match the node.
Default
- match:
false
Add option:
const option =match:attrs:class: /^.+$/ // match class not including '__'.;;
Before:
After: comment is inserted only in BEM Block
<!-- /.block --><!-- /.block.block--mod -->
Contributing
See PostHTML Guidelines and contribution guide.