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
|
/*** EXPORT ------------------------------------------- ***/
export default (input: string): string => {
const codeBlocks: { content: string; index: number; language: string; }[] = [];
const codeBlockPattern = /```(\w*)[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*```/g;
const inlineCode: string[] = [];
const inlineCodePattern = /`([^`]+)`/g;
const refPattern = /\s*\[(\w+)\]\s+<([^>]+)>$/gm;
const refs = new Map<string, string>();
/*** extract and replace code blocks with placeholders ***/
let processed = input.replace(codeBlockPattern, (_match, language, content) => {
const index = codeBlocks.length;
codeBlocks.push({
content: content.trimEnd(),
index,
language: language || ""
});
return `<!--CODE:BLOCK:${index}-->`;
});
/*** extract inline code ***/
processed = processed.replace(inlineCodePattern, (_match, content) => {
const index = inlineCode.length;
inlineCode.push(content);
return `<!--INLINE:CODE:${index}-->`;
});
/*** extract reference definitions (images/links) ***/
for (const match of processed.matchAll(refPattern)) {
const [, ref, url] = match;
refs.set(ref, url);
}
/*** remove reference definitions before blockquote processing ***/
processed = processed.replace(refPattern, "");
/*** process blockquotes ***/
processed = processBlockquotes(processed);
processed = processed
.replace(/(\n){6}/g, "") /*** remove 6 lines from the top of the memo ***/
.replace("References", "") /*** remove reference to reference definitions from output ***/
.replace(/(\*\*|__)(.*?)\1/g, `<strong style="white-space: nowrap;">$2</strong>`) /*** bold ***/
.replace(/(\*|_)(.*?)\1/g, "<em>$2</em>") /*** italic ***/
.replace(/(\~\~)(.*?)\1/g, `<del>$2</del>`) /*** strikethrough ***/
.replace(/'/g, "’") /*** fancy apostrophe ***/
.replace(/(:nbhyp:)/g, "‑") /*** non‑breaking hyphen ***/
// deno-lint-ignore no-irregular-whitespace
.replace(/(:nbsp:)/g, " ") /*** non‑breaking space ***/
.replace(/(\.\.\.)/g, "…") /*** ellipsis ***/
.replace(/---/g, "<hr/>")
.replace(/📸\[([^\]]+)\]\[(\w+)\]/g, (match, alt, ref) => {
const url = refs.get(ref);
return url ? `<img src="${url}" alt="${alt}" loading="lazy">` : match;
})
.replace(/📼\[([^\]]+)\]\[(\w+)\]/g, (match, alt, ref) => {
const url = refs.get(ref);
return url ? `<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen frameborder="0" referrerpolicy="strict-origin-when-cross-origin" src="${url}" title="${alt}"></iframe>` : match;
})
.replace(/\[(\w+)\](?!\s+<|\()/g, (match, ref) => {
const url = refs.get(ref);
const isExternal = url && url.startsWith("http://") || url && url.startsWith("https://");
return url ?
isExternal ?
`<a href="${url + (url.includes("?") ? "&" : "?")}ref=blog.webb.page" target="_blank">[${ref}]</a>` :
`<a href="${url}">[${ref}]</a>` :
match;
})
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, text, url) => {
const isExternal = url.startsWith("http://") || url.startsWith("https://");
return isExternal ?
`<a href="${url}?ref=blog.webb.page" target="_blank">${text}</a>` :
`<a href="${url}">${text}</a>`;
});
/*** restore inline code ***/
for (const block in inlineCode) {
processed = processed.replace(`<!--INLINE:CODE:${block}-->`, `<code>${escapeHtml(inlineCode[block])}</code>`);
}
/*** restore code blocks ***/
for (const block of codeBlocks) {
const langAttr = block.language ? ` data-language="${block.language}"` : "";
const html = `<pre><code${langAttr}>\n${escapeHtml(block.content)}\n\n</code></pre>`;
processed = processed.replace(`<!--CODE:BLOCK:${block.index}-->`, html);
}
// console.log(processed); /*** for troubleshooting ***/
return processed.trimEnd();
}
/*** HELPER ------------------------------------------- ***/
function escapeHtml(str: string): string {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function processBlockquotes(input: string): string {
const lines = input.split("\n");
const result: string[] = [];
let quoteBuffer: string[] = [];
const flushQuote = () => {
if (quoteBuffer.length > 0) {
/*** special indentation to fit memo/remark formatting ***/
result.push(`<blockquote>\n ${quoteBuffer.join("\n ")}\n\n</blockquote>`);
quoteBuffer = [];
// TODO
// : handle links within blockquotes (WM-032)
}
};
for (const line of lines) {
/*** preserve code block placeholders ***/
if (line.includes("<!--CODE:BLOCK") || line.includes("<!--INLINE:CODE")) {
flushQuote();
result.push(line);
continue;
}
const quoteMatch = line.match(/\s*>\s?(.*)$/);
if (quoteMatch) {
quoteBuffer.push(quoteMatch[1]);
} else {
flushQuote();
result.push(line);
}
}
flushQuote();
return result.join("\n");
}
|