-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (65 loc) · 1.41 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
export default function parse (markdown = '') {
// TODO: Add input checking
const states = {
blankline: 1,
code: 2
}
const tags = {
code: {
open: '<pre><code>',
close: '</code></pre>'
}
}
let state = states.blankline
let spaces = 0
const ctx = Object.create(null)
ctx.value = ''
ctx.output = ''
const lexer = /\t|\s|\n|\r|[^\t\n\r]+/y
const output = () => {
ctx.output += ctx.value
ctx.value = ''
}
let tokens = []
let match = ''
let end = false
while ((tokens = lexer.exec(markdown)) !== null) {
match = tokens[0]
end = lexer.lastIndex === markdown.length
if (state === states.blankline) {
switch (match) {
case '\t':
spaces += 4
continue
case ' ':
spaces += 1
continue
default:
if (spaces >= 4) {
state = states.code
spaces = 0
}
break
}
}
if (state === states.code) {
if (ctx.value === '') { ctx.value += tags.code.open }
if (/\n|\r/.test(match)) {
ctx.value += '\n'
state = states.blankline
continue
}
ctx.value += match
if (end) {
ctx.value += '\n' + tags.code.close
output()
}
}
}
// // flush the last value
// if (ctx.entry.length !== 0) {
// this.valueEnd(ctx);
// this.entryEnd(ctx);
// }
return ctx.output
}