one.js 886 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. module.exports = one
  3. var u = require('unist-builder')
  4. var all = require('./all')
  5. var own = {}.hasOwnProperty
  6. // Transform an unknown node.
  7. function unknown(h, node) {
  8. if (text(node)) {
  9. return h.augment(node, u('text', node.value))
  10. }
  11. return h(node, 'div', all(h, node))
  12. }
  13. // Visit a node.
  14. function one(h, node, parent) {
  15. var type = node && node.type
  16. var fn = own.call(h.handlers, type) ? h.handlers[type] : null
  17. // Fail on non-nodes.
  18. if (!type) {
  19. throw new Error('Expected node, got `' + node + '`')
  20. }
  21. return (typeof fn === 'function' ? fn : unknown)(h, node, parent)
  22. }
  23. // Check if the node should be renderered a text node.
  24. function text(node) {
  25. var data = node.data || {}
  26. if (
  27. own.call(data, 'hName') ||
  28. own.call(data, 'hProperties') ||
  29. own.call(data, 'hChildren')
  30. ) {
  31. return false
  32. }
  33. return 'value' in node
  34. }