100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
'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
|
|
}
|