-
Notifications
You must be signed in to change notification settings - Fork 0
/
why.js
215 lines (188 loc) · 5.19 KB
/
why.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// why?component
const componentsDirectory = "/components/"
function markdown(src) {
// drawdown.js | (c) Adam Leggett (https://github.com/adamvleggett/drawdown) | MIT License
var rx_lt = /</g
var rx_gt = />/g
var rx_space = /\t|\r|\uf8ff/g
var rx_escape = /\\([\\\|`*_{}\[\]()#+\-~])/g
var rx_hr = /^([*\-=_] *){3,}$/gm
var rx_blockquote = /\n *> *([^]*?)(?=(\n|$){2})/g
var rx_list =
/\n( *)(?:[*\-+]|((\d+)|([a-z])|[A-Z])[.)]) +([^]*?)(?=(\n|$){2})/g
var rx_listjoin = /<\/(ol|ul)>\n\n<\1>/g
var rx_highlight =
/(^|[^A-Za-z\d\\])(([*_])|(~)|(\^)|(--)|(\+\+)|`)(\2?)([^<]*?)\2\8(?!\2)(?=\W|_|$)/g
var rx_code = /\n((```|~~~).*\n?([^]*?)\n?\2|(( .*?\n)+))/g
var rx_link = /((!?)\[(.*?)\]\((.*?)( ".*")?\)|\\([\\`*_{}\[\]()#+\-.!~]))/g
var rx_table = /\n(( *\|.*?\| *\n)+)/g
var rx_thead = /^.*\n( *\|( *\:?-+\:?-+\:? *\|)* *\n|)/
var rx_row = /.*\n/g
var rx_cell = /\||(.*?[^\\])\|/g
var rx_heading = /(?=^|>|\n)([>\s]*?)(#{1,6}) (.*?)( #*)? *(?=\n|$)/g
var rx_para = /(?=^|>|\n)\s*\n+([^<]+?)\n+\s*(?=\n|<|$)/g
var rx_stash = /-\d+\uf8ff/g
function replace(rex, fn) {
src = src.replace(rex, fn)
}
function element(tag, content) {
return "<" + tag + ">" + content + "</" + tag + ">"
}
function blockquote(src) {
return src.replace(rx_blockquote, function (all, content) {
return element(
"blockquote",
blockquote(highlight(content.replace(/^ *> */gm, "")))
)
})
}
function list(src) {
return src.replace(rx_list, function (all, ind, ol, num, low, content) {
var entry = element(
"li",
highlight(
content
.split(
RegExp("\n ?" + ind + "(?:(?:\\d+|[a-zA-Z])[.)]|[*\\-+]) +", "g")
)
.map(list)
.join("</li><li>")
)
)
return (
"\n" +
(ol
? '<ol start="' +
(num
? ol + '">'
: parseInt(ol, 36) -
9 +
'" style="list-style-type:' +
(low ? "low" : "upp") +
'er-alpha">') +
entry +
"</ol>"
: element("ul", entry))
)
})
}
function highlight(src) {
return src.replace(
rx_highlight,
function (all, _, p1, emp, sub, sup, small, big, p2, content) {
return (
_ +
element(
emp
? p2
? "strong"
: "em"
: sub
? p2
? "s"
: "sub"
: sup
? "sup"
: small
? "small"
: big
? "big"
: "code",
highlight(content)
)
)
}
)
}
function unesc(str) {
return str.replace(rx_escape, "$1")
}
var stash = []
var si = 0
src = "\n" + src + "\n"
replace(rx_lt, "<")
replace(rx_gt, ">")
replace(rx_space, " ")
// blockquote
src = blockquote(src)
// horizontal rule
replace(rx_hr, "<hr/>")
// list
src = list(src)
replace(rx_listjoin, "")
// code
replace(rx_code, function (all, p1, p2, p3, p4) {
stash[--si] = element(
"pre",
element("code", p3 || p4.replace(/^ /gm, ""))
)
return si + "\uf8ff"
})
// link or image
replace(rx_link, function (all, p1, p2, p3, p4, p5, p6) {
stash[--si] = p4
? p2
? '<img src="' + p4 + '" alt="' + p3 + '"/>'
: '<a href="' + p4 + '">' + unesc(highlight(p3)) + "</a>"
: p6
return si + "\uf8ff"
})
// table
replace(rx_table, function (all, table) {
var sep = table.match(rx_thead)[1]
return (
"\n" +
element(
"table",
table.replace(rx_row, function (row, ri) {
return row == sep
? ""
: element(
"tr",
row.replace(rx_cell, function (all, cell, ci) {
return ci
? element(
sep && !ri ? "th" : "td",
unesc(highlight(cell || ""))
)
: ""
})
)
})
)
)
})
// heading
replace(rx_heading, function (all, _, p1, p2) {
return _ + element("h" + p1.length, unesc(highlight(p2)))
})
// paragraph
replace(rx_para, function (all, content) {
return element("p", unesc(highlight(content)))
})
// stash
replace(rx_stash, function (all) {
return stash[parseInt(all)]
})
return src.trim()
}
async function insertComponent(element, componentPath) {
const isMarkdown = !(element.attributes.markdown === undefined)
const componentText = (
componentPath === undefined
? element.innerHTML
: await (
await fetch(
componentsDirectory + componentPath + (isMarkdown ? ".md" : ".html")
)
).text()
).replace("[y-data]", element.innerHTML)
element.innerHTML = isMarkdown ? markdown(componentText) : componentText
}
async function addComponents() {
const components = document.querySelectorAll("y-component")
components.forEach((component) =>
insertComponent(component, component.attributes.src?.value)
)
}
addComponents()