-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwiki.html
311 lines (280 loc) · 8.97 KB
/
wiki.html
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
article:not(.index, :target, :has(:target)),
:root:has(:target) article.index:not(:has(:target)) {
display: none;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
color: #1d1d1d;
background-color: #fefefe;
}
article {
padding: 1em;
margin: auto;
max-width: 70ch;
font: 1.25em sans-serif;
line-height: 1.75;
img {
max-width: 100%;
height: auto;
}
}
.editor {
display: flex;
flex-direction: column;
position: fixed;
top: 0;
left: 0;
z-index: 999;
width: 100vw;
height: 100vh;
font: 1em sans-serif;
background-color: inherit;
.menu {
display: flex;
justify-content: space-between;
padding: 0.5rem;
gap: 0.5rem;
}
textarea {
padding: 0.5rem;
border: 0;
outline: none;
flex: 1;
resize: none;
}
}
body:has(.editor:not([style*="display"])) {
overflow: hidden;
}
.download {
position: fixed;
right: 0;
top: 0;
z-index: 99;
margin: 0.5rem;
}
</style>
</head>
<body>
<article class="index">
<h1>1.5KB Single-File Wiki</h1>
<p><a href="https://dev.to/fedia/15kb-single-file-wiki-46a1">About this wiki</a></p>
<p>Double click anywhere to edit.</p>
<p>Visit the <a href="#sink">kitchen sink</a>.</p>
<pre><code>alert("Hello world");</code></pre>
<p>And <a href="#new-page">this page</a> doesn't exist yet.</p>
</article>
<template class="md" data-index>
# 1.5KB Single-File Wiki
[About this wiki](https://dev.to/fedia/15kb-single-file-wiki-46a1)
Double click anywhere to edit.
Visit the [kitchen sink](#sink).
```
alert("Hello world");
```
And [this page](#new-page) doesn't exist yet.
</template>
<button class="download" style="display: none" onclick="download()">
Download
</button>
<div class="editor" style="display: none">
<div class="menu">
<button onclick="cancelEdit()">Cancel</button>
<span class="hash"></span>
<button onclick="doneEdit()">Done</button>
</div>
<textarea placeholder="Write here..."></textarea>
<script>
{
const editorEl = document.querySelector(".editor");
const mdInput = editorEl.querySelector("textarea");
const hashEl = editorEl.querySelector(".hash");
const downloadBtn = document.querySelector(".download");
let currentId = "";
let currentPage = null;
const edit = () => {
hashEl.textContent = currentId;
mdInput.value = currentPage?.innerHTML || "";
editorEl.style.display = "";
mdInput.focus();
};
document.addEventListener("dblclick", edit);
const onLoad = () => {
currentId = location.hash.substring(1);
currentPage = document.querySelector(
currentId
? `template.md[data-id=${currentId}]`
: "template.md[data-index]"
);
if (!currentPage) edit();
};
window.addEventListener("hashchange", onLoad);
onLoad();
function cancelEdit() {
editorEl.style.display = "none";
if (!currentPage) location.hash = "";
}
function doneEdit() {
const mdValue = mdInput.value;
if (!currentPage) {
currentPage = document.createElement("template");
currentPage.className = "md";
currentPage.dataset.id = currentId;
document.body.append(currentPage);
}
let view = document.querySelector(
currentId ? `article#${currentId}` : "article.index"
);
if (!view) {
view = document.createElement("article");
view.id = currentId;
document.body.append(view);
}
if (mdValue.length) {
currentPage.innerHTML = mdValue;
view.innerHTML = md2html(mdValue);
location.hash = "";
location.hash = currentId;
} else {
currentPage.remove();
view.remove();
location.hash = "";
}
editorEl.style.display = "none";
downloadBtn.style.display = "";
}
function download() {
downloadBtn.style.display = "none";
const doc = document.documentElement.cloneNode(true);
const html = "<!DOCTYPE html>\n" + doc.outerHTML;
const blob = new Blob([html], { type: "text/html" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
const filename = location.pathname.split("/").pop() || "wiki";
link.download = filename.replace(
/(\d*\.html)?$/,
Date.now() + ".html"
);
link.click();
}
}
function md2html(str) {
const enchtml = (str) => str.replaceAll("<", "<");
const inlines = [
[
/(\*{1,2}|~~|`)(.+?)\1/g,
(_, c, txt) =>
c == "*"
? `<i>${txt}</i>`
: c == "**"
? `<b>${txt}</b>`
: c == "~~"
? `<s>${txt}</s>`
: `<code>${enchtml(txt)}</code>`,
],
[
/!\[(.*?)\]\((.+?)\)/g,
(_, txt, url) => `<img src="${url}" alt="${txt}">`,
],
[
/\[(.+?)\]\((.+?)\)/g,
(_, txt, url) => `<a href="${url}">${txt}</a>`,
],
[
/(?<!\=["'])https?\:\/\/[^\s]+/gi,
(url) => `<a href="${url}">${url}</a>`,
],
];
const replaceInlines = (str) =>
inlines.reduce((md, rule) => md.replace(...rule), str);
const fenceBuf = [];
const fenceMark = "\n<!\ufeff>\n";
const fence = (str) => (fenceBuf.push(str), fenceMark);
const blocks = [
[
/\n```([^\n]*)\n([\s\S]*?)\n```\n/g,
(_, arg, code) =>
fence(`\n<pre><code>${enchtml(code)}</code></pre>\n`),
],
[
/\n(#+)([^\n]+)/g,
(_, h, txt) => `\n<h${h.length}>${txt.trim()}</h${h.length}>`,
],
[
/\n(\n *\-[^\n]+)+/g,
(txt) =>
`\n<ul><li>${replaceInlines(txt)
.split(/\n+ *\- */)
.filter(Boolean)
.join("</li><li>")}</li></ul>`,
],
[
/\n(\n *\d+\.[^\n]+)+/g,
(txt) =>
`\n<ol><li>${replaceInlines(txt)
.split(/\n+ *\d+\. */)
.filter(Boolean)
.join("</li><li>")}</li></ol>`,
],
[
/\n([^<\n][^\n]*\n)+/g,
(txt) =>
`\n<p>${replaceInlines(
txt.replaceAll(" \n", "<br>\n").trim()
)}</p>\n`,
],
];
return blocks
.reduce((md, rule) => md.replace(...rule), `\n${str}\n`)
.replaceAll(fenceMark, () => fenceBuf.shift());
}
</script>
</div>
<template class="md" data-id="sink"># Kitchen Sink
this is para with **bold**, *italic* and [link](https://example.com).
Second line
and third line
will be on the same line in html!
1. no ~~content here~~
2. some `inline codes` and _nested_ formats: `*aa<tt>?</tt>a*`
<div>this *DIV* tag is _ignored_</div>
- unordered
- list
- item #3
some html:
<a href="https://example.com">example</a>
inline link: https://example.com
and another para with image: 
```
a multiline
code
block
```</template><article id="sink">
<h1>Kitchen Sink</h1>
<p>this is para with <b>bold</b>, <i>italic</i> and <a href="https://example.com">link</a>.
Second line
and third line<br>
will be on the same line in html!</p>
<ol><li>no <s>content here</s></li><li>some <code>inline codes</code> and _nested_ formats: <code>*aa<tt>?</tt>a*</code></li></ol>
<div>this *DIV* tag is _ignored_</div>
<ul><li>unordered</li><li>list</li><li>item #3</li></ul>
<p>some html:</p>
<a href="https://example.com">example</a>
<p>inline link: <a href="https://example.com">https://example.com</a></p>
<p>and another para with image: <img src="https://place-hold.it/80x20" alt="placeholder image"></p>
<pre><code>a multiline
code
block</code></pre>
</article>
</body>
</html>