-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (60 loc) · 2.37 KB
/
index.js
File metadata and controls
79 lines (60 loc) · 2.37 KB
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
const visit = require('unist-util-visit')
const unified = require('unified')
const parse = require('rehype-parse')
const toHTML = require('hast-util-to-html')
// const hastToString = require('hast-util-to-string')
module.exports = rehypeDetails
const assign = Object.assign
const parseHtml = unified().use(parse, {fragment: true, position: false})
const source = 'rehype-details'
function rehypeDetails(options) {
const opts = options || {}
const throwOnError = opts.throwOnError || false
return transformDetails
function transformDetails(tree, file) {
// var Compiler = this.Compiler;
// var visitors = Compiler.prototype.visitors;
// console.log(Compiler)
visit(tree, 'element', visitor)
function visitor(node, index, parent) {
const className = node.properties.className || []
if (node.tagName !== 'div' || !className.includes('details')) {
return;
}
// console.log(node)
let header = node.properties.header || ""
let icon = 'note'
let display = 0
let value = node.properties.summary || "<p>Details</p>"
if (header.length > 0)
header = header.replace('\n', '')
// const arr = header.split(value)
if (header.indexOf('!!!') > -1 || header.substr(0, 4).indexOf('+') > -1) display = 1
// // const title = arr[1]
if (value.indexOf('warning') > -1) icon = 'warning'
// // value = value.replace(icon, '').trim()
// // console.log(`${title}, ${header}, ${value}, ${display}`)
// // value = value.replace(/"+$/, "").replace(/^"+/, "").trim();
// }
const ch = node.children
// console.log(header)
// console.log(toHTML(node))
let classes = `details-${icon}` + (display?` details-open`:``)
// console.log(classes)
let starter = `<details class="${classes}">`
if (value && value.length > 0) {
value = value.trim()
starter += `<summary class="summary-note">${value}</summary>`
}
let result = ''
let ending = `</details>`
for (let cur of ch) result += toHTML(cur)
// node.children
// {/* <summary>Details</summary> */}
// console.log((ch.length))
// console.log((starter + result + ending))
// node.children = parseHtml.parse(result).children
node.children = parseHtml.parse(starter + result + ending).children
}
}
}