-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.js
More file actions
76 lines (73 loc) · 2.3 KB
/
rules.js
File metadata and controls
76 lines (73 loc) · 2.3 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
export default [
{
regex: /^(#{1,6}) (.*?)(?:(?: \1)|$)/gm,
assign: (match) => {
return {
type: 'header',
subtype: `h${match[1].length}`,
content: match[2],
}
},
template: (entry) => {
return `<${entry.subtype}>${entry.content}<${entry.subtype}>`
},
index: 1
},
{
regex: /(?:^(?:(?: {2})*\d. )(?:.*)\n?|^(?:(?: {2})*[-+*] )(?:.*)\n?)+/gm,
assign: (match) => {
return {
type: 'list',
content: match[0]
}
},
template: (entry) => {
/**
* content = whole element
* while list elements exist
* for each list element
* get list type from prefix -> prefix
* remove prefix from list element
* wrap element in <?prefix><li>...</li></?prefix>
* replace original in content with wrapped
* remove all <(ul|ol)></\1>
**/
while (entry.content.match(/^(?: {2})*(?:\d+\.|[*+-]) /m)) {
while (entry.content.match(/^(?:\d+\.|[*+-]) /m)) {
let listItem = entry.content.match(/^(?:\d+\.|[*+-]) .*(?:\r?\n(?!\d+\.|[*+-]).*)*/m)[0]
let type = (listItem.match(/^\d+\. /)) ? 'ol' : 'ul'
let parsedListItem = listItem.replace(/^(?:\d+\.|[*+-]) /, '')
// console.log(parsedListItem);
let wrappedListItem = `<${type}><li>${parsedListItem}</li></${type}>`
console.log(entry.content)
console.log('=====div=====')
entry.content = entry.content.replace(listItem, wrappedListItem)
}
entry.content = entry.content.replace(/<\/(ol|ul)>\r?\n?<\1>/g, '\n')
entry.content = entry.content.replace(/^ {2}/gm, '')
}
return entry.content
},
index: 0
},
{
regex: /(?:^>+ .*\n?)+/gm,
assign: (match) => {
return {
type: 'blockquote',
content: match[0],
}
},
template: (entry) => {
while (entry.content.match(/^> /m)) {
let newNested = entry.content.match(/(?:^>+ .*\n?)+/m)[0]
newNested = newNested.replace(/^> /gm, '')
newNested = `<blockquote>${newNested}</blockquote>`
entry.content = entry.content.replace(/(?:^>+ .*\n?)+/m, newNested)
}
entry.content = entry.content.replace(/\n/g, '<br>')
return entry.content
},
index: 2
}
]