npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

posthtml-comment-after

v0.5.4

Published

A PostHTML plug-in that adds comments after HTML elements.

Downloads

5

Readme

PostHTML Comment After Plugin

NPM Deps Build Cover Standard Code Style license MIT

A PostHTML plug-in that adds comments after HTML elements.

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p><!-- /.wow -->
    </div><!-- /#wrapper.container -->
  </body>
</html>

Install

npm install posthtml posthtml-comment-after --save-dev

Usage

const fs = require('fs');
const posthtml = require('posthtml');
const commentAfter = require('posthtml-comment-after');

posthtml()
  .use(commentAfter())
  .process(html)
  .then(result => fs.writeFileSync('./after.html', result.html));

Options

output.sameline

You can specify whether to insert comments on the same line.

Default

  • output.sameline: true

Add option:

const option = {
  output: {
    sameline: false
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div>
  </body>
</html>

After: comment is inserted after a line break.

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
      <!-- /.wow -->
    </div>
    <!-- /#wrapper.container -->
  </body>
</html>

output.idoutput.class

You 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: true,
    class: false
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => fs.writeFileSync('./after.html', result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div>
  </body>
</html>

After: id name is displayed, and class name is hidden.

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div><!-- /#wrapper -->
  </body>
</html>

Note: If both are set to false, comments will not be inserted.

output.beforeTextoutput.afterText

You 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: ' !!!!' 
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => fs.writeFileSync('./after.html', result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p><!-- End of .wow !!!! -->
    </div><!-- End of #wrapper.container !!!! -->
  </body>
</html>

output.idTemplateoutput.classTemplate

You 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, ", ") %>' 
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow foooo">OMG</p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow foooo">OMG</p><!-- / class: wow, foooo -->
    </div><!-- / id: WRAPPER class: container -->
  </body>
</html>

output.template

You can specify the comment format freely by underscore template format.

Note: This option overrides output.idTemplate, output.classTemplate, output.beforeText, and output.afterText.

Default

  • output.template: false

Add option:

const option = {
  output: {
    template: '<% if (attrs.id) { %>=== end of <%= attrs.id %> ===<% } %>'
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow">OMG</p>
    </div><!-- === end of wrapper === -->
  </body>
</html>

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, and output.afterText.

Default

  • output.compiler: false

Add option:

function myCompiler (className) {

  return function (node) {
    if (!node.attrs || !node.attrs.class) {
      return '';
    }
    
    if (node.attrs.class.split(' ').includes(className)) {
      return `👈 This Element has .${className} !!!`;
    }
    return '';
  };

}

const option = {
  output: {
    compiler: myCompiler('wow')
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow foooo">OMG</p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <p class="wow foooo">OMG</p><!-- 👈 This Element has .wow !!! -->
    </div>
  </body>
</html>

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
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div id="wrapper" class="container">
      <a class="btn btn--large">OMG</a>
    </div>
  </body>
</html>

After: If true is specified, it is replaced with '__'.

<html>
  <body>
    <div id="wrapper" class="container">
      <a class="btn btn--large">OMG</a><!-- /.btn.btn__large -->
    </div><!-- #wrapper.container -->
  </body>
</html>

Add option:

const option = {
  output: {
    replaceAdjacentHyphens: '~~'
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

After:

<html>
  <body>
    <div id="wrapper" class="container">
      <a class="btn btn--large">OMG</a><!-- /.btn.btn~~large -->
    </div><!-- #wrapper.container -->
  </body>
</html>

targetAttribute

Insert comments only on elements with specified attributes.

Default

  • targetAttribute: false

Add option:

const option = {
  targetAttribute: 'data-posthtml-comment-after'
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div class="block" data-posthtml-comment-after>
      <p class="block__elem"></p>
    </div>
    <div class="block block--mod">
      <p class="block__elem" data-posthtml-comment-after></p>
      <p class="block__elem"></p>
    </div>
  </body>
</html>

After:

<html>
  <body>
    <div class="block">
      <p class="block__elem"></p>
    </div><!-- /.block -->
    <div class="block block--mod">
      <p class="block__elem"></p><!-- /.block__elem -->
      <p class="block__elem"></p>
    </div>
  </body>
</html>

match

You can specify expression to match the node.

Default

  • match: false

Add option:

const option = {
  match: {
    attrs: {
      class: /^(?!.*__).+$/ // match class not including '__'.
    }
  }
};

posthtml()
  .use(commentAfter(option))
  .process(html)
  .then(result => console.log(result.html));

Before:

<html>
  <body>
    <div class="block">
      <p class="block__elem"></p>
    </div>
    <div class="block block--mod">
      <p class="block__elem"></p>
      <p class="block__elem"></p>
    </div>
  </body>
</html>

After: comment is inserted only in BEM Block

<html>
  <body>
    <div class="block">
      <p class="block__elem"></p>
    </div><!-- /.block -->
    <div class="block block--mod">
      <p class="block__elem"></p>
      <p class="block__elem"></p>
    </div><!-- /.block.block--mod -->
  </body>
</html>

Contributing

See PostHTML Guidelines and contribution guide.

License

MIT