-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromJSON.ts
More file actions
299 lines (292 loc) · 8.26 KB
/
fromJSON.ts
File metadata and controls
299 lines (292 loc) · 8.26 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
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
import * as parser from "@progfay/scrapbox-parser"; // MIT License
import type * as cosenseTypes from "@cosense/types/rest"; // MIT License
function hsc(unsafeText: string) {
if (typeof unsafeText !== "string") {
return unsafeText;
}
return unsafeText.replace(
/[&'`"<>]/g,
function (match) {
return {
"&": "&",
"'": "'",
"`": "`",
'"': """,
"<": "<",
">": ">",
}[match as "&" | "'" | "`" | '"' | "<" | ">"];
},
);
}
function genereteHTML_Node(node: parser.Node, slashcount: number): string {
switch (node.type) {
case "quote":
return `<q>${
node.nodes.map((a) => genereteHTML_Node(a, slashcount)).join("")
}</q>`;
case "helpfeel":
return node.text;
case "strong":
return `<strong>${
node.nodes.map((a) => genereteHTML_Node(a, slashcount)).join("")
}</strong>`;
case "formula":
return `<code>math:${hsc(node.formula)}</code>`;
case "decoration":
return node.nodes.map((a) => genereteHTML_Node(a, slashcount))
.join("");
case "code":
return `<code>${hsc(node.text)}</code>`;
case "commandLine":
return `<code>${hsc(node.text)}</code>`;
case "blank":
return "";
case "strongImage":
case "image":
return `<a href="${hsc(encodeURI(node.src))}">[image]</a>`;
case "link":
if (node.pathType == "absolute") {
return `<a href="${hsc(encodeURI(node.href))}">${
hsc(node.content)
}</a>`;
} else if (node.pathType == "root") {
return `<a href="https://scrapbox.io${hsc(node.href)}">${
hsc(node.href)
}</a>`;
}
return `<a href="${"../".repeat(slashcount)}${
hsc(sanitizeDirName(node.href))
}/text.html">${hsc(node.href)}</a>`;
case "googleMap":
return `<a href="${hsc(encodeURI(node.url))}">[map]</a>`;
case "strongIcon":
case "icon":
if (node.pathType == "root") {
return `<a href="https://scrapbox.io${hsc(node.path)}">[${
hsc(node.path)
}.icon]</a>`;
}
return `<a href="${"../".repeat(slashcount)}${
hsc(sanitizeDirName(node.path))
}/text.html">[${hsc(node.path)}.icon]</a>`;
case "hashTag":
return `<a href="${"../".repeat(slashcount)}${
hsc(sanitizeDirName(node.href))
}/text.html">#${hsc(node.href)}</a>`;
case "numberList":
return `${node.number}. ${
node.nodes.map((a) => genereteHTML_Node(a, slashcount))
.join("")
}`;
case "plain":
return node.text;
}
}
function generateHTML(
page: parser.Page,
slashcount: number,
related: string[],
) {
let html = `
<!DOCTYPE HTML><html><head><title>${
page[0].type == "title" ? page[0].text : "unknown"
}</title></head><body>
<a href="${
"../".repeat(slashcount)
}index.html">top</a>, <a href="https://scrapbox.io/${PROJECT_NAME}/${
page[0].type == "title" ? page[0].text : ""
}">online</a>, <a href="./">source</a>`;
for (const block of page) {
if (block.type == "title") {
html += `<h1>${hsc(block.text)}</h1>`;
} else if (block.type == "line") {
html += `<p style="margin-left:${block.indent}em;">${
block.nodes.map((a) => genereteHTML_Node(a, slashcount)).join(
"",
)
}</p>`;
} else if (block.type == "table") {
html += `<table style="margin-left:${block.indent}em;">`;
html += `table:${hsc(block.fileName)}`;
for (const rows of block.cells) {
html += `<tr>`;
for (const cols of rows) {
html += `<td>`;
html += cols.map((a) => genereteHTML_Node(a, slashcount))
.join("");
html += `</td>`;
}
html += `</tr>`;
}
html += `</table>`;
} else if (block.type == "codeBlock") {
html += `<p><a href="${hsc(sanitizeFileName(block.fileName))}">code:${
hsc(block.fileName)
}</a></p>`;
}
}
html += `<h1>1 hop links</h1><ul>`;
for (const element of related) {
html += `<li><a href="${"../".repeat(slashcount)}${
hsc(sanitizeDirName(element))
}/text.html">${hsc(element)}</a></li>`;
}
html += `</ul>`;
html += `<p>JSONから生成した場合、2 hop linksは使用できません<p>`;
html += `</body></html>`;
return html;
}
function sanitizeDirName(name: string) {
// Windowsで使えない文字を考慮
if (Deno.build.os == "windows") {
return name.replaceAll("%", "%25").replaceAll(":", "%3A").replaceAll(
"*",
"%2A",
).replaceAll("?", "%3F").replaceAll('"', "%22").replaceAll("<", "%3C")
.replaceAll(">", "%3E").replaceAll("?", "%3F").replaceAll(
"|",
"%7C",
);
} else {
return name;
}
}
function sanitizeFileName(name: string) {
// Windowsで使えない文字を考慮
if (Deno.build.os == "windows") {
return name.replaceAll("%", "%25").replaceAll(":", "%3A").replaceAll(
"*",
"%2A",
).replaceAll("?", "%3F").replaceAll('"', "%22").replaceAll("<", "%3C")
.replaceAll(">", "%3E").replaceAll("?", "%3F").replaceAll(
"|",
"%7C",
).replaceAll("/", "%2F");
} else {
return name.replaceAll("/", "%2F");
}
}
async function generateIndexHTML(indexPages: string[]) {
indexPages.sort();
const indexGrouped = Object.groupBy(indexPages, (a) => a[0]);
await Deno.writeTextFile(
`./${destination}/index.html`,
`
<!DOCTYPE HTML>
<html>
<head>
<title>pages of ${hsc(PROJECT_NAME)}</title>
</head>
<body>
<h1>pages of ${hsc(PROJECT_NAME)}</h1>
${
Object.entries(indexGrouped).map(([k, _]) =>
`<a href="#${hsc(k)}" style="padding-inline-end:4px;">${hsc(k)}</a>`
).join("")
}
<ul>
${
Object.entries(indexGrouped).map(([k, v]) =>
`<li id="${hsc(k)}">${hsc(k)}<ul>${
v?.map((v) =>
`<li><a href="${hsc(encodeURI(sanitizeDirName(v)))}/text.html">${
hsc(v)
}</a></li>`
).join("")
}</ul></li>`
).join("")
}
</ul>
</body>
</html>
`,
);
}
let PROJECT_NAME = "";
let destination = "";
export async function cloneFromJSON(
jsonFileName: string,
destination_: string,
) {
destination = destination_;
const data: cosenseTypes.BackupData = JSON.parse(
await Deno.readTextFile(jsonFileName),
);
PROJECT_NAME = data.name;
const pageCount = data.pages.length;
try {
await Deno.remove(
`./${destination}`,
{ recursive: true },
);
console.log("deleted");
} catch {
//
}
await Deno.mkdir(
`./${destination}`,
);
await Deno.writeTextFile(
`./${destination}/index.html`,
"",
);
await generateIndexHTML(data.pages.map((a) => a.title));
let skipnum = 0;
for (const page of data.pages) {
skipnum++;
console.log(
`${skipnum}/${pageCount}`,
);
try {
const pagename = page.title;
if (pagename.includes("../")) {
console.log(
'pagename "' + pagename + '" is danger!not clone this page.',
);
}
await Deno.mkdir(
`./${destination}/${sanitizeDirName(pagename)}`,
{ recursive: true },
);
const pagetext = page.lines.map((a) => a.text).join("\n");
const pageparse = parser.parse(
pagetext,
);
await Deno.writeTextFile(
`./${destination}/${sanitizeDirName(pagename)}/text.txt`,
pagetext,
);
await Deno.writeTextFile(
`./${destination}/${sanitizeDirName(pagename)}/text.html`,
generateHTML(
pageparse,
pagename.split("/").length,
page.linksLc.concat(
data.pages.filter((a) => a.linksLc.includes(pagename))
.map((a) => a.title),
),
),
);
await Deno.writeTextFile(
`./${destination}/${sanitizeDirName(pagename)}/json.json`,
JSON.stringify(page),
);
for (const element of pageparse) {
if (element.type == "codeBlock") {
try {
await Deno.writeTextFile(
`./${destination}/${sanitizeDirName(pagename)}/${
sanitizeFileName(element.fileName)
}`,
element.content,
);
} catch (e) {
console.error(e);
}
}
}
} catch (e) {
console.error(e);
}
}
}