-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown.js
More file actions
132 lines (121 loc) · 3.31 KB
/
markdown.js
File metadata and controls
132 lines (121 loc) · 3.31 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// base
const unified = require('unified')
const remark = require('remark')
const parse = require('remark-parse')
const remark2rehype = require('remark-rehype')
const format = require('rehype-format')
const html = require('rehype-stringify')
const path = require('path')
const visit = require('unist-util-visit')
const tokenizeWords = require('space-separated-tokens')
// markdown plugins
const terms = require('remark-terms')
const containers = require('remark-containers')
const sub_super = require('remark-sub-super')
const frontmatter = require('remark-frontmatter')
const parseFrontmatter = require('remark-parse-yaml')
const guide = require('./markdown-lint')
// html plugins
const slug = require('rehype-slug')
const urls = require('rehype-urls')
const markdown = unified()
.use(parse)
.use(guide)
.use(terms, [{
open: '{',
close: '}',
element: 'span',
class: 'term-1'
}, {
open: '{{',
close: '}}',
element: 'span',
class: 'term-2'
}])
.use(containers, {
default: true,
custom: [{
type: 'sidebar',
element: 'aside',
transform: function(node, config, tokenize) {
node.data.hProperties = {
className: config || 'left'
}
}
}, {
type: 'callout',
element: 'article',
transform: function(node, config, tokenize) {
node.data.hProperties = {
className: config || 'left'
}
}
}, {
type: 'columns',
element: 'div',
transform: function(node, config, tokenize) {
node.data.hProperties = {
className: 'columns'
}
}
}, {
type: 'quote',
element: 'aside',
transform: function(node, config, tokenize) {
var words = tokenizeWords.parse(config)
node.data.hProperties = {
className: `quoted ${words.shift()}`
}
node.children.push({
type: 'footer',
data: {
hName: 'footer'
},
children: tokenize(words.join(' '))
})
}
}, {
type: 'figure-table',
element: 'figure',
transform: function(node, config, tokenize) {
node.data.hProperties = {
className: `figure-table`
}
node.children.push({
type: 'figcaption',
data: {
hName: 'figcaption'
},
children: tokenize(config)
})
}
}]
})
.use(sub_super)
.use(frontmatter, {
type: 'yaml',
marker: '-'
})
.use(parseFrontmatter)
.use(copyFrontmatter)
.use(remark2rehype)
.use(slug)
.use(urls, fixupLinks)
.use(format)
.use(html)
function fixupLinks(url) {
if (url.pathname && path.extname(url.pathname) === '.md') {
// if the link is internal to an .md file, change it to an .html file
return url.pathname.replace('.md', '.html')
}
return url.href
}
function copyFrontmatter() {
return function(ast, file) {
visit(ast, 'yaml', item => {
// copy parsed frontmatter to the file data
file.data.metadata = item.data.parsedValue
})
}
}
module.exports = markdown