heading.js 804 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var repeat = require('repeat-string');
  3. module.exports = heading;
  4. /* Stringify a heading.
  5. *
  6. * In `setext: true` mode and when `depth` is smaller than
  7. * three, creates a setext header:
  8. *
  9. * Foo
  10. * ===
  11. *
  12. * Otherwise, an ATX header is generated:
  13. *
  14. * ### Foo
  15. *
  16. * In `closeAtx: true` mode, the header is closed with
  17. * hashes:
  18. *
  19. * ### Foo ###
  20. */
  21. function heading(node) {
  22. var self = this;
  23. var depth = node.depth;
  24. var setext = self.options.setext;
  25. var closeAtx = self.options.closeAtx;
  26. var content = self.all(node).join('');
  27. var prefix;
  28. if (setext && depth < 3) {
  29. return content + '\n' + repeat(depth === 1 ? '=' : '-', content.length);
  30. }
  31. prefix = repeat('#', node.depth);
  32. return prefix + ' ' + content + (closeAtx ? ' ' + prefix : '');
  33. }