| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 'use strict'
- module.exports = toHast
- var xtend = require('xtend')
- var u = require('unist-builder')
- var visit = require('unist-util-visit')
- var position = require('unist-util-position')
- var generated = require('unist-util-generated')
- var definitions = require('mdast-util-definitions')
- var one = require('./one')
- var footer = require('./footer')
- var handlers = require('./handlers')
- // Factory to transform.
- function factory(tree, options) {
- var settings = options || {}
- var dangerous = settings.allowDangerousHTML
- h.dangerous = dangerous
- h.definition = definitions(tree, settings)
- h.footnotes = []
- h.augment = augment
- h.handlers = xtend(handlers, settings.handlers || {})
- visit(tree, 'footnoteDefinition', visitor)
- return h
- // Finalise the created `right`, a hast node, from `left`, an mdast node.
- function augment(left, right) {
- var data
- var ctx
- // Handle `data.hName`, `data.hProperties, `hChildren`.
- if (left && 'data' in left) {
- data = left.data
- if (right.type === 'element' && data.hName) {
- right.tagName = data.hName
- }
- if (right.type === 'element' && data.hProperties) {
- right.properties = xtend(right.properties, data.hProperties)
- }
- if (right.children && data.hChildren) {
- right.children = data.hChildren
- }
- }
- ctx = left && left.position ? left : {position: left}
- if (!generated(ctx)) {
- right.position = {
- start: position.start(ctx),
- end: position.end(ctx)
- }
- }
- return right
- }
- // Create an element for a `node`.
- function h(node, tagName, props, children) {
- if (
- (children === undefined || children === null) &&
- typeof props === 'object' &&
- 'length' in props
- ) {
- children = props
- props = {}
- }
- return augment(node, {
- type: 'element',
- tagName: tagName,
- properties: props || {},
- children: children || []
- })
- }
- function visitor(definition) {
- h.footnotes.push(definition)
- }
- }
- // Transform `tree`, which is an mdast node, to a hast node.
- function toHast(tree, options) {
- var h = factory(tree, options)
- var node = one(h, tree)
- var footnotes = footer(h)
- if (node && node.children && footnotes) {
- node.children = node.children.concat(u('text', '\n'), footnotes)
- }
- return node
- }
|