This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.js
81 lines (66 loc) · 2.07 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var defaults = require('lodash.defaults')
var isPlainObj = require('is-plain-obj')
var render = require('./lib/render')
var sanitize = require('./lib/sanitize')
var markyInfo = require('./marky.json')
var defaultOptions = {
sanitize: true,
nofollow: true,
linkify: true,
highlightSyntax: true,
prefixHeadingIds: true,
enableHeadingLinkIcons: true,
serveImagesWithCDN: false,
debug: false,
package: null,
headingAnchorClass: 'anchor',
headingSvgClass: ['octicon', 'octicon-link']
}
var marky = module.exports = function (markdown, options) {
var html
if (typeof markdown !== 'string') {
throw Error('first argument must be a string')
}
if (typeof options !== 'undefined' && !isPlainObj(options)) {
throw Error('second argument must be an object')
}
options = options || {}
defaults(options, defaultOptions)
var log = function (msg) {
if (options.debug) {
console.log('marky-markdown: ' + msg)
}
}
log('\n\n' + markdown + '\n\n')
log('Parse markdown into HTML and add syntax highlighting')
html = render(markdown, options)
if (options.sanitize) {
log('Sanitize malicious or malformed HTML')
html = sanitize(html, options)
}
if (options.debug) {
var debugHeader =
'<!--' +
' this HTML was generated using marky-markdown version ' + markyInfo.version + '.' +
' see an issue? file at ' + markyInfo.issuesUrl + '.' +
' please include the version in your issue. thanks for using marky!' +
' to learn more, visit ' + markyInfo.repositoryUrl + '.' +
' -->'
html = debugHeader + '\n' + html
}
return html
}
marky.parsePackageDescription = function (description) {
return sanitize(render.renderPackageDescription(description), defaultOptions)
}
marky.getParser = function (options) {
options = options || {}
var parser = render.getParser(defaults(options, defaultOptions))
if (options.sanitize) {
var originalRender = parser.render
parser.render = function (markdown) {
return sanitize(originalRender.call(parser, markdown), options)
}
}
return parser
}