forked from Jellyvision/jsdoc-mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (55 loc) · 1.55 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
const doctrine = require('doctrine')
/**
* @constant {string} JSDOC_MERMAID_TAG
* @description Matches starting strings of a jsdoc comment
*/
const JSDOC_MERMAID_TAG = /@mermaid\b/
/**
* @constant {string} MERMAID_VERSION
* @description Semver Mermaid version
*/
const MERMAID_VERSION = 'latest'
/**
* @constant {string} MERMAID_HTML_SCRIPT
* @description Html tag that include mermaid library
*/
const MERMAID_HTML_SCRIPT = `<script src="https://unpkg.com/mermaid@${MERMAID_VERSION}/dist/mermaid.min.js"></script>`
/**
* @constant {Object} ESCAPE_HTML_MAPPING
* @description Map of all characters that need HTML escaping
*/
const ESCAPE_HTML_MAPPING = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
function escapeHtml(str) {
return str.replace(/[&<>"']/g, c => ESCAPE_HTML_MAPPING[c])
}
let isAddedMermaid = {}
exports.handlers = {
newDoclet: function (e) {
if (e.doclet.comment && JSDOC_MERMAID_TAG.test(e.doclet.comment)) {
const tags = doctrine.parse(e.doclet.comment, {
unwrap: true,
tags: ['mermaid'],
recoverable: true
}).tags
const htmls = tags.map(tag => [
'<div class="mermaid">',
escapeHtml(tag.description),
'</div>'
].join(''))
if (htmls) {
e.doclet.description = e.doclet.description || ''
if (!isAddedMermaid[e.doclet.memberof]) {
e.doclet.description += MERMAID_HTML_SCRIPT
isAddedMermaid[e.doclet.memberof] = true
}
e.doclet.description += htmls.join('')
}
}
}
}