-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
33 lines (27 loc) · 956 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
const toHAST = require(`mdast-util-to-hast`)
const hastToHTML = require(`hast-util-to-html`)
const codeHandler = require(`./code-handler`)
const AES = require(`crypto-js/aes`)
module.exports = ({ markdownNode, markdownAST }, pluginOptions = {}) => {
if (markdownNode.frontmatter.password) {
// ast to html: copied from gatsby-transform-remark
const htmlAst = toHAST(markdownAST, {
allowDangerousHTML: true,
handlers: {
code: codeHandler,
},
})
const html = hastToHTML(htmlAst, {
allowDangerousHTML: true,
})
// encrypt it use crypto-js
const crypted = AES.encrypt(html, String(markdownNode.frontmatter.password)).toString()
// replace all nodes into single encrypted text
markdownAST.children = [{
type: 'html',
value: crypted,
}]
console.log('success encrypt post: ' + markdownNode.frontmatter.title)
}
return markdownAST
}