-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
383 lines (362 loc) · 11.1 KB
/
mod.ts
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { parse as parseScrapbox } from "@progfay/scrapbox-parser";
import type * as scrapboxParser from "@progfay/scrapbox-parser";
type Logger = {
error: (message: string) => unknown;
warn: (message: string) => unknown;
};
type ReViewOption = {
baseHeadingLevel?: number;
logger?: Logger;
};
export type ConverterOption = ReViewOption & scrapboxParser.ParserOption;
type Itemization = {
level: number;
type: "normal";
nodes: scrapboxParser.Node[];
} | {
level: number;
type: "number";
number: number;
raw: string;
nodes: scrapboxParser.Node[];
};
type State = {
currentItemization: null | Itemization[];
inBlockQuote: boolean;
};
function generateReView(
ast: scrapboxParser.Page,
option: ReViewOption = {},
): string {
const baseHeadingLevel = option.baseHeadingLevel || 3;
const logger = option.logger || {
error(message: string) {
console.error(message);
},
warn(message: string) {
console.warn(message);
},
};
let out = "";
const state: State = {
currentItemization: null,
inBlockQuote: false,
};
for (const n of ast) {
if (n.type === "title") {
out += `= ${n.text}`;
out += "\n\n";
} else {
if (n.indent === 0 && state.currentItemization) {
out += generateReViewItemization(state.currentItemization, logger);
// 箇条書き終了
state.currentItemization = null;
}
if (
!(n.type === "line" && n.indent === 0 && n.nodes.length !== 0 &&
n.nodes[0].type === "quote") && state.inBlockQuote
) {
// 引用終了
state.inBlockQuote = false;
out += "//}\n\n";
}
if (
n.type === "line" && n.indent === 0 && n.nodes.length !== 0 &&
n.nodes[0].type === "quote"
) {
// 引用
if (!state.inBlockQuote) {
// 引用開始
state.inBlockQuote = true;
out += "//quote{\n";
}
out += `${
n.nodes[0].nodes.map((n) => nodeToReView(n, logger)).join("")
}\n`;
continue;
}
if (n.indent !== 0) {
// 箇条書き
if (!state.currentItemization) {
// 箇条書き開始
state.currentItemization = [];
}
if (n.type === "line") {
if (n.nodes.length !== 0 && n.nodes[0].type === "numberList") {
// 数字付き箇条書き
const numListNode = n.nodes[0];
state.currentItemization.push({
level: n.indent,
type: "number",
number: numListNode.number,
nodes: numListNode.nodes,
raw: numListNode.raw,
});
} else {
// 箇条書き
state.currentItemization.push({
level: n.indent,
type: "normal",
nodes: n.nodes,
});
}
continue;
} else {
if (n.type === "table") {
// 箇条書きの中の表、現時点では非対応
logger.error(
`Table inside itemization not supported: ${n.fileName}`,
);
}
if (n.type === "codeBlock") {
// 箇条書きの中のコードブロック、現時点では非対応
logger.error(
`Code block inside itemization not supported: ${n.fileName}`,
);
}
out += ` ${"*".repeat(n.indent)}\n`;
continue;
}
}
if (n.type === "codeBlock" && n.indent === 0) {
// コードブロック
out += `//emlist[${
escapeBlockCommandOption(n.fileName)
}]{\n${n.content}\n//}\n\n`;
continue;
}
if (n.type === "table" && n.indent === 0) {
// 表
out += `${generateReViewTable(n, logger)}\n\n`;
continue;
}
if (
n.type === "line" && n.indent === 0 && n.nodes.length !== 0 &&
n.nodes[0].type === "commandLine"
) {
// コマンドライン
out += `//cmd{\n${n.nodes[0].raw}\n//}\n\n`;
continue;
}
if (
n.type === "line" && n.indent !== 0 && n.nodes.length !== 0 &&
n.nodes[0].type === "quote"
) {
// 箇条書きの中の引用、現時点では非対応
logger.error(
`Blockquote inside itemization not supported: ${n.nodes[0].raw}`,
);
}
if (
n.type === "line" && n.nodes.length === 1 &&
n.nodes[0].type === "decoration" && n.nodes[0].rawDecos != "*" &&
/^\*+$/.test(n.nodes[0].rawDecos)
) {
// 見出し
const boldNode = n.nodes[0];
if (boldNode.rawDecos.length <= baseHeadingLevel) {
const header = "=".repeat(
baseHeadingLevel + 2 - boldNode.rawDecos.length,
);
if (
boldNode.nodes.length === 1 && boldNode.nodes[0].type === "image"
) {
out += `//indepimage[${
escapeBlockCommandOption(boldNode.nodes[0].src)
}]\n\n`;
continue;
}
out += `${header} ${
boldNode.nodes.map((n) => nodeToReView(n, logger)).join("")
}`;
out += "\n\n";
continue;
}
}
if (
n.type === "line" && n.nodes.length === 1 &&
(n.nodes[0].type === "image" || n.nodes[0].type === "strongImage")
) {
// 画像
// とりあえずsrcそのまま入れる
out += `//indepimage[${escapeBlockCommandOption(n.nodes[0].src)}]\n\n`;
continue;
}
if (
n.type === "line" && n.nodes.length === 1 &&
n.nodes[0].type === "formula"
) {
out += `//texequation{\n${n.nodes[0].formula}\n//}`;
continue;
}
if (n.type === "line") {
out += n.nodes.map((n) => nodeToReView(n, logger)).join("");
out += "\n\n";
}
}
}
return out.replaceAll(/\n{2,}/g, "\n\n").replace(/\n*$/, "\n");
}
function generateReViewItemization(lists: Itemization[], logger: Logger) {
if (lists.length === 0) {
return "";
}
if (lists.every((l) => l.type === "number")) {
const numbers = lists.map((l) => l.number);
let isNumberContinuous = true;
for (let i = 0; i < numbers.length - 1; i++) {
if (numbers[i + 1] - numbers[i] !== 1) {
isNumberContinuous = false;
break;
}
}
if (isNumberContinuous && lists.every((l) => l.level === 1)) {
let out = "";
if (numbers[0] !== 1) {
out += `//olnum[${numbers[0]}]\n\n`;
}
for (const list of lists) {
const lineContent = list.nodes.map((n) => nodeToReView(n, logger)).join(
"",
);
out += ` ${list.number}. ${lineContent}\n`;
}
out += "\n";
return out;
}
}
let out = "";
for (const list of lists) {
if (list.type === "normal") {
const lineContent = list.nodes.map((n) => nodeToReView(n, logger)).join(
"",
);
out += ` ${"*".repeat(list.level)} ${lineContent}\n`;
continue;
}
logger.error(
`Nested or discontinuous number list not supported: ${list.raw}`,
);
const lineContent = list.nodes.map((n) => nodeToReView(n, logger)).join(
"",
);
out += ` ${"*".repeat(list.level)} ${list.number}. ${lineContent}\n`;
}
out += "\n";
return out;
}
function generateReViewTable(node: scrapboxParser.Table, logger: Logger) {
const headerColumns = node.cells[0];
if (headerColumns === undefined) {
return `//emtable[${escapeBlockCommandOption(node.fileName)}]{\n//}`;
}
const headerText = generateReViewTableColumn(headerColumns, logger);
const borderText = "------------";
return `//emtable[${escapeBlockCommandOption(node.fileName)}]{
${headerText}
${borderText}
${
node.cells.slice(1).map((column) =>
generateReViewTableColumn(column, logger)
).join("\n")
}
//}`;
}
function generateReViewTableColumn(
column: scrapboxParser.Node[][],
logger: Logger,
): string {
return column.map((cell) => cell.map((n) => nodeToReView(n, logger)).join(""))
.join("\t");
}
function nodeToReView(node: scrapboxParser.Node, logger: Logger): string {
if (node.type === "link") {
if (node.pathType === "relative") {
logger.error(
`Can't convert relative links. Please use absolute links instead: ${node.raw}`,
);
return node.raw;
}
if (node.pathType === "root") {
logger.warn(`An internal link to a Scrapbox's page is used: ${node.raw}`);
const href = new URL(node.href, "https://scrapbox.io").href;
return `@<href>{${escapeHrefUrl(href)}}`;
}
return node.content === ""
? `@<href>{${escapeHrefUrl(node.href)}}`
: `@<href>{${escapeHrefUrl(node.href)}, ${escapeHrefUrl(node.content)}}`;
} else if (node.type === "hashTag") {
logger.error(
`Can't convert relative links. Please use absolute links instead: ${node.raw}`,
);
return node.raw;
} else if (node.type === "strong") {
return `@<strong>{${
escapeInlineCommand(
node.nodes.map((n) => nodeToReView(n, logger)).join(""),
)
}}`;
} else if (node.type === "decoration") {
if (node.nodes.length === 1 && node.nodes[0].type === "image") {
return nodeToReView(node.nodes[0], logger);
}
return node.decos.reduce(
(inside, decoration) => {
if (/\*-[0-9]*/.test(decoration)) {
return `@<strong>{${inside}}`;
} else if (decoration === "/") {
return `@<i>{${inside}}`;
} else if (decoration === "-") {
return `@<del>{${inside}}`;
}
return inside;
},
escapeInlineCommand(
node.nodes.map((n) => nodeToReView(n, logger)).join(""),
),
);
} else if (node.type === "code") {
return `@<code>{${escapeInlineCommand(node.text)}}`;
} else if (node.type === "commandLine") {
return `@<code>{${escapeInlineCommand(node.raw)}}`;
} else if (node.type === "formula") {
return `@<m>{${escapeInlineCommand(node.formula)}}`;
} else if (node.type === "image" || node.type === "strongImage") {
return `@<icon>{${escapeInlineCommand(node.src)}}`;
} else if (node.type === "plain") {
return node.text;
} else if (node.type === "icon" || node.type === "strongIcon") {
logger.warn(`An icon is used: ${node.raw}`);
return `@<icon>{${node.path}.icon}`;
} else if (node.type === "numberList") {
return node.raw;
} else if (node.type === "blank") {
return node.text;
} else {
logger.error(`Unsupported syntax: ${node.raw}`);
return node.raw;
}
}
function escapeInlineCommand(content: string): string {
return content.replaceAll("}", "\\}").replace(/\\$/, "\\\\");
}
function escapeHrefUrl(href: string) {
return escapeInlineCommand(href).replaceAll(",", "\\,");
}
function escapeBlockCommandOption(option: string) {
return option.replaceAll("]", "\\]");
}
/**
* Convert scrapbox format to Re:VIEW
* @param src scrapbox text
* @param option options
* @returns Re:VIEW text
*/
export default function scrapboxToReView(
src: string,
option: ConverterOption = {},
): string {
// 箇条書き/引用終了処理のため、番兵として最後に空行を入れる
const ast = parseScrapbox(src + "\n", option);
return generateReView(ast, option);
}