index.js 580 B

1234567891011121314151617181920212223242526272829
  1. 'use strict'
  2. var toString = require('mdast-util-to-string')
  3. var visit = require('unist-util-visit')
  4. var slugs = require('github-slugger')()
  5. module.exports = slug
  6. function slug() {
  7. return transformer
  8. }
  9. // Patch slugs on heading nodes.
  10. function transformer(ast) {
  11. slugs.reset()
  12. visit(ast, 'heading', visitor)
  13. function visitor(node) {
  14. var data = node.data || (node.data = {})
  15. var props = data.hProperties || (data.hProperties = {})
  16. var id = props.id
  17. id = id ? slugs.slug(id, true) : slugs.slug(toString(node))
  18. data.id = id
  19. props.id = id
  20. }
  21. }