@dogsbay/format-hugo 0.2.0-beta.182 → 0.2.0-beta.184
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/conventions.d.ts +15 -0
- package/dist/adapters/conventions.js +213 -0
- package/dist/adapters/datadog.d.ts +23 -0
- package/dist/adapters/datadog.js +376 -0
- package/dist/adapters/docsy.d.ts +9 -0
- package/dist/adapters/docsy.js +57 -0
- package/dist/adapters/index.d.ts +28 -0
- package/dist/adapters/index.js +40 -0
- package/dist/adapters/kubernetes.d.ts +22 -0
- package/dist/adapters/kubernetes.js +738 -0
- package/dist/builtins.d.ts +4 -1
- package/dist/builtins.js +8 -111
- package/dist/choice-html.d.ts +9 -0
- package/dist/choice-html.js +57 -0
- package/dist/cli.d.ts +46 -1
- package/dist/cli.js +466 -11
- package/dist/config.d.ts +16 -0
- package/dist/config.js +121 -2
- package/dist/html-blocks.d.ts +42 -0
- package/dist/html-blocks.js +671 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/intent.d.ts +123 -0
- package/dist/intent.js +357 -0
- package/dist/references.d.ts +29 -0
- package/dist/references.js +91 -0
- package/dist/registry.d.ts +116 -2
- package/dist/resolve.d.ts +27 -0
- package/dist/resolve.js +32 -6
- package/dist/shortcodes.d.ts +21 -21
- package/dist/shortcodes.js +60 -27
- package/dist/static.d.ts +25 -0
- package/dist/static.js +195 -0
- package/dist/translate.d.ts +30 -3
- package/dist/translate.js +175 -18
- package/package.json +3 -3
- package/dist/kubernetes.d.ts +0 -13
- package/dist/kubernetes.js +0 -185
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A site's HTML blocks → Dogsbay MD directives (an adapter's `htmlBlocks`).
|
|
3
|
+
*
|
|
4
|
+
* Hugo pages hold raw HTML, and a site's styled boxes are often HTML rather than a shortcode:
|
|
5
|
+
* Datadog writes its notes as `<div class="alert alert-info">…</div>`, 2,327 of them. Raw HTML is
|
|
6
|
+
* an HTML block to Markdown, so everything inside stayed raw — and a reader-choice value inside
|
|
7
|
+
* one could only show the default's value (choice-html.ts). Made a directive, its body is
|
|
8
|
+
* Markdown and a value in it switches like any other.
|
|
9
|
+
*
|
|
10
|
+
* Runs on the TRANSLATED page (shortcodes already mapped, so line numbers in the ledger are the
|
|
11
|
+
* source's), before `tidy` and `resolveIntent`: a value's mark inside `<code>` becomes a mark
|
|
12
|
+
* inside a code span, which resolveIntent writes as a `{choices}` span. Marks are hidden while a
|
|
13
|
+
* box is converted (their text holds `"`, `[`, `]`), so nothing here escapes or splits one.
|
|
14
|
+
*
|
|
15
|
+
* What the box's text MEANT follows Hugo: up to the box's first blank line it is an HTML block,
|
|
16
|
+
* where text is literal, so a line that would start a Markdown block there is escaped; after a
|
|
17
|
+
* blank line Hugo reads Markdown (Datadog writes lists and reference links there), kept as
|
|
18
|
+
* written. Inline formatting characters are not escaped: Datadog's HTML text holds almost none
|
|
19
|
+
* (0 backticks, 1 `**` in 1,633 one-line boxes), and the text also holds shortcode output that IS
|
|
20
|
+
* Markdown (`{{< ui >}}` → `**…**`), which cannot be told apart from it any more.
|
|
21
|
+
*
|
|
22
|
+
* A box is converted only when its whole body reads: text and the inline tags in
|
|
23
|
+
* `convertInlineHtml`, paragraphs, flat lists. Anything else (a table, a code block, a nested
|
|
24
|
+
* `<div>`, a tag it cannot read, a link it would not write) keeps the box as HTML, as before, and
|
|
25
|
+
* the ledger says why. Linear on any input: the page is someone else's.
|
|
26
|
+
*/
|
|
27
|
+
import { createFenceTracker } from "@dogsbay/format-dogsbay-md/browser";
|
|
28
|
+
import { codeRanges } from "./shortcodes.js";
|
|
29
|
+
/** Past this, a box is left as written (a missing closer must not make the rest of the page one box). */
|
|
30
|
+
const MAX_BODY_CHARS = 10_000;
|
|
31
|
+
/** A tag longer than this is not read (each failed read would otherwise scan to the line's end). */
|
|
32
|
+
const MAX_TAG_CHARS = 2_000;
|
|
33
|
+
/** A reader-choice mark (intent.ts): `U+FDD0 … U+FDD1`, nothing nested. */
|
|
34
|
+
const MARK = /[^]*/g;
|
|
35
|
+
/** A hidden mark while a box converts: its index between the same two characters, digits only. */
|
|
36
|
+
const PLACEHOLDER = /(\d+)/g;
|
|
37
|
+
const LIST_ITEM = /^([ \t]*)([-+*:]|\d{1,9}[.)])([ \t]+|$)/;
|
|
38
|
+
export function rewriteHtmlBlocks(markdown, mappings, report,
|
|
39
|
+
/** The page as written, to give a box's ledger entry its source line. */
|
|
40
|
+
source) {
|
|
41
|
+
if (mappings.length === 0)
|
|
42
|
+
return markdown;
|
|
43
|
+
const tags = new Set(mappings.map((m) => m.tag));
|
|
44
|
+
// a quick refusal: no opener for any mapped element anywhere
|
|
45
|
+
const lower = markdown.toLowerCase();
|
|
46
|
+
if (![...tags].some((t) => lower.includes(`<${t}`)))
|
|
47
|
+
return markdown;
|
|
48
|
+
const lines = markdown.split("\n");
|
|
49
|
+
const tracker = createFenceTracker();
|
|
50
|
+
const out = [];
|
|
51
|
+
const sourceLines = lineFinder(source);
|
|
52
|
+
// where each open list item's text starts (outermost first), so an opener indented four or
|
|
53
|
+
// more is known to be in an item (Datadog's ` <div …>` under `1. Step`) and not indented code
|
|
54
|
+
const items = [];
|
|
55
|
+
let blank = true;
|
|
56
|
+
// what every box's search for its end tag may read in all, in proportion to the page: thousands
|
|
57
|
+
// of unclosed boxes would otherwise each read up to MAX_BODY_CHARS
|
|
58
|
+
const budget = { left: 4 * markdown.length + MAX_BODY_CHARS };
|
|
59
|
+
for (let i = 0; i < lines.length; i++) {
|
|
60
|
+
const line = lines[i];
|
|
61
|
+
if (tracker.step(line) !== "prose") {
|
|
62
|
+
out.push(line);
|
|
63
|
+
blank = false;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const wasBlank = blank;
|
|
67
|
+
blank = line.trim() === "";
|
|
68
|
+
const item = LIST_ITEM.exec(line);
|
|
69
|
+
if (item) {
|
|
70
|
+
const marker = item[1].replace(/\t/g, " ").length;
|
|
71
|
+
while (items.length > 0 && items[items.length - 1] > marker)
|
|
72
|
+
items.pop();
|
|
73
|
+
items.push(marker + item[2].length + Math.max(1, item[3].length));
|
|
74
|
+
}
|
|
75
|
+
else if (!blank && wasBlank && !/^[ \t]/.test(line))
|
|
76
|
+
items.length = 0;
|
|
77
|
+
const opener = /^([ \t]*)<([a-zA-Z][\w-]*)/.exec(line);
|
|
78
|
+
if (!opener || !tags.has(opener[2].toLowerCase())) {
|
|
79
|
+
out.push(line);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const indent = opener[1];
|
|
83
|
+
const width = indent.replace(/\t/g, " ").length;
|
|
84
|
+
if (width >= 4 && !items.some((c) => width >= c && width - c < 4)) {
|
|
85
|
+
out.push(line); // indented code, or an indent we cannot place
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const tagName = opener[2].toLowerCase();
|
|
89
|
+
const open = readTag(line, indent.length);
|
|
90
|
+
if (!open || open.closing) {
|
|
91
|
+
out.push(line);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const classes = (open.attrs.class ?? "").split(/\s+/).filter(Boolean);
|
|
95
|
+
let mapping;
|
|
96
|
+
let name;
|
|
97
|
+
for (const m of mappings) {
|
|
98
|
+
if (m.tag !== tagName)
|
|
99
|
+
continue;
|
|
100
|
+
name = m.directive(classes);
|
|
101
|
+
if (name) {
|
|
102
|
+
mapping = m;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (!mapping || !name) {
|
|
107
|
+
out.push(line);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const at = sourceLines.next(line.slice(indent.length, open.end));
|
|
111
|
+
const kept = (why) => {
|
|
112
|
+
report({ shortcode: "(html)", action: "preserved", note: `${mapping.name} kept as HTML: ${why}`, line: at });
|
|
113
|
+
out.push(line);
|
|
114
|
+
};
|
|
115
|
+
// The box's text, from after its opening tag to its closing tag, which may be lines later.
|
|
116
|
+
const found = findClose(lines, i, open.end, tagName, budget);
|
|
117
|
+
if (typeof found === "string") {
|
|
118
|
+
kept(found);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const marks = [];
|
|
122
|
+
const hidden = found.body.replace(MARK, (m) => `${marks.push(m) - 1}`);
|
|
123
|
+
const converted = convertInlineHtml(hidden);
|
|
124
|
+
if (typeof converted !== "string") {
|
|
125
|
+
kept(converted.why);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const body = converted
|
|
129
|
+
.replace(PLACEHOLDER, (_m, n) => marks[Number(n)])
|
|
130
|
+
.split("\n")
|
|
131
|
+
.map((l) => (l.trim() === "" ? "" : indent + l))
|
|
132
|
+
.join("\n");
|
|
133
|
+
out.push(`${indent}:::${name}`);
|
|
134
|
+
if (body.trim() !== "")
|
|
135
|
+
out.push(body);
|
|
136
|
+
out.push(`${indent}:::`);
|
|
137
|
+
// text after the end tag on its line is still in Hugo's HTML block: literal
|
|
138
|
+
const rest = found.after.trim();
|
|
139
|
+
// (raw HTML there — another box, a comment — stays HTML, as Hugo had it)
|
|
140
|
+
if (rest !== "")
|
|
141
|
+
out.push("", indent + (rest.startsWith("<") ? rest : escapeLineStart(rest)));
|
|
142
|
+
// the lines the box took are spent; none was a fence (findClose refused those)
|
|
143
|
+
for (let k = i + 1; k <= found.lastLine; k++)
|
|
144
|
+
tracker.step(lines[k]);
|
|
145
|
+
i = found.lastLine;
|
|
146
|
+
blank = false;
|
|
147
|
+
}
|
|
148
|
+
return out.join("\n");
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The source line of each opening tag in turn: the next occurrence of the same text after the
|
|
152
|
+
* last one found, outside code (a code example showing the box is not it), newlines counted from
|
|
153
|
+
* there — linear over a page of thousands of boxes. 0 when the text is not in the source (a box
|
|
154
|
+
* from an included file).
|
|
155
|
+
*/
|
|
156
|
+
function lineFinder(source) {
|
|
157
|
+
const last = new Map();
|
|
158
|
+
let code;
|
|
159
|
+
const inCode = (at) => {
|
|
160
|
+
code ??= codeRanges(source);
|
|
161
|
+
let lo = 0;
|
|
162
|
+
let hi = code.length - 1;
|
|
163
|
+
while (lo <= hi) {
|
|
164
|
+
const mid = (lo + hi) >> 1;
|
|
165
|
+
const r = code[mid];
|
|
166
|
+
if (at < r.start)
|
|
167
|
+
hi = mid - 1;
|
|
168
|
+
else if (at >= r.end)
|
|
169
|
+
lo = mid + 1;
|
|
170
|
+
else
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
};
|
|
175
|
+
return {
|
|
176
|
+
next(text) {
|
|
177
|
+
const prev = last.get(text) ?? { at: -1, line: 1 };
|
|
178
|
+
if (prev.line === 0)
|
|
179
|
+
return 0;
|
|
180
|
+
let at = source.indexOf(text, prev.at + 1);
|
|
181
|
+
while (at >= 0 && inCode(at))
|
|
182
|
+
at = source.indexOf(text, at + 1);
|
|
183
|
+
if (at < 0) {
|
|
184
|
+
last.set(text, { at: source.length, line: 0 });
|
|
185
|
+
return 0;
|
|
186
|
+
}
|
|
187
|
+
let line = prev.line;
|
|
188
|
+
for (let k = Math.max(prev.at, 0); k < at; k++)
|
|
189
|
+
if (source.charCodeAt(k) === 10)
|
|
190
|
+
line++;
|
|
191
|
+
last.set(text, { at, line });
|
|
192
|
+
return line;
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The text between the opening tag (line `first`, column `from`) and its matching closing tag,
|
|
198
|
+
* counting nested elements of the same name. Refuses (a reason) at a code fence, past the size
|
|
199
|
+
* cap or at the page's end.
|
|
200
|
+
*/
|
|
201
|
+
function findClose(lines, first, from, tag, budget) {
|
|
202
|
+
const fences = createFenceTracker();
|
|
203
|
+
let depth = 1;
|
|
204
|
+
let body = "";
|
|
205
|
+
for (let i = first; i < lines.length; i++) {
|
|
206
|
+
const line = lines[i];
|
|
207
|
+
if (i > first && fences.step(line) !== "prose")
|
|
208
|
+
return "it holds a code block";
|
|
209
|
+
if (body.length + line.length > MAX_BODY_CHARS)
|
|
210
|
+
return "too long, or its closing tag was not found";
|
|
211
|
+
budget.left -= line.length + 1;
|
|
212
|
+
if (budget.left < 0)
|
|
213
|
+
return "the page's boxes are too long to read, or their closing tags were not found";
|
|
214
|
+
const start = i === first ? from : 0;
|
|
215
|
+
let k = start;
|
|
216
|
+
while (k < line.length) {
|
|
217
|
+
const lt = line.indexOf("<", k);
|
|
218
|
+
if (lt < 0)
|
|
219
|
+
break;
|
|
220
|
+
const close = line[lt + 1] === "/";
|
|
221
|
+
const at = lt + (close ? 2 : 1);
|
|
222
|
+
// only a tag of this name is read: no work at any other `<`
|
|
223
|
+
if (line.slice(at, at + tag.length).toLowerCase() === tag && !/[\w-]/.test(line[at + tag.length] ?? "")) {
|
|
224
|
+
const t = readTag(line, lt);
|
|
225
|
+
if (t) {
|
|
226
|
+
depth += t.closing ? -1 : t.selfClosing ? 0 : 1;
|
|
227
|
+
if (depth === 0) {
|
|
228
|
+
body += line.slice(start, lt);
|
|
229
|
+
return { body, after: line.slice(t.end), lastLine: i };
|
|
230
|
+
}
|
|
231
|
+
k = t.end;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
k = lt + 1;
|
|
236
|
+
}
|
|
237
|
+
body += line.slice(start) + "\n";
|
|
238
|
+
}
|
|
239
|
+
return "its closing tag was not found";
|
|
240
|
+
}
|
|
241
|
+
/** An HTML tag at `at` (`<` there), read by hand: one pass, no backtracking, at most MAX_TAG_CHARS. */
|
|
242
|
+
export function readTag(text, at) {
|
|
243
|
+
if (text[at] !== "<")
|
|
244
|
+
return undefined;
|
|
245
|
+
const limit = Math.min(text.length, at + MAX_TAG_CHARS);
|
|
246
|
+
let k = at + 1;
|
|
247
|
+
const closing = text[k] === "/";
|
|
248
|
+
if (closing)
|
|
249
|
+
k++;
|
|
250
|
+
const nameStart = k;
|
|
251
|
+
if (!/[a-zA-Z]/.test(text[k] ?? ""))
|
|
252
|
+
return undefined;
|
|
253
|
+
while (k < limit && /[\w-]/.test(text[k]))
|
|
254
|
+
k++;
|
|
255
|
+
const name = text.slice(nameStart, k).toLowerCase();
|
|
256
|
+
const attrs = Object.create(null);
|
|
257
|
+
for (;;) {
|
|
258
|
+
const before = k;
|
|
259
|
+
while (k < limit && /\s/.test(text[k]))
|
|
260
|
+
k++;
|
|
261
|
+
if (k >= limit)
|
|
262
|
+
return undefined;
|
|
263
|
+
if (text[k] === ">")
|
|
264
|
+
return { name, closing, selfClosing: false, attrs, end: k + 1 };
|
|
265
|
+
if (text[k] === "/" && text[k + 1] === ">")
|
|
266
|
+
return { name, closing, selfClosing: true, attrs, end: k + 2 };
|
|
267
|
+
if (k === before)
|
|
268
|
+
return undefined; // an attribute must follow a space
|
|
269
|
+
const attrStart = k;
|
|
270
|
+
while (k < limit && !/[\s=>"'/]/.test(text[k]))
|
|
271
|
+
k++;
|
|
272
|
+
if (k === attrStart)
|
|
273
|
+
return undefined;
|
|
274
|
+
const attr = text.slice(attrStart, k).toLowerCase();
|
|
275
|
+
let j = k;
|
|
276
|
+
while (j < limit && /\s/.test(text[j]))
|
|
277
|
+
j++;
|
|
278
|
+
let value = "";
|
|
279
|
+
if (text[j] === "=") {
|
|
280
|
+
j++;
|
|
281
|
+
while (j < limit && /\s/.test(text[j]))
|
|
282
|
+
j++;
|
|
283
|
+
const q = text[j];
|
|
284
|
+
if (q === '"' || q === "'") {
|
|
285
|
+
const close = text.indexOf(q, j + 1);
|
|
286
|
+
if (close < 0 || close >= limit)
|
|
287
|
+
return undefined;
|
|
288
|
+
value = text.slice(j + 1, close);
|
|
289
|
+
j = close + 1;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const vs = j;
|
|
293
|
+
while (j < limit && !/[\s>]/.test(text[j]))
|
|
294
|
+
j++;
|
|
295
|
+
if (j === vs)
|
|
296
|
+
return undefined;
|
|
297
|
+
value = text.slice(vs, j);
|
|
298
|
+
}
|
|
299
|
+
k = j;
|
|
300
|
+
}
|
|
301
|
+
if (!(attr in attrs))
|
|
302
|
+
attrs[attr] = value;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const ENTITIES = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'", nbsp: " " };
|
|
306
|
+
/** Entities decoded. Undefined at one it does not know, or a code point that is no character. */
|
|
307
|
+
function decodeEntities(text) {
|
|
308
|
+
let bad = false;
|
|
309
|
+
const out = text.replace(/&(#x[0-9a-fA-F]{1,6}|#[0-9]{1,7}|[a-zA-Z]{2,8});/g, (m, e) => {
|
|
310
|
+
if (e[0] === "#") {
|
|
311
|
+
const code = e[1] === "x" || e[1] === "X" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
|
312
|
+
if (code <= 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) {
|
|
313
|
+
bad = true;
|
|
314
|
+
return m;
|
|
315
|
+
}
|
|
316
|
+
return String.fromCodePoint(code);
|
|
317
|
+
}
|
|
318
|
+
const v = ENTITIES[e.toLowerCase()];
|
|
319
|
+
if (v === undefined)
|
|
320
|
+
bad = true;
|
|
321
|
+
return v ?? m;
|
|
322
|
+
});
|
|
323
|
+
return bad ? undefined : out;
|
|
324
|
+
}
|
|
325
|
+
/** A code span for `code`: a backtick run longer than any inside, spaced when it starts or ends with one. */
|
|
326
|
+
function codeSpan(code) {
|
|
327
|
+
let longest = 0;
|
|
328
|
+
for (const m of code.matchAll(/`+/g))
|
|
329
|
+
longest = Math.max(longest, m[0].length);
|
|
330
|
+
const ticks = "`".repeat(longest + 1);
|
|
331
|
+
const pad = code.startsWith("`") || code.endsWith("`") || (code.startsWith(" ") && code.endsWith(" ") && code.trim() !== "") ? " " : "";
|
|
332
|
+
return `${ticks}${pad}${code}${pad}${ticks}`;
|
|
333
|
+
}
|
|
334
|
+
/** http, https, mailto or a path — read as a browser would, entities decoded and spaces dropped. */
|
|
335
|
+
function isSafeHref(href) {
|
|
336
|
+
const decoded = decodeEntities(href);
|
|
337
|
+
if (decoded === undefined)
|
|
338
|
+
return false;
|
|
339
|
+
const bare = decoded.replace(/[\s\u0000-\u001f\u007f]/g, "");
|
|
340
|
+
const scheme = /^([a-zA-Z][a-zA-Z0-9+.-]*):/.exec(bare);
|
|
341
|
+
if (!scheme)
|
|
342
|
+
return true;
|
|
343
|
+
return ["http", "https", "mailto"].includes(scheme[1].toLowerCase());
|
|
344
|
+
}
|
|
345
|
+
/** A Markdown destination: bracketed when it holds a space, `(`, `)`, `<` or `>`; a backslash encoded. */
|
|
346
|
+
function destination(href) {
|
|
347
|
+
const h = href.replace(/\\/g, "%5C");
|
|
348
|
+
return /[\s()<>]/.test(h) ? `<${h.replace(/[<>]/g, (c) => (c === "<" ? "%3C" : "%3E"))}>` : h;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* A line of literal text that would start a Markdown block (a heading, a list item, a quote, a
|
|
352
|
+
* table row, a thematic break or setext underline, a fence, a directive, an HTML block, math)
|
|
353
|
+
* gets a backslash before it.
|
|
354
|
+
*/
|
|
355
|
+
export function escapeLineStart(line) {
|
|
356
|
+
const number = /^\d{1,9}(?=[.)](\s|$))/.exec(line);
|
|
357
|
+
if (number)
|
|
358
|
+
return `${number[0]}\\${line.slice(number[0].length)}`;
|
|
359
|
+
const colons = /^:{2,}/.exec(line);
|
|
360
|
+
if (colons)
|
|
361
|
+
return colons[0].replace(/:/g, "\\:") + line.slice(colons[0].length);
|
|
362
|
+
if (/^(#{1,6}(\s|$)|[-+*:](\s|$)|[-=_*]{2,}|[>|]|`{3,}|~{3,}|\$\$)/.test(line))
|
|
363
|
+
return `\\${line}`;
|
|
364
|
+
return line;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* A box's inner HTML as Markdown, or why not. Inline tags only: `code`, `a` (a safe `href`),
|
|
368
|
+
* `strong`/`b`, `em`/`i`, `kbd`, a plain `span`, `br` and `p` (paragraph breaks), and flat
|
|
369
|
+
* `ul`/`ol` lists of inline items. Up to the first blank line the text is HTML: each line's
|
|
370
|
+
* indent is dropped and a line that would start a Markdown block is escaped. After it, Hugo reads
|
|
371
|
+
* Markdown: the text keeps its lines, out-dented by the indent they share.
|
|
372
|
+
*/
|
|
373
|
+
export function convertInlineHtml(html) {
|
|
374
|
+
const stack = [{ tag: "", out: "" }];
|
|
375
|
+
const top = () => stack[stack.length - 1];
|
|
376
|
+
const refuse = (why) => ({ ok: false, why });
|
|
377
|
+
const inList = () => stack.some((f) => f.tag === "ul" || f.tag === "ol");
|
|
378
|
+
const inLink = () => stack.some((f) => f.tag === "a");
|
|
379
|
+
// where Hugo's HTML block ends: the first blank line
|
|
380
|
+
const blankLine = /\n[ \t]*\n/.exec(html);
|
|
381
|
+
const htmlEnd = blankLine ? blankLine.index : html.length;
|
|
382
|
+
// the indent the Markdown part's lines share
|
|
383
|
+
let shared = Infinity;
|
|
384
|
+
if (blankLine) {
|
|
385
|
+
for (const l of html.slice(htmlEnd + 1).split("\n")) {
|
|
386
|
+
if (l.trim() === "")
|
|
387
|
+
continue;
|
|
388
|
+
shared = Math.min(shared, l.length - l.trimStart().length);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (shared === Infinity)
|
|
392
|
+
shared = 0;
|
|
393
|
+
/** Text from `from` to `to` of the source, as Markdown, onto the top frame. */
|
|
394
|
+
const addText = (from, to) => {
|
|
395
|
+
const frame = top();
|
|
396
|
+
if (frame.tag === "ul" || frame.tag === "ol") {
|
|
397
|
+
// between a list's items only spaces mean nothing; words there belong to no item
|
|
398
|
+
return html.slice(from, to).trim() === "" ? undefined : "text in a list outside its items";
|
|
399
|
+
}
|
|
400
|
+
let text = "";
|
|
401
|
+
if (frame.tag === "code")
|
|
402
|
+
text = html.slice(from, to);
|
|
403
|
+
else {
|
|
404
|
+
const split = Math.max(from, Math.min(to, htmlEnd));
|
|
405
|
+
if (split > from) {
|
|
406
|
+
const raw = html.slice(from, split);
|
|
407
|
+
// a `:::name` (or a closer line) here is shortcode output from a block in the box, or text
|
|
408
|
+
// the importer's tidy would put on a line of its own: either way not text to escape
|
|
409
|
+
if (/:{3,}[A-Za-z{]/.test(raw) || /(^|\n)[ \t]*:{3,}[ \t]*(\n|$)/.test(raw))
|
|
410
|
+
return "a block directive inside its HTML";
|
|
411
|
+
text += literal(raw, frame);
|
|
412
|
+
}
|
|
413
|
+
if (to > split)
|
|
414
|
+
text += markdownText(html.slice(split, to), split === 0 || html[split - 1] === "\n");
|
|
415
|
+
if (inLink())
|
|
416
|
+
text = text.replace(/[[\]]/g, (c) => `\\${c}`);
|
|
417
|
+
}
|
|
418
|
+
if (frame.guardBrace && text.startsWith("{"))
|
|
419
|
+
text = `\\${text}`;
|
|
420
|
+
frame.guardBrace = false;
|
|
421
|
+
frame.out += text;
|
|
422
|
+
return undefined;
|
|
423
|
+
};
|
|
424
|
+
/** HTML text: each line's indent dropped, a block start escaped where a line of the box or an item starts. */
|
|
425
|
+
const literal = (text, frame) => text
|
|
426
|
+
.split("\n")
|
|
427
|
+
.map((p, i) => {
|
|
428
|
+
const atStart = i > 0 || ((frame.tag === "" || frame.tag === "li") && (frame.out === "" || frame.out.endsWith("\n")));
|
|
429
|
+
return atStart ? escapeLineStart(p.trimStart()) : p;
|
|
430
|
+
})
|
|
431
|
+
.join("\n")
|
|
432
|
+
// a `{…}` ending a paragraph would be its attributes: braces are text here, except a span
|
|
433
|
+
// directive's own group (`:when[…]{site=…}`, shortcode output) straight after its `]`
|
|
434
|
+
.replace(/(^|[^\]])\{/g, "$1\\{");
|
|
435
|
+
/** Markdown text (after the first blank line): kept, out-dented by the shared indent. */
|
|
436
|
+
const markdownText = (text, lineStart) => text
|
|
437
|
+
.split("\n")
|
|
438
|
+
.map((p, i) => (i > 0 || lineStart ? p.slice(Math.min(shared, p.length - p.trimStart().length)) : p))
|
|
439
|
+
.join("\n");
|
|
440
|
+
let k = 0;
|
|
441
|
+
while (k < html.length) {
|
|
442
|
+
const lt = html.indexOf("<", k);
|
|
443
|
+
const end = lt < 0 ? html.length : lt;
|
|
444
|
+
if (end > k) {
|
|
445
|
+
const err = addText(k, end);
|
|
446
|
+
if (err)
|
|
447
|
+
return refuse(err);
|
|
448
|
+
}
|
|
449
|
+
if (lt < 0)
|
|
450
|
+
break;
|
|
451
|
+
// a shortcode left as written (`{{< x >}}`) is text, `>` and all
|
|
452
|
+
if (lt >= 2 && html[lt - 1] === "{" && html[lt - 2] === "{") {
|
|
453
|
+
const close = html.indexOf(">}}", lt);
|
|
454
|
+
if (close < 0)
|
|
455
|
+
return refuse("a shortcode in it is not closed");
|
|
456
|
+
const err = addText(lt, close + 3);
|
|
457
|
+
if (err)
|
|
458
|
+
return refuse(err);
|
|
459
|
+
k = close + 3;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
if (html.startsWith("<!--", lt))
|
|
463
|
+
return refuse("it holds an HTML comment");
|
|
464
|
+
const t = readTag(html, lt);
|
|
465
|
+
if (!t) {
|
|
466
|
+
// a `<` that starts no tag is text, as in HTML; one that looks like a tag and cannot be
|
|
467
|
+
// read would leave its end tag to close nothing
|
|
468
|
+
if (/[a-zA-Z/!?]/.test(html[lt + 1] ?? ""))
|
|
469
|
+
return refuse("a tag it cannot read");
|
|
470
|
+
const err = addText(lt, lt + 1);
|
|
471
|
+
if (err)
|
|
472
|
+
return refuse(err);
|
|
473
|
+
k = lt + 1;
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
k = t.end;
|
|
477
|
+
const frame = top();
|
|
478
|
+
frame.guardBrace = false;
|
|
479
|
+
if (frame.tag === "code" && !(t.name === "code" && t.closing))
|
|
480
|
+
return refuse(`<${t.name}> inside <code>`);
|
|
481
|
+
const attrNames = Object.keys(t.attrs);
|
|
482
|
+
switch (t.name) {
|
|
483
|
+
case "br":
|
|
484
|
+
if (stack.length > 1) {
|
|
485
|
+
if (frame.tag === "li")
|
|
486
|
+
frame.out += " ";
|
|
487
|
+
else
|
|
488
|
+
return refuse("a <br> inside formatting");
|
|
489
|
+
}
|
|
490
|
+
else
|
|
491
|
+
frame.out += "\n\n";
|
|
492
|
+
continue;
|
|
493
|
+
case "p":
|
|
494
|
+
if (stack.length > 1)
|
|
495
|
+
return refuse("a <p> inside formatting or a list");
|
|
496
|
+
frame.out += "\n\n";
|
|
497
|
+
continue;
|
|
498
|
+
case "ul":
|
|
499
|
+
case "ol":
|
|
500
|
+
if (t.closing) {
|
|
501
|
+
if (frame.tag === "li")
|
|
502
|
+
closeFrame(stack);
|
|
503
|
+
if (top().tag !== t.name)
|
|
504
|
+
return refuse(`</${t.name}> out of place`);
|
|
505
|
+
const list = stack.pop();
|
|
506
|
+
top().out += `${list.out}\n\n`;
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
if (stack.length > 1)
|
|
510
|
+
return refuse("a list inside a list or formatting");
|
|
511
|
+
if (attrNames.length > 0)
|
|
512
|
+
return refuse(`<${t.name}> with attributes`);
|
|
513
|
+
frame.out += "\n\n";
|
|
514
|
+
stack.push({ tag: t.name, out: "" });
|
|
515
|
+
}
|
|
516
|
+
continue;
|
|
517
|
+
case "li": {
|
|
518
|
+
if (!inList())
|
|
519
|
+
return refuse("an <li> outside a list");
|
|
520
|
+
if (t.closing) {
|
|
521
|
+
if (frame.tag !== "li")
|
|
522
|
+
return refuse("</li> out of place");
|
|
523
|
+
closeFrame(stack);
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
if (frame.tag === "li")
|
|
527
|
+
closeFrame(stack); // an unclosed item ends at the next
|
|
528
|
+
if (top().tag !== "ul" && top().tag !== "ol")
|
|
529
|
+
return refuse("an <li> inside formatting");
|
|
530
|
+
stack.push({ tag: "li", out: "" });
|
|
531
|
+
}
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
case "code":
|
|
535
|
+
case "a":
|
|
536
|
+
case "strong":
|
|
537
|
+
case "b":
|
|
538
|
+
case "em":
|
|
539
|
+
case "i":
|
|
540
|
+
case "kbd":
|
|
541
|
+
case "span": {
|
|
542
|
+
if (t.closing) {
|
|
543
|
+
// an end tag with nothing to close is ignored, as browsers do (`text.</a>`: 39 boxes)
|
|
544
|
+
if (!stack.some((f) => f.tag === t.name))
|
|
545
|
+
continue;
|
|
546
|
+
if (frame.tag !== t.name)
|
|
547
|
+
return refuse(`</${t.name}> out of place`);
|
|
548
|
+
const err = closeFrame(stack);
|
|
549
|
+
if (err)
|
|
550
|
+
return refuse(err);
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (t.selfClosing)
|
|
554
|
+
return refuse(`<${t.name} />`);
|
|
555
|
+
// a link keeps only its href (a class, a title, data-* mean nothing in Markdown); an
|
|
556
|
+
// event handler is not something to drop quietly
|
|
557
|
+
const extra = attrNames.filter((a) => (t.name === "a" ? /^on/.test(a) : true));
|
|
558
|
+
if (extra.length > 0)
|
|
559
|
+
return refuse(`<${t.name}> with ${extra.join(", ")}`);
|
|
560
|
+
if (t.name === "a") {
|
|
561
|
+
if (inLink())
|
|
562
|
+
return refuse("a link inside a link");
|
|
563
|
+
const href = (t.attrs.href ?? "").trim();
|
|
564
|
+
if (!href)
|
|
565
|
+
return refuse("a link with no href");
|
|
566
|
+
if (//.test(href))
|
|
567
|
+
return refuse("a reader-choice value in a link's address");
|
|
568
|
+
if (!isSafeHref(href))
|
|
569
|
+
return refuse("a link that is not http, https, mailto or a path");
|
|
570
|
+
stack.push({ tag: "a", out: "", href });
|
|
571
|
+
}
|
|
572
|
+
else
|
|
573
|
+
stack.push({ tag: t.name, out: "" });
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
default:
|
|
577
|
+
return refuse(`it holds <${t.name}>`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (stack.length > 1) {
|
|
581
|
+
// an item left open at the box's end closes with it; anything else is unbalanced
|
|
582
|
+
if (top().tag === "li")
|
|
583
|
+
closeFrame(stack);
|
|
584
|
+
const list = top();
|
|
585
|
+
if (list.tag === "ul" || list.tag === "ol") {
|
|
586
|
+
stack.pop();
|
|
587
|
+
top().out += `${list.out}\n\n`;
|
|
588
|
+
}
|
|
589
|
+
if (stack.length > 1)
|
|
590
|
+
return refuse(`<${top().tag}> is not closed`);
|
|
591
|
+
}
|
|
592
|
+
return tidyBody(stack[0].out);
|
|
593
|
+
}
|
|
594
|
+
/** Lines joined with one space where they meet (linear: no `\s*\n\s*` regex on someone else's text). */
|
|
595
|
+
function oneLine(text) {
|
|
596
|
+
if (!text.includes("\n"))
|
|
597
|
+
return text;
|
|
598
|
+
const parts = text.split("\n");
|
|
599
|
+
return parts.map((p, i) => (i === 0 ? p.trimEnd() : i === parts.length - 1 ? p.trimStart() : p.trim())).join(" ");
|
|
600
|
+
}
|
|
601
|
+
/** Close the top frame into the one below it; a reason when it cannot be written. */
|
|
602
|
+
function closeFrame(stack) {
|
|
603
|
+
const f = stack.pop();
|
|
604
|
+
const parent = stack[stack.length - 1];
|
|
605
|
+
const inner = f.out;
|
|
606
|
+
switch (f.tag) {
|
|
607
|
+
case "code": {
|
|
608
|
+
// decoded, THEN one line: a ` ` must not put a newline in the span
|
|
609
|
+
const code = decodeEntities(inner);
|
|
610
|
+
if (code === undefined)
|
|
611
|
+
return "an entity in <code> it cannot read";
|
|
612
|
+
// the importer's tidy would put a `:::name` on a line of its own, even in a span
|
|
613
|
+
if (/:{3,}[A-Za-z]/.test(code))
|
|
614
|
+
return "`:::` in <code>";
|
|
615
|
+
const flat = oneLine(code.replace(/\r/g, ""));
|
|
616
|
+
parent.out += flat === "" ? "" : codeSpan(flat);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
case "li": {
|
|
620
|
+
const marker = parent.tag === "ol" ? "1." : "-";
|
|
621
|
+
const item = oneLine(inner).trim();
|
|
622
|
+
parent.out += `\n${marker} ${item}`;
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
case "a": {
|
|
626
|
+
let label = oneLine(inner).trim();
|
|
627
|
+
if (label === "")
|
|
628
|
+
return "a link with no text";
|
|
629
|
+
// a backslash ending the label would escape its `]`
|
|
630
|
+
if (/(^|[^\\])(\\\\)*\\$/.test(label))
|
|
631
|
+
label += "\\";
|
|
632
|
+
parent.out += `[${label}](${destination(f.href)})`;
|
|
633
|
+
parent.guardBrace = true;
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
case "kbd": {
|
|
637
|
+
const key = inner.trim();
|
|
638
|
+
if (key === "" || /[\n[\]\\\uFDD0]/.test(key))
|
|
639
|
+
return "a <kbd> it cannot write";
|
|
640
|
+
parent.out += `:kbd[${key}]`;
|
|
641
|
+
parent.guardBrace = true;
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
case "span":
|
|
645
|
+
parent.out += inner;
|
|
646
|
+
return;
|
|
647
|
+
default: {
|
|
648
|
+
// strong / b / em / i: the marks hug the text, spaces go outside them
|
|
649
|
+
const mark = f.tag === "strong" || f.tag === "b" ? "**" : "*";
|
|
650
|
+
const lead = /^\s*/.exec(inner)[0];
|
|
651
|
+
const core = inner.slice(lead.length).trimEnd();
|
|
652
|
+
const trail = inner.slice(lead.length + core.length);
|
|
653
|
+
if (core === "")
|
|
654
|
+
parent.out += lead + trail;
|
|
655
|
+
else if (core.includes("\n\n"))
|
|
656
|
+
return `a paragraph break inside <${f.tag}>`;
|
|
657
|
+
else
|
|
658
|
+
parent.out += `${lead}${mark}${core}${mark}${trail}`;
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
/** Line ends trimmed, the first line's start too, blank runs to one, blank edges dropped. */
|
|
664
|
+
function tidyBody(md) {
|
|
665
|
+
return md
|
|
666
|
+
.split("\n")
|
|
667
|
+
.map((l, i) => (i === 0 ? l.trim() : l.trimEnd()))
|
|
668
|
+
.join("\n")
|
|
669
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
670
|
+
.replace(/^\n+|\n+$/g, "");
|
|
671
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { scanShortcodes, parseArgs, stripContentTypeMarkers, CONTENT_TYPE_MARKERS, type ScanNode, type ShortcodeNode, type TextNode, type ShortcodeArgs, type ShortcodeDelimiter, } from "./shortcodes.js";
|
|
2
|
-
export { detectHugo, loadHugoConfig, lookupParam, contentDirFor, type HugoConfig, type HugoLanguage, } from "./config.js";
|
|
3
|
-
export { arg, attrBlock, directive, composeRegistry, type ShortcodeMapping, type ShortcodeRegistry, type ShortcodeContext, type HugoLedgerEntry, } from "./registry.js";
|
|
2
|
+
export { detectHugo, loadHugoConfig, lookupParam, contentDirFor, loadHugoI18n, type HugoConfig, type HugoLanguage, } from "./config.js";
|
|
3
|
+
export { arg, attrBlock, directive, composeRegistry, type ShortcodeMapping, type ShortcodeRegistry, type ShortcodeContext, type HugoLedgerEntry, type HugoSiteAdapter, type HugoSiteFacts, } from "./registry.js";
|
|
4
4
|
export { BUILTIN_SHORTCODES } from "./builtins.js";
|
|
5
|
-
export { KUBERNETES_SHORTCODES } from "./kubernetes.js";
|
|
5
|
+
export { KUBERNETES_SHORTCODES } from "./adapters/kubernetes.js";
|
|
6
|
+
export { HUGO_ADAPTERS, CONVENTIONS, DOCSY, KUBERNETES, DATADOG, chooseAdapters, type ChosenAdapter } from "./adapters/index.js";
|
|
6
7
|
export { makeContentResolver, looksLikeFile } from "./resolve.js";
|
|
7
8
|
export { translateHugo, type TranslateOptions, type TranslateResult } from "./translate.js";
|
|
9
|
+
export { rewriteHtmlBlocks, convertInlineHtml, type HtmlBlockMapping } from "./html-blocks.js";
|
|
8
10
|
export { collectHugoPages, buildHugoNav, makeRefResolver, splitFrontmatter, type HugoPage, } from "./nav.js";
|
|
9
11
|
export { importHugo, summariseHugoLedger, plugin, type HugoImportResult, } from "./cli.js";
|