mdast-util-to-hast
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
module.exports = require('./lib/index.js'); | ||
'use strict' | ||
module.exports = require('./lib') |
@@ -1,19 +0,19 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = all; | ||
module.exports = all | ||
var trim = require('trim'); | ||
var one = require('./one'); | ||
var trim = require('trim') | ||
var one = require('./one') | ||
/* Transform the children of `parent`. */ | ||
function all(h, parent) { | ||
var nodes = parent.children || []; | ||
var length = nodes.length; | ||
var values = []; | ||
var index = -1; | ||
var result; | ||
var head; | ||
var nodes = parent.children || [] | ||
var length = nodes.length | ||
var values = [] | ||
var index = -1 | ||
var result | ||
var head | ||
while (++index < length) { | ||
result = one(h, nodes[index], parent); | ||
result = one(h, nodes[index], parent) | ||
@@ -23,17 +23,17 @@ if (result) { | ||
if (result.value) { | ||
result.value = trim.left(result.value); | ||
result.value = trim.left(result.value) | ||
} | ||
head = result.children && result.children[0]; | ||
head = result.children && result.children[0] | ||
if (head && head.value) { | ||
head.value = trim.left(head.value); | ||
head.value = trim.left(head.value) | ||
} | ||
} | ||
values = values.concat(result); | ||
values = values.concat(result) | ||
} | ||
} | ||
return values; | ||
return values | ||
} |
@@ -1,7 +0,7 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = failsafe; | ||
module.exports = failsafe | ||
var u = require('unist-builder'); | ||
var all = require('./all'); | ||
var u = require('unist-builder') | ||
var all = require('./all') | ||
@@ -11,11 +11,11 @@ /* Return the content of a reference without definition | ||
function failsafe(h, node, definition) { | ||
var subtype = node.referenceType; | ||
var subtype = node.referenceType | ||
if (subtype !== 'collapsed' && subtype !== 'full' && !definition) { | ||
if (node.type === 'imageReference') { | ||
return u('text', '![' + node.alt + ']'); | ||
return u('text', '![' + node.alt + ']') | ||
} | ||
return [u('text', '[')].concat(all(h, node), u('text', ']')); | ||
return [u('text', '[')].concat(all(h, node), u('text', ']')) | ||
} | ||
} |
@@ -1,23 +0,23 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = generateFootnotes; | ||
module.exports = generateFootnotes | ||
var thematicBreak = require('./handlers/thematic-break'); | ||
var list = require('./handlers/list'); | ||
var wrap = require('./wrap'); | ||
var thematicBreak = require('./handlers/thematic-break') | ||
var list = require('./handlers/list') | ||
var wrap = require('./wrap') | ||
/* Transform all footnote definitions, if any. */ | ||
function generateFootnotes(h) { | ||
var footnotes = h.footnotes; | ||
var length = footnotes.length; | ||
var index = -1; | ||
var listItems = []; | ||
var def; | ||
var footnotes = h.footnotes | ||
var length = footnotes.length | ||
var index = -1 | ||
var listItems = [] | ||
var def | ||
if (!length) { | ||
return null; | ||
return null | ||
} | ||
while (++index < length) { | ||
def = footnotes[index]; | ||
def = footnotes[index] | ||
@@ -31,21 +31,24 @@ listItems[index] = { | ||
data: {hProperties: {className: ['footnote-backref']}}, | ||
children: [{ | ||
type: 'text', | ||
value: '↩' | ||
}] | ||
children: [{type: 'text', value: '↩'}] | ||
}), | ||
position: def.position | ||
}; | ||
} | ||
} | ||
return h(null, 'div', { | ||
className: ['footnotes'] | ||
}, wrap([ | ||
thematicBreak(h), | ||
list(h, { | ||
type: 'list', | ||
ordered: true, | ||
children: listItems | ||
}) | ||
], true)); | ||
return h( | ||
null, | ||
'div', | ||
{className: ['footnotes']}, | ||
wrap( | ||
[ | ||
thematicBreak(h), | ||
list(h, { | ||
type: 'list', | ||
ordered: true, | ||
children: listItems | ||
}) | ||
], | ||
true | ||
) | ||
) | ||
} |
@@ -1,11 +0,11 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = blockquote; | ||
module.exports = blockquote | ||
var wrap = require('../wrap'); | ||
var all = require('../all'); | ||
var wrap = require('../wrap') | ||
var all = require('../all') | ||
/* Transform a block quote. */ | ||
function blockquote(h, node) { | ||
return h(node, 'blockquote', wrap(all(h, node), true)); | ||
return h(node, 'blockquote', wrap(all(h, node), true)) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = hardBreak; | ||
module.exports = hardBreak | ||
var u = require('unist-builder'); | ||
var u = require('unist-builder') | ||
/* Transform an inline break. */ | ||
function hardBreak(h, node) { | ||
return [h(node, 'br'), u('text', '\n')]; | ||
return [h(node, 'br'), u('text', '\n')] | ||
} |
@@ -1,21 +0,19 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = code; | ||
module.exports = code | ||
var detab = require('detab'); | ||
var u = require('unist-builder'); | ||
var detab = require('detab') | ||
var u = require('unist-builder') | ||
/* Transform a code block. */ | ||
function code(h, node) { | ||
var value = node.value ? detab(node.value + '\n') : ''; | ||
var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/); | ||
var props = {}; | ||
var value = node.value ? detab(node.value + '\n') : '' | ||
var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/) | ||
var props = {} | ||
if (lang) { | ||
props.className = ['language-' + lang]; | ||
props.className = ['language-' + lang] | ||
} | ||
return h(node.position, 'pre', [ | ||
h(node, 'code', props, [u('text', value)]) | ||
]); | ||
return h(node.position, 'pre', [h(node, 'code', props, [u('text', value)])]) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = strikethrough; | ||
module.exports = strikethrough | ||
var all = require('../all'); | ||
var all = require('../all') | ||
/* Transform deletions. */ | ||
function strikethrough(h, node) { | ||
return h(node, 'del', all(h, node)); | ||
return h(node, 'del', all(h, node)) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = emphasis; | ||
module.exports = emphasis | ||
var all = require('../all'); | ||
var all = require('../all') | ||
/* Transform emphasis. */ | ||
function emphasis(h, node) { | ||
return h(node, 'em', all(h, node)); | ||
return h(node, 'em', all(h, node)) | ||
} |
@@ -1,17 +0,16 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = footnoteReference; | ||
module.exports = footnoteReference | ||
var u = require('unist-builder'); | ||
var u = require('unist-builder') | ||
/* Transform a reference to a footnote. */ | ||
function footnoteReference(h, node) { | ||
var identifier = node.identifier; | ||
var identifier = node.identifier | ||
return h(node.position, 'sup', {id: 'fnref-' + identifier}, [ | ||
h(node, 'a', { | ||
href: '#fn-' + identifier, | ||
className: ['footnote-ref'] | ||
}, [u('text', identifier)]) | ||
]); | ||
h(node, 'a', {href: '#fn-' + identifier, className: ['footnote-ref']}, [ | ||
u('text', identifier) | ||
]) | ||
]) | ||
} |
@@ -1,24 +0,24 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = footnote; | ||
module.exports = footnote | ||
var footnoteReference = require('./footnote-reference'); | ||
var footnoteReference = require('./footnote-reference') | ||
/* Transform an inline footnote. */ | ||
function footnote(h, node) { | ||
var identifiers = []; | ||
var identifier = 1; | ||
var footnotes = h.footnotes; | ||
var length = footnotes.length; | ||
var index = -1; | ||
var identifiers = [] | ||
var identifier = 1 | ||
var footnotes = h.footnotes | ||
var length = footnotes.length | ||
var index = -1 | ||
while (++index < length) { | ||
identifiers[index] = footnotes[index].identifier; | ||
identifiers[index] = footnotes[index].identifier | ||
} | ||
while (identifiers.indexOf(String(identifier)) !== -1) { | ||
identifier++; | ||
identifier++ | ||
} | ||
identifier = String(identifier); | ||
identifier = String(identifier) | ||
@@ -28,8 +28,5 @@ footnotes.push({ | ||
identifier: identifier, | ||
children: [{ | ||
type: 'paragraph', | ||
children: node.children | ||
}], | ||
children: [{type: 'paragraph', children: node.children}], | ||
position: node.position | ||
}); | ||
}) | ||
@@ -40,3 +37,3 @@ return footnoteReference(h, { | ||
position: node.position | ||
}); | ||
}) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = heading; | ||
module.exports = heading | ||
var all = require('../all'); | ||
var all = require('../all') | ||
/* Transform a heading. */ | ||
function heading(h, node) { | ||
return h(node, 'h' + node.depth, all(h, node)); | ||
return h(node, 'h' + node.depth, all(h, node)) | ||
} |
@@ -1,6 +0,6 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = html; | ||
module.exports = html | ||
var u = require('unist-builder'); | ||
var u = require('unist-builder') | ||
@@ -10,3 +10,3 @@ /* Return either a `raw` node, in dangerous mode, or | ||
function html(h, node) { | ||
return h.dangerous ? h.augment(node, u('raw', node.value)) : null; | ||
return h.dangerous ? h.augment(node, u('raw', node.value)) : null | ||
} |
@@ -1,18 +0,18 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = imageReference; | ||
module.exports = imageReference | ||
var normalize = require('mdurl/encode'); | ||
var failsafe = require('../failsafe'); | ||
var normalize = require('mdurl/encode') | ||
var failsafe = require('../failsafe') | ||
/* Transform a reference to an image. */ | ||
function imageReference(h, node) { | ||
var def = h.definition(node.identifier); | ||
var props = {src: normalize((def && def.url) || ''), alt: node.alt}; | ||
var def = h.definition(node.identifier) | ||
var props = {src: normalize((def && def.url) || ''), alt: node.alt} | ||
if (def && def.title !== null && def.title !== undefined) { | ||
props.title = def.title; | ||
props.title = def.title | ||
} | ||
return failsafe(h, node, def) || h(node, 'img', props); | ||
return failsafe(h, node, def) || h(node, 'img', props) | ||
} |
@@ -1,16 +0,16 @@ | ||
'use strict'; | ||
'use strict' | ||
var normalize = require('mdurl/encode'); | ||
var normalize = require('mdurl/encode') | ||
module.exports = image; | ||
module.exports = image | ||
/* Transform an image. */ | ||
function image(h, node) { | ||
var props = {src: normalize(node.url), alt: node.alt}; | ||
var props = {src: normalize(node.url), alt: node.alt} | ||
if (node.title !== null && node.title !== undefined) { | ||
props.title = node.title; | ||
props.title = node.title | ||
} | ||
return h(node, 'img', props); | ||
return h(node, 'img', props) | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
'use strict' | ||
@@ -30,7 +30,7 @@ module.exports = { | ||
footnoteDefinition: ignore | ||
}; | ||
} | ||
/* Return nothing for nodes which are ignored. */ | ||
function ignore() { | ||
return null; | ||
return null | ||
} |
@@ -1,11 +0,11 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = inlineCode; | ||
module.exports = inlineCode | ||
var collapse = require('collapse-white-space'); | ||
var u = require('unist-builder'); | ||
var collapse = require('collapse-white-space') | ||
var u = require('unist-builder') | ||
/* Transform inline code. */ | ||
function inlineCode(h, node) { | ||
return h(node, 'code', [u('text', collapse(node.value))]); | ||
return h(node, 'code', [u('text', collapse(node.value))]) | ||
} |
@@ -1,19 +0,19 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = linkReference; | ||
module.exports = linkReference | ||
var normalize = require('mdurl/encode'); | ||
var failsafe = require('../failsafe'); | ||
var all = require('../all'); | ||
var normalize = require('mdurl/encode') | ||
var failsafe = require('../failsafe') | ||
var all = require('../all') | ||
/* Transform a reference to a link. */ | ||
function linkReference(h, node) { | ||
var def = h.definition(node.identifier); | ||
var props = {href: normalize((def && def.url) || '')}; | ||
var def = h.definition(node.identifier) | ||
var props = {href: normalize((def && def.url) || '')} | ||
if (def && def.title !== null && def.title !== undefined) { | ||
props.title = def.title; | ||
props.title = def.title | ||
} | ||
return failsafe(h, node, def) || h(node, 'a', props, all(h, node)); | ||
return failsafe(h, node, def) || h(node, 'a', props, all(h, node)) | ||
} |
@@ -1,17 +0,17 @@ | ||
'use strict'; | ||
'use strict' | ||
var normalize = require('mdurl/encode'); | ||
var all = require('../all'); | ||
var normalize = require('mdurl/encode') | ||
var all = require('../all') | ||
module.exports = link; | ||
module.exports = link | ||
/* Transform a link. */ | ||
function link(h, node) { | ||
var props = {href: normalize(node.url)}; | ||
var props = {href: normalize(node.url)} | ||
if (node.title !== null && node.title !== undefined) { | ||
props.title = node.title; | ||
props.title = node.title | ||
} | ||
return h(node, 'a', props, all(h, node)); | ||
return h(node, 'a', props, all(h, node)) | ||
} |
@@ -1,50 +0,56 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = listItem; | ||
module.exports = listItem | ||
var u = require('unist-builder'); | ||
var wrap = require('../wrap'); | ||
var all = require('../all'); | ||
var u = require('unist-builder') | ||
var wrap = require('../wrap') | ||
var all = require('../all') | ||
/* Transform a list-item. */ | ||
function listItem(h, node, parent) { | ||
var children = node.children; | ||
var head = children[0]; | ||
var props = {}; | ||
var single = false; | ||
var result; | ||
var container; | ||
var children = node.children | ||
var head = children[0] | ||
var props = {} | ||
var single = false | ||
var result | ||
var container | ||
if ((!parent || !parent.loose) && children.length === 1 && head.type === 'paragraph') { | ||
single = true; | ||
if ( | ||
(!parent || !parent.loose) && | ||
children.length === 1 && | ||
head.type === 'paragraph' | ||
) { | ||
single = true | ||
} | ||
result = all(h, single ? head : node); | ||
result = all(h, single ? head : node) | ||
if (typeof node.checked === 'boolean') { | ||
if (!single && (!head || head.type !== 'paragraph')) { | ||
result.unshift(h(null, 'p', [])); | ||
result.unshift(h(null, 'p', [])) | ||
} | ||
container = single ? result : result[0].children; | ||
container = single ? result : result[0].children | ||
if (container.length !== 0) { | ||
container.unshift(u('text', ' ')); | ||
container.unshift(u('text', ' ')) | ||
} | ||
container.unshift(h(null, 'input', { | ||
type: 'checkbox', | ||
checked: node.checked, | ||
disabled: true | ||
})); | ||
container.unshift( | ||
h(null, 'input', { | ||
type: 'checkbox', | ||
checked: node.checked, | ||
disabled: true | ||
}) | ||
) | ||
/* According to github-markdown-css, this class hides bullet. */ | ||
props.className = ['task-list-item']; | ||
props.className = ['task-list-item'] | ||
} | ||
if (!single && result.length !== 0) { | ||
result = wrap(result, true); | ||
result = wrap(result, true) | ||
} | ||
return h(node, 'li', props, result); | ||
return h(node, 'li', props, result) | ||
} |
@@ -1,18 +0,18 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = list; | ||
module.exports = list | ||
var wrap = require('../wrap'); | ||
var all = require('../all'); | ||
var wrap = require('../wrap') | ||
var all = require('../all') | ||
/* Transform a list. */ | ||
function list(h, node) { | ||
var props = {}; | ||
var name = node.ordered ? 'ol' : 'ul'; | ||
var props = {} | ||
var name = node.ordered ? 'ol' : 'ul' | ||
if (typeof node.start === 'number' && node.start !== 1) { | ||
props.start = node.start; | ||
props.start = node.start | ||
} | ||
return h(node, name, props, wrap(all(h, node), true)); | ||
return h(node, name, props, wrap(all(h, node), true)) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = paragraph; | ||
module.exports = paragraph | ||
var all = require('../all'); | ||
var all = require('../all') | ||
/* Transform a paragraph. */ | ||
function paragraph(h, node) { | ||
return h(node, 'p', all(h, node)); | ||
return h(node, 'p', all(h, node)) | ||
} |
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = root; | ||
module.exports = root | ||
var u = require('unist-builder'); | ||
var wrap = require('../wrap'); | ||
var all = require('../all'); | ||
var u = require('unist-builder') | ||
var wrap = require('../wrap') | ||
var all = require('../all') | ||
/* Transform a `root`. */ | ||
function root(h, node) { | ||
return h.augment(node, u('root', wrap(all(h, node)))); | ||
return h.augment(node, u('root', wrap(all(h, node)))) | ||
} |
@@ -1,10 +0,10 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = strong; | ||
module.exports = strong | ||
var all = require('../all'); | ||
var all = require('../all') | ||
/* Transform importance. */ | ||
function strong(h, node) { | ||
return h(node, 'strong', all(h, node)); | ||
return h(node, 'strong', all(h, node)) | ||
} |
@@ -1,45 +0,54 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = table; | ||
module.exports = table | ||
var position = require('unist-util-position'); | ||
var wrap = require('../wrap'); | ||
var all = require('../all'); | ||
var position = require('unist-util-position') | ||
var wrap = require('../wrap') | ||
var all = require('../all') | ||
/* Transform a table. */ | ||
function table(h, node) { | ||
var rows = node.children; | ||
var index = rows.length; | ||
var align = node.align; | ||
var alignLength = align.length; | ||
var result = []; | ||
var pos; | ||
var row; | ||
var out; | ||
var name; | ||
var cell; | ||
var rows = node.children | ||
var index = rows.length | ||
var align = node.align | ||
var alignLength = align.length | ||
var result = [] | ||
var pos | ||
var row | ||
var out | ||
var name | ||
var cell | ||
while (index--) { | ||
row = rows[index].children; | ||
name = index === 0 ? 'th' : 'td'; | ||
pos = alignLength; | ||
out = []; | ||
row = rows[index].children | ||
name = index === 0 ? 'th' : 'td' | ||
pos = alignLength | ||
out = [] | ||
while (pos--) { | ||
cell = row[pos]; | ||
out[pos] = h(cell, name, { | ||
align: align[pos] | ||
}, cell ? wrap(all(h, cell)) : []); | ||
cell = row[pos] | ||
out[pos] = h(cell, name, {align: align[pos]}, cell ? all(h, cell) : []) | ||
} | ||
result[index] = h(rows[index], 'tr', wrap(out, true)); | ||
result[index] = h(rows[index], 'tr', wrap(out, true)) | ||
} | ||
return h(node, 'table', wrap([ | ||
h(result[0].position, 'thead', wrap([result[0]], true)), | ||
h({ | ||
start: position.start(result[1]), | ||
end: position.end(result[result.length - 1]) | ||
}, 'tbody', wrap(result.slice(1), true)) | ||
], true)); | ||
return h( | ||
node, | ||
'table', | ||
wrap( | ||
[ | ||
h(result[0].position, 'thead', wrap([result[0]], true)), | ||
h( | ||
{ | ||
start: position.start(result[1]), | ||
end: position.end(result[result.length - 1]) | ||
}, | ||
'tbody', | ||
wrap(result.slice(1), true) | ||
) | ||
], | ||
true | ||
) | ||
) | ||
} |
@@ -1,11 +0,11 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = text; | ||
module.exports = text | ||
var u = require('unist-builder'); | ||
var trimLines = require('trim-lines'); | ||
var u = require('unist-builder') | ||
var trimLines = require('trim-lines') | ||
/* Transform text. */ | ||
function text(h, node) { | ||
return h.augment(node, u('text', trimLines(node.value))); | ||
return h.augment(node, u('text', trimLines(node.value))) | ||
} |
@@ -1,8 +0,8 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = thematicBreak; | ||
module.exports = thematicBreak | ||
/* Transform a thematic break / horizontal rule. */ | ||
function thematicBreak(h, node) { | ||
return h(node, 'hr'); | ||
return h(node, 'hr') | ||
} |
@@ -1,29 +0,29 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = toHAST; | ||
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'); | ||
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; | ||
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 || {})); | ||
h.dangerous = dangerous | ||
h.definition = definitions(tree, settings) | ||
h.footnotes = [] | ||
h.augment = augment | ||
h.handlers = xtend(handlers, settings.handlers || {}) | ||
visit(tree, 'footnoteDefinition', visitor); | ||
visit(tree, 'footnoteDefinition', visitor) | ||
return h; | ||
return h | ||
@@ -33,23 +33,23 @@ /* Finalise the created `right`, a HAST node, from | ||
function augment(left, right) { | ||
var data; | ||
var ctx; | ||
var data | ||
var ctx | ||
/* Handle `data.hName`, `data.hProperties, `hChildren`. */ | ||
if (left && 'data' in left) { | ||
data = left.data; | ||
data = left.data | ||
if (right.type === 'element' && data.hName) { | ||
right.tagName = data.hName; | ||
right.tagName = data.hName | ||
} | ||
if (right.type === 'element' && data.hProperties) { | ||
right.properties = xtend(right.properties, data.hProperties); | ||
right.properties = xtend(right.properties, data.hProperties) | ||
} | ||
if (right.children && data.hChildren) { | ||
right.children = data.hChildren; | ||
right.children = data.hChildren | ||
} | ||
} | ||
ctx = left && left.position ? left : {position: left}; | ||
ctx = left && left.position ? left : {position: left} | ||
@@ -60,6 +60,6 @@ if (!generated(ctx)) { | ||
end: position.end(ctx) | ||
}; | ||
} | ||
} | ||
return right; | ||
return right | ||
} | ||
@@ -74,4 +74,4 @@ | ||
) { | ||
children = props; | ||
props = {}; | ||
children = props | ||
props = {} | ||
} | ||
@@ -84,7 +84,7 @@ | ||
children: children || [] | ||
}); | ||
}) | ||
} | ||
function visitor(definition) { | ||
h.footnotes.push(definition); | ||
h.footnotes.push(definition) | ||
} | ||
@@ -95,11 +95,11 @@ } | ||
function toHAST(tree, options) { | ||
var h = factory(tree, options); | ||
var node = one(h, tree); | ||
var footnotes = footer(h); | ||
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); | ||
node.children = node.children.concat(u('text', '\n'), footnotes) | ||
} | ||
return node; | ||
return node | ||
} |
@@ -1,9 +0,9 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = one; | ||
module.exports = one | ||
var u = require('unist-builder'); | ||
var all = require('./all'); | ||
var u = require('unist-builder') | ||
var all = require('./all') | ||
var own = {}.hasOwnProperty; | ||
var own = {}.hasOwnProperty | ||
@@ -13,6 +13,6 @@ /* Transform an unknown node. */ | ||
if (text(node)) { | ||
return h.augment(node, u('text', node.value)); | ||
return h.augment(node, u('text', node.value)) | ||
} | ||
return h(node, 'div', all(h, node)); | ||
return h(node, 'div', all(h, node)) | ||
} | ||
@@ -22,11 +22,11 @@ | ||
function one(h, node, parent) { | ||
var type = node && node.type; | ||
var fn = own.call(h.handlers, type) ? h.handlers[type] : null; | ||
var type = node && node.type | ||
var fn = own.call(h.handlers, type) ? h.handlers[type] : null | ||
/* Fail on non-nodes. */ | ||
if (!type) { | ||
throw new Error('Expected node, got `' + node + '`'); | ||
throw new Error('Expected node, got `' + node + '`') | ||
} | ||
return (typeof fn === 'function' ? fn : unknown)(h, node, parent); | ||
return (typeof fn === 'function' ? fn : unknown)(h, node, parent) | ||
} | ||
@@ -36,9 +36,13 @@ | ||
function text(node) { | ||
var data = node.data || {}; | ||
var data = node.data || {} | ||
if (own.call(data, 'hName') || own.call(data, 'hProperties') || own.call(data, 'hChildren')) { | ||
return false; | ||
if ( | ||
own.call(data, 'hName') || | ||
own.call(data, 'hProperties') || | ||
own.call(data, 'hChildren') | ||
) { | ||
return false | ||
} | ||
return 'value' in node; | ||
return 'value' in node | ||
} |
@@ -1,6 +0,6 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = wrap; | ||
module.exports = wrap | ||
var u = require('unist-builder'); | ||
var u = require('unist-builder') | ||
@@ -10,8 +10,8 @@ /* Wrap `nodes` with newlines between each entry. | ||
function wrap(nodes, loose) { | ||
var result = []; | ||
var index = -1; | ||
var length = nodes.length; | ||
var result = [] | ||
var index = -1 | ||
var length = nodes.length | ||
if (loose) { | ||
result.push(u('text', '\n')); | ||
result.push(u('text', '\n')) | ||
} | ||
@@ -21,13 +21,13 @@ | ||
if (index) { | ||
result.push(u('text', '\n')); | ||
result.push(u('text', '\n')) | ||
} | ||
result.push(nodes[index]); | ||
result.push(nodes[index]) | ||
} | ||
if (loose && nodes.length !== 0) { | ||
result.push(u('text', '\n')); | ||
result.push(u('text', '\n')) | ||
} | ||
return result; | ||
return result | ||
} |
{ | ||
"name": "mdast-util-to-hast", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Transform MDAST to HAST", | ||
@@ -35,24 +35,34 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"browserify": "^16.0.0", | ||
"esmangle": "^1.0.1", | ||
"nyc": "^11.0.0", | ||
"remark-cli": "^4.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"nyc": "^12.0.0", | ||
"prettier": "^1.13.3", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.0.0", | ||
"xo": "^0.18.0" | ||
"xo": "^0.21.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . -qfo", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify index.js --bare -s mdastUtilToHAST > mdast-util-to-hast.js", | ||
"build-mangle": "esmangle mdast-util-to-hast.js > mdast-util-to-hast.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"guard-for-in": "off" | ||
@@ -59,0 +69,0 @@ }, |
167
readme.md
@@ -5,3 +5,3 @@ # mdast-util-to-hast [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
> **Note** You probably want to use [remark-rehype][]. | ||
> **Note**: You probably want to use [remark-rehype][]. | ||
@@ -24,14 +24,16 @@ ## Installation | ||
...and next to it, `example.js`: | ||
…and next to it, `example.js`: | ||
```javascript | ||
var inspect = require('unist-util-inspect'); | ||
var unified = require('unified'); | ||
var parse = require('remark-parse'); | ||
var vfile = require('to-vfile'); | ||
var toHAST = require('mdast-util-to-hast'); | ||
var inspect = require('unist-util-inspect') | ||
var unified = require('unified') | ||
var parse = require('remark-parse') | ||
var vfile = require('to-vfile') | ||
var toHAST = require('mdast-util-to-hast') | ||
var tree = unified().use(parse).parse(vfile.readSync('example.md')); | ||
var tree = unified() | ||
.use(parse) | ||
.parse(vfile.readSync('example.md')) | ||
console.log(inspect(toHAST(tree))); | ||
console.log(inspect(toHAST(tree))) | ||
``` | ||
@@ -56,7 +58,8 @@ | ||
##### Options | ||
###### `options.allowDangerousHTML` | ||
Whether to allow `html` nodes and inject them as raw HTML (`boolean`, | ||
default: `false`). Only do this when compiling later with | ||
`hast-util-to-html`. | ||
Whether to allow `html` nodes and inject them as raw HTML (`boolean`, default: | ||
`false`). Only do this when compiling later with `hast-util-to-html`. | ||
@@ -70,22 +73,126 @@ ###### `options.commonmark` | ||
Object mapping [MDAST nodes][mdast] to functions | ||
handling those elements. | ||
Object mapping [MDAST nodes][mdast] to functions handling those elements. | ||
Take a look at [`lib/handlers/`][handlers] for examples. | ||
###### Returns | ||
##### Returns | ||
[`HASTNode`][hast]. | ||
###### Note | ||
##### Notes | ||
* [`yaml`][mdast-yaml] and [`html`][mdast-html] nodes are ignored | ||
* `yaml` and `toml` nodes are ignored | ||
* [`html`][mdast-html] nodes are ignored if `allowDangerousHTML` is `false` | ||
* [`position`][unist-position]s are properly patched | ||
* Unknown nodes with `children` are transformed to `div` elements | ||
* Unknown nodes with `value` are transformed to `text` nodes | ||
* If `node.data.hName` is set, it’s used as the HAST element’s tag-name | ||
* If `node.data.hProperties` is set, it’s mixed into the HAST element’s | ||
* [`node.data.hName`][hname] configures the HAST element’s tag-name | ||
* [`node.data.hProperties`][hproperties] is mixed into the HAST element’s | ||
properties | ||
* If `node.data.hChildren` is set, it’s used as the element’s HAST | ||
children | ||
* [`node.data.hChildren`][hchildren] configures the HAST element’s children | ||
##### Examples | ||
###### `hName` | ||
`node.data.hName` sets the tag-name of an element. | ||
The following [MDAST][]: | ||
```js | ||
{ | ||
type: 'strong', | ||
data: {hName: 'b'}, | ||
children: [{type: 'text', value: 'Alpha'}] | ||
} | ||
``` | ||
Yields, in [HAST][]: | ||
```js | ||
{ | ||
type: 'element', | ||
tagName: 'b', | ||
properties: {}, | ||
children: [{type: 'text', value: 'Alpha'}] | ||
} | ||
``` | ||
###### `hProperties` | ||
`node.data.hProperties` in sets the properties of an element. | ||
The following [MDAST][]: | ||
```js | ||
{ | ||
type: 'image', | ||
src: 'circle.svg', | ||
alt: 'Big red circle on a black background', | ||
title: null | ||
data: {hProperties: {className: ['responsive']}} | ||
} | ||
``` | ||
Yields, in [HAST][]: | ||
```js | ||
{ | ||
type: 'element', | ||
tagName: 'img', | ||
properties: { | ||
src: 'circle.svg', | ||
alt: 'Big red circle on a black background', | ||
className: ['responsive'] | ||
}, | ||
children: [] | ||
} | ||
``` | ||
###### `hChildren` | ||
`node.data.hChildren` sets the children of an element. | ||
The following [MDAST][]: | ||
```js | ||
{ | ||
type: 'code', | ||
lang: 'js', | ||
data: { | ||
hChildren: [ | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: {className: ['hljs-meta']}, | ||
children: [{type: 'text', value: '"use strict"'}] | ||
}, | ||
{type: 'text', value: ';'} | ||
] | ||
}, | ||
value: '"use strict";' | ||
} | ||
``` | ||
Yields, in [HAST][] (**note**: the `pre` and `language-js` class are normal | ||
`mdast-util-to-hast` functionality): | ||
```js | ||
{ | ||
type: 'element', | ||
tagName: 'pre', | ||
properties: {}, | ||
children: [{ | ||
type: 'element', | ||
tagName: 'code', | ||
properties: {className: ['language-js']}, | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: {className: ['hljs-meta']}, | ||
children: [{type: 'text', value: '"use strict"'}] | ||
}, | ||
{type: 'text', value: ';'} | ||
] | ||
}] | ||
} | ||
``` | ||
## Related | ||
@@ -99,5 +206,5 @@ | ||
— Transform HAST to MDAST | ||
* [`remark-rehype`](https://github.com/wooorm/remark-rehype) | ||
* [`remark-rehype`](https://github.com/remarkjs/remark-rehype) | ||
— rehype support for remark | ||
* [`rehype-remark`](https://github.com/wooorm/remark-remark) | ||
* [`rehype-remark`](https://github.com/rehypejs/rehype-remark) | ||
— remark support for rehype | ||
@@ -107,3 +214,3 @@ | ||
See [`contribute.md` in `syntax-tree/mdast`][contribute] for ways to get | ||
See [`contributing.md` in `syntax-tree/mdast`][contributing] for ways to get | ||
started. | ||
@@ -138,4 +245,2 @@ | ||
[mdast-yaml]: https://github.com/syntax-tree/mdast#yaml | ||
[mdast-html]: https://github.com/syntax-tree/mdast#html | ||
@@ -147,6 +252,12 @@ | ||
[remark-rehype]: https://github.com/wooorm/remark-rehype | ||
[remark-rehype]: https://github.com/remarkjs/remark-rehype | ||
[contribute]: https://github.com/syntax-tree/mdast/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/mdast/blob/master/contributing.md | ||
[coc]: https://github.com/syntax-tree/mdast/blob/master/code-of-conduct.md | ||
[hname]: #hname | ||
[hproperties]: #hproperties | ||
[hchildren]: #hchildren |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24225
560
256
8
1