-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
42 lines (32 loc) · 957 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
var toHast = require('mdast-util-to-hast')
var toHtml = require('hast-util-to-html')
var sanitize = require('hast-util-sanitize')
module.exports = plugin
function plugin(options) {
var settings = options || {}
var clean = settings.sanitize
var schema = clean && typeof clean === 'object' ? clean : null
var handlers = settings.handlers || {}
this.Compiler = compiler
function compiler(node, file) {
var root = node && node.type && node.type === 'root'
var hast = toHast(node, {allowDangerousHtml: !clean, handlers: handlers})
var result
if (file.extname) {
file.extname = '.html'
}
if (clean) {
hast = sanitize(hast, schema)
}
result = toHtml(
hast,
Object.assign({}, settings, {allowDangerousHtml: !clean})
)
// Add an eof eol.
if (root && result && /[^\r\n]/.test(result.charAt(result.length - 1))) {
result += '\n'
}
return result
}
}