@dogsbay/format-hugo 0.2.0-beta.183 → 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
package/dist/cli.js
CHANGED
|
@@ -9,18 +9,123 @@
|
|
|
9
9
|
* on kubernetes/website) and resolving it needs the whole page set. That is
|
|
10
10
|
* why this is two passes, not one.
|
|
11
11
|
*/
|
|
12
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
13
|
-
import { join, resolve } from "node:path";
|
|
12
|
+
import { existsSync, opendirSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
13
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
14
14
|
import { dogsbayMdToTree, extractHeadings } from "@dogsbay/format-dogsbay-md";
|
|
15
|
-
import { detectHugo, loadHugoConfig, contentDirFor } from "./config.js";
|
|
15
|
+
import { detectHugo, loadHugoConfig, loadHugoI18n, contentDirFor } from "./config.js";
|
|
16
16
|
import { collectHugoPages, buildHugoNav, makeRefResolver, splitFrontmatter } from "./nav.js";
|
|
17
|
-
import {
|
|
17
|
+
import { makeContentLocator } from "./resolve.js";
|
|
18
|
+
import { liftTemplateBlocks, newIntentStore } from "./intent.js";
|
|
19
|
+
import { bakeChoiceValuesInHtml } from "./choice-html.js";
|
|
18
20
|
import { translateHugo } from "./translate.js";
|
|
21
|
+
import { staticAssets } from "./static.js";
|
|
22
|
+
import { parse as parseToml } from "smol-toml";
|
|
23
|
+
import { parse as parseYaml } from "yaml";
|
|
24
|
+
import { HUGO_ADAPTERS, chooseAdapters } from "./adapters/index.js";
|
|
25
|
+
/**
|
|
26
|
+
* What a site says about itself before its pages are read: its themes, and the names of the
|
|
27
|
+
* shortcodes it defines. Names only; the one file opened is `go.mod`, for the modules it
|
|
28
|
+
* requires. All of it is the site's, so: nothing outside the site's folder is listed (a
|
|
29
|
+
* `layouts/shortcodes` that links elsewhere is skipped), links inside are named and not
|
|
30
|
+
* followed, and counts and depth are bounded.
|
|
31
|
+
*/
|
|
32
|
+
export function siteFacts(source, config) {
|
|
33
|
+
const LIMIT = 5000;
|
|
34
|
+
let root;
|
|
35
|
+
try {
|
|
36
|
+
root = realpathSync(source);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return { themes: config.themes, modules: config.modules, shortcodes: new Set(), params: config.params };
|
|
40
|
+
}
|
|
41
|
+
/** The folder's entries, at most LIMIT of them, when it is inside the site after links are followed. */
|
|
42
|
+
const list = (dir) => {
|
|
43
|
+
const out = [];
|
|
44
|
+
try {
|
|
45
|
+
const real = realpathSync(dir);
|
|
46
|
+
if (real !== root && !real.startsWith(root + sep))
|
|
47
|
+
return out;
|
|
48
|
+
const handle = opendirSync(real);
|
|
49
|
+
try {
|
|
50
|
+
for (let entry = handle.readSync(); entry && out.length < LIMIT; entry = handle.readSync())
|
|
51
|
+
out.push(entry);
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
handle.closeSync();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// no such folder
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
};
|
|
62
|
+
// A language's own version of a shortcode is named for it (`intro.en.md`): only a language
|
|
63
|
+
// the site declares is taken off the name, or `setup.js.html` would lose its `.js`.
|
|
64
|
+
const languages = new Set(config.languages.map((l) => l.code.toLowerCase()));
|
|
65
|
+
const nameOf = (file) => {
|
|
66
|
+
if (file.startsWith("."))
|
|
67
|
+
return undefined;
|
|
68
|
+
let name = file.replace(/\.[A-Za-z0-9]+$/, "");
|
|
69
|
+
const language = /\.([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})?)$/.exec(name)?.[1];
|
|
70
|
+
if (language && languages.has(language.toLowerCase()))
|
|
71
|
+
name = name.slice(0, -language.length - 1);
|
|
72
|
+
return name || undefined;
|
|
73
|
+
};
|
|
74
|
+
const shortcodes = new Set();
|
|
75
|
+
const walk = (dir, prefix, depth) => {
|
|
76
|
+
if (depth > 6)
|
|
77
|
+
return;
|
|
78
|
+
for (const entry of list(dir)) {
|
|
79
|
+
if (shortcodes.size >= LIMIT)
|
|
80
|
+
return;
|
|
81
|
+
if (entry.isDirectory())
|
|
82
|
+
walk(join(dir, entry.name), `${prefix}${entry.name}/`, depth + 1);
|
|
83
|
+
else if (entry.isFile() || entry.isSymbolicLink()) {
|
|
84
|
+
const name = nameOf(entry.name);
|
|
85
|
+
if (name)
|
|
86
|
+
shortcodes.add(prefix + name);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
walk(join(root, "layouts", "shortcodes"), "", 0);
|
|
91
|
+
walk(join(root, "layouts", "_shortcodes"), "", 0);
|
|
92
|
+
// A theme may be a folder under `themes/` that the config does not name (it is given on the
|
|
93
|
+
// command line), or a module the site's go.mod requires.
|
|
94
|
+
const themes = new Set(config.themes);
|
|
95
|
+
for (const entry of list(join(root, "themes")))
|
|
96
|
+
if (entry.isDirectory() || entry.isSymbolicLink())
|
|
97
|
+
themes.add(entry.name);
|
|
98
|
+
const modules = new Set(config.modules);
|
|
99
|
+
try {
|
|
100
|
+
const goMod = join(root, "go.mod");
|
|
101
|
+
if (statSync(goMod).isFile() && statSync(goMod).size <= 256 * 1024 && realpathSync(goMod).startsWith(root + sep)) {
|
|
102
|
+
for (const m of readFileSync(goMod, "utf8").matchAll(/^\s*(?:require\s+)?([\w.-]+(?:\/[\w.~-]+)+)\s+v\S+/gm)) {
|
|
103
|
+
if (modules.size < 500)
|
|
104
|
+
modules.add(m[1]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// no go.mod
|
|
110
|
+
}
|
|
111
|
+
return { themes: [...themes].slice(0, 500), modules: [...modules], shortcodes, params: config.params };
|
|
112
|
+
}
|
|
19
113
|
export async function importHugo(source, opts) {
|
|
20
114
|
if (!existsSync(source)) {
|
|
21
115
|
throw new Error(`Source directory not found: ${source}`);
|
|
22
116
|
}
|
|
23
117
|
const config = loadHugoConfig(source);
|
|
118
|
+
// The theme's and the site's own shortcodes: the adapters the site matches, or the ones asked
|
|
119
|
+
// for. Chosen first, so a mistyped name fails before the site is read.
|
|
120
|
+
if (opts.kubernetes !== undefined) {
|
|
121
|
+
throw new Error("the `kubernetes` option (--no-kubernetes) is retired: use `adapters` (--adapters none, or a list without kubernetes)");
|
|
122
|
+
}
|
|
123
|
+
const wanted = opts.adapters;
|
|
124
|
+
if (wanted !== undefined && typeof wanted !== "string" && !(Array.isArray(wanted) && wanted.every((n) => typeof n === "string"))) {
|
|
125
|
+
throw new Error("`adapters` is auto, none, or adapter names (a comma-separated string or a list)");
|
|
126
|
+
}
|
|
127
|
+
const chosen = chooseAdapters(siteFacts(source, config), Array.isArray(wanted) ? (wanted.length ? wanted.join(",") : "none") : wanted ?? "auto");
|
|
128
|
+
const adapters = chosen.map((c) => c.adapter);
|
|
24
129
|
if (config.parseErrors.length > 0) {
|
|
25
130
|
// Not fatal, but never silent: params feed `{{< param >}}` and
|
|
26
131
|
// `{{< skew >}}`, so a config we fell back on turns those into nothing.
|
|
@@ -48,7 +153,198 @@ export async function importHugo(source, opts) {
|
|
|
48
153
|
const hugoPages = collectHugoPages(contentRoot, config.permalinks, config.sectionPermalinks);
|
|
49
154
|
const nav = buildHugoNav(hugoPages, hrefPrefix);
|
|
50
155
|
const resolveRef = makeRefResolver(hugoPages, hrefPrefix);
|
|
51
|
-
|
|
156
|
+
// Files a shortcode names are read from the LANGUAGE root, whatever section is imported;
|
|
157
|
+
// an example the language does not have, from the default language.
|
|
158
|
+
// The default language is the site's `defaultContentLanguage`, never a guess from the order.
|
|
159
|
+
const defaultLang = config.defaultLanguage ?? "en";
|
|
160
|
+
const defaultDeclared = config.languages.some((l) => l.code === defaultLang);
|
|
161
|
+
const fallbackRoot = defaultDeclared ? resolve(source, contentDirFor(config, defaultLang)) : undefined;
|
|
162
|
+
const locateContentFile = makeContentLocator({
|
|
163
|
+
contentRoot: langRoot,
|
|
164
|
+
pageRoot: contentRoot,
|
|
165
|
+
// a monolingual site has no other language to fall back to
|
|
166
|
+
...(fallbackRoot ? { fallbackRoot } : {}),
|
|
167
|
+
});
|
|
168
|
+
const readContentFile = (path, from) => locateContentFile(path, from)?.text;
|
|
169
|
+
// A folder of pages a shortcode reads as data, from the default language as Hugo reads it.
|
|
170
|
+
// The folder is named by our own mappings, but it is checked like any path: inside the content
|
|
171
|
+
// root after links are followed, files only, a bounded number of them and of bytes each.
|
|
172
|
+
const dirListings = new Map();
|
|
173
|
+
const listContentDir = (dir) => {
|
|
174
|
+
const cached = dirListings.get(dir);
|
|
175
|
+
if (cached)
|
|
176
|
+
return cached;
|
|
177
|
+
const out = [];
|
|
178
|
+
dirListings.set(dir, out);
|
|
179
|
+
try {
|
|
180
|
+
const root = realpathSync(fallbackRoot ?? langRoot);
|
|
181
|
+
const folder = realpathSync(resolve(root, dir.replace(/^\/+/, "")));
|
|
182
|
+
if (folder !== root && !folder.startsWith(root + sep))
|
|
183
|
+
return out;
|
|
184
|
+
// at most 5,000 names looked at, and 64 MB read in all
|
|
185
|
+
const names = [];
|
|
186
|
+
const handle = opendirSync(folder);
|
|
187
|
+
try {
|
|
188
|
+
for (let entry = handle.readSync(); entry && names.length < 5000; entry = handle.readSync())
|
|
189
|
+
names.push(entry.name);
|
|
190
|
+
}
|
|
191
|
+
finally {
|
|
192
|
+
handle.closeSync();
|
|
193
|
+
}
|
|
194
|
+
let total = 0;
|
|
195
|
+
for (const name of names.sort()) {
|
|
196
|
+
if (!name.endsWith(".md"))
|
|
197
|
+
continue;
|
|
198
|
+
const file = realpathSync(join(folder, name));
|
|
199
|
+
if (!file.startsWith(root + sep))
|
|
200
|
+
continue;
|
|
201
|
+
const stat = statSync(file);
|
|
202
|
+
if (!stat.isFile() || stat.size > 1024 * 1024)
|
|
203
|
+
continue;
|
|
204
|
+
total += stat.size;
|
|
205
|
+
if (total > 64 * 1024 * 1024)
|
|
206
|
+
break;
|
|
207
|
+
out.push({ name, text: readFileSync(file, "utf8") });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// no such folder: the mapping reports what it could not look up
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
};
|
|
215
|
+
// A file of the site's data folder, as Hugo's `.Site.Data` has it. Named by our own mappings,
|
|
216
|
+
// and checked like any path: a plain name, inside the data folder after links are followed,
|
|
217
|
+
// one file of at most 1 MB (what a Dogsbay data file may be in an untrusted build).
|
|
218
|
+
const siteData = new Map();
|
|
219
|
+
// Data the host gives by name (`--site-data regions=<file>`): a file the site MOUNTS as data
|
|
220
|
+
// from outside its folder (Datadog's `../shared/regions.yaml`), which the import does not follow
|
|
221
|
+
const given = (opts.siteData && typeof opts.siteData === "object" ? opts.siteData : {});
|
|
222
|
+
const readSiteData = (name) => {
|
|
223
|
+
if (Object.prototype.hasOwnProperty.call(given, name))
|
|
224
|
+
return given[name];
|
|
225
|
+
if (siteData.has(name))
|
|
226
|
+
return siteData.get(name);
|
|
227
|
+
let parsed;
|
|
228
|
+
try {
|
|
229
|
+
if (!/^[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)*$/.test(name))
|
|
230
|
+
throw new Error("not a data file name");
|
|
231
|
+
const site = realpathSync(source);
|
|
232
|
+
const root = realpathSync(resolve(source, "data"));
|
|
233
|
+
// (a data folder that is a link out of the site is not the site's data)
|
|
234
|
+
if (!root.startsWith(site + sep))
|
|
235
|
+
throw new Error("the data folder is outside the site");
|
|
236
|
+
for (const ext of [".yaml", ".yml", ".json", ".toml"]) {
|
|
237
|
+
const path = join(root, name + ext);
|
|
238
|
+
if (!existsSync(path))
|
|
239
|
+
continue;
|
|
240
|
+
const file = realpathSync(path);
|
|
241
|
+
const stat = statSync(file);
|
|
242
|
+
if (!file.startsWith(root + sep) || !stat.isFile() || stat.size > 1024 * 1024)
|
|
243
|
+
break;
|
|
244
|
+
const text = readFileSync(file, "utf8");
|
|
245
|
+
// merge keys read as the build reads them, so a value is the same before and after
|
|
246
|
+
parsed = ext === ".toml" ? parseToml(text) : ext === ".json" ? JSON.parse(text) : parseYaml(text, { merge: true });
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
parsed = undefined; // no such file, or one that cannot be read: the mapping says what it did instead
|
|
252
|
+
}
|
|
253
|
+
siteData.set(name, parsed);
|
|
254
|
+
return parsed;
|
|
255
|
+
};
|
|
256
|
+
// A shortcode the site defines as a Markdown file: its text. The name is the page's, so it is
|
|
257
|
+
// checked like any path: plain segments, inside the site's shortcode folders after links are
|
|
258
|
+
// followed, one file of at most 1 MB. The language imported, then no language, then the
|
|
259
|
+
// default language's.
|
|
260
|
+
const shortcodeFiles = new Map();
|
|
261
|
+
const shortcodeNames = new Map();
|
|
262
|
+
const shortcodeFile = (name) => {
|
|
263
|
+
if (shortcodeFiles.has(name))
|
|
264
|
+
return shortcodeFiles.get(name);
|
|
265
|
+
let found;
|
|
266
|
+
try {
|
|
267
|
+
if (!/^[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)*$/.test(name))
|
|
268
|
+
throw new Error("not a shortcode name");
|
|
269
|
+
const site = realpathSync(source);
|
|
270
|
+
// (a language code is the site's text: only a plain one goes into a file name)
|
|
271
|
+
const languages = [...new Set([lang ?? defaultLang, "", defaultLang])].filter((code) => /^[A-Za-z0-9-]*$/.test(code));
|
|
272
|
+
search: for (const folder of ["shortcodes", "_shortcodes"]) {
|
|
273
|
+
const dir = resolve(source, "layouts", folder);
|
|
274
|
+
if (!existsSync(dir))
|
|
275
|
+
continue;
|
|
276
|
+
const root = realpathSync(dir);
|
|
277
|
+
if (!root.startsWith(site + sep))
|
|
278
|
+
continue;
|
|
279
|
+
// Hugo's page is HTML: where the shortcode has an HTML template too, that is the one
|
|
280
|
+
// it uses, and the Markdown file is for another output (a plain-text copy of the site)
|
|
281
|
+
if (languages.some((language) => existsSync(join(root, `${name}${language ? `.${language}` : ""}.html`))))
|
|
282
|
+
break;
|
|
283
|
+
for (const language of languages) {
|
|
284
|
+
const path = join(root, `${name}${language ? `.${language}` : ""}.md`);
|
|
285
|
+
if (!existsSync(path))
|
|
286
|
+
continue;
|
|
287
|
+
const file = realpathSync(path);
|
|
288
|
+
const stat = statSync(file);
|
|
289
|
+
if (!file.startsWith(root + sep) || !stat.isFile() || stat.size > 1024 * 1024)
|
|
290
|
+
continue;
|
|
291
|
+
found = { path: file, text: readFileSync(file, "utf8"), fallback: false };
|
|
292
|
+
shortcodeNames.set(file, name);
|
|
293
|
+
break search;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
found = undefined; // no such file, or one that cannot be read: the call is kept as written
|
|
299
|
+
}
|
|
300
|
+
shortcodeFiles.set(name, found);
|
|
301
|
+
return found;
|
|
302
|
+
};
|
|
303
|
+
// Keeping the source's intent (a migration to a Dogsbay site): attributes, includes and
|
|
304
|
+
// expressions where Hugo had params, includes and version arithmetic. See intent.ts.
|
|
305
|
+
const store = opts.keepIntent === true ? newIntentStore() : undefined;
|
|
306
|
+
// Reader-choice values inside raw HTML: the default's, and said so (Markdown is not read there)
|
|
307
|
+
const htmlValues = (tree, file) => {
|
|
308
|
+
if (!store || store.choices.size === 0)
|
|
309
|
+
return;
|
|
310
|
+
const n = bakeChoiceValuesInHtml(tree, Object.fromEntries(store.choices));
|
|
311
|
+
// one entry per value, one wording, so the report counts them in one row
|
|
312
|
+
for (let k = 0; k < n; k++)
|
|
313
|
+
ledger.push({ shortcode: "(choice)", action: "degraded", note: "a reader-choice value inside raw HTML shows the default's value to every reader (Markdown is not read inside HTML)", line: 0, file });
|
|
314
|
+
};
|
|
315
|
+
const templatePages = [];
|
|
316
|
+
/** A written page's slug → its source file, as ledger entries name it. */
|
|
317
|
+
const sourceOf = new Map();
|
|
318
|
+
const posixPath = (p) => p.split(sep).join("/");
|
|
319
|
+
/** Where a shared file is written: `_includes/…` for a text, `_examples/…` for an example. */
|
|
320
|
+
const sharedPath = (located, kind) => {
|
|
321
|
+
if (kind === "shortcode") {
|
|
322
|
+
// only a file `shortcodeFile` found: its name was checked there
|
|
323
|
+
const name = shortcodeNames.get(located.path);
|
|
324
|
+
if (!name)
|
|
325
|
+
return undefined;
|
|
326
|
+
const out = `_includes/shortcodes/${name}.md`;
|
|
327
|
+
const taken = store?.shared.get(out);
|
|
328
|
+
return taken && taken.source !== located.path ? undefined : out;
|
|
329
|
+
}
|
|
330
|
+
const root = located.fallback && fallbackRoot ? fallbackRoot : langRoot;
|
|
331
|
+
let rel = posixPath(relative(root, located.path));
|
|
332
|
+
if (rel.startsWith("..") || rel.startsWith("/"))
|
|
333
|
+
return undefined;
|
|
334
|
+
if (kind === "example") {
|
|
335
|
+
if (!rel.startsWith("examples/"))
|
|
336
|
+
return undefined;
|
|
337
|
+
rel = rel.slice("examples/".length);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
rel = rel.replace(/^includes\//, "");
|
|
341
|
+
}
|
|
342
|
+
const out = `${kind === "example" ? "_examples" : "_includes"}/${rel}`;
|
|
343
|
+
// two sources must not be written to one path
|
|
344
|
+
const taken = store?.shared.get(out);
|
|
345
|
+
return taken && taken.source !== located.path ? undefined : out;
|
|
346
|
+
};
|
|
347
|
+
const i18n = loadHugoI18n(source, lang ?? defaultLang, defaultLang);
|
|
52
348
|
// PASS 2 — translate and parse.
|
|
53
349
|
const ledger = [];
|
|
54
350
|
const pages = [];
|
|
@@ -77,23 +373,60 @@ export async function importHugo(source, opts) {
|
|
|
77
373
|
frontmatter: page.frontmatter,
|
|
78
374
|
});
|
|
79
375
|
seen.add(page.slug);
|
|
376
|
+
sourceOf.set(page.slug, page.rel);
|
|
80
377
|
continue;
|
|
81
378
|
}
|
|
82
379
|
const source_ = readFileSync(page.file, "utf-8");
|
|
83
|
-
const { body } = splitFrontmatter(source_);
|
|
84
|
-
const
|
|
380
|
+
const { body, frontmatter: pageParams } = splitFrontmatter(source_);
|
|
381
|
+
const NOT_KEPT = "a block computed from data could not be kept through the page's Markdown: the page is written as it is today";
|
|
382
|
+
const translatePage = (allowTemplate) => translateHugo(body, {
|
|
85
383
|
config,
|
|
86
384
|
file: page.rel,
|
|
87
385
|
resolveRef,
|
|
88
386
|
readContentFile,
|
|
89
|
-
|
|
387
|
+
listContentDir,
|
|
388
|
+
readSiteData,
|
|
389
|
+
shortcodeFile,
|
|
390
|
+
pageParams,
|
|
391
|
+
i18n,
|
|
392
|
+
adapters,
|
|
393
|
+
...(store
|
|
394
|
+
? {
|
|
395
|
+
intent: { store, pagePath: `${page.slug || "index"}.md`, allowTemplate, ...(allowTemplate ? {} : { whyNotTemplate: NOT_KEPT }) },
|
|
396
|
+
locateContentFile,
|
|
397
|
+
sharedPath,
|
|
398
|
+
}
|
|
399
|
+
: {}),
|
|
90
400
|
});
|
|
91
|
-
ledger
|
|
401
|
+
let { markdown, ledger: entries, template, blocks } = translatePage(true);
|
|
92
402
|
let tree;
|
|
93
403
|
try {
|
|
94
|
-
(
|
|
404
|
+
if (blocks && store) {
|
|
405
|
+
// A loop over a data file is template text, not Markdown: it is read as a word on a
|
|
406
|
+
// line of its own, and put back where the word's paragraph is.
|
|
407
|
+
const lifted = liftTemplateBlocks(markdown, blocks, store.key);
|
|
408
|
+
({ tree } = dogsbayMdToTree(lifted.markdown));
|
|
409
|
+
if (restoreTemplateBlocks(tree, lifted.restore) !== blocks.length) {
|
|
410
|
+
// a block that did not come back as a paragraph of its own: the page as it is today
|
|
411
|
+
// (what the first reading reported and the second does not: a gate's description is
|
|
412
|
+
// translated once an import, so its notes were made the first time)
|
|
413
|
+
const first = entries;
|
|
414
|
+
({ markdown, ledger: entries, template, blocks } = translatePage(false));
|
|
415
|
+
const said = new Set(entries.map((e) => JSON.stringify(e)));
|
|
416
|
+
entries.push(...first.filter((e) => e.shortcode !== "(intent)" && !said.has(JSON.stringify(e))));
|
|
417
|
+
if (!entries.some((e) => e.note === NOT_KEPT))
|
|
418
|
+
entries.push({ shortcode: "(intent)", action: "degraded", note: NOT_KEPT, line: 0, file: page.rel });
|
|
419
|
+
({ tree } = dogsbayMdToTree(markdown));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
({ tree } = dogsbayMdToTree(markdown));
|
|
424
|
+
}
|
|
425
|
+
ledger.push(...entries);
|
|
426
|
+
htmlValues(tree, page.rel);
|
|
95
427
|
}
|
|
96
428
|
catch (err) {
|
|
429
|
+
ledger.push(...entries);
|
|
97
430
|
// A page that will not parse must be REPORTED, not dropped: it is the
|
|
98
431
|
// one failure mode a page count cannot show.
|
|
99
432
|
ledger.push({
|
|
@@ -122,18 +455,119 @@ export async function importHugo(source, opts) {
|
|
|
122
455
|
slug = `${slug}-${n}`;
|
|
123
456
|
}
|
|
124
457
|
seen.add(slug);
|
|
458
|
+
sourceOf.set(slug, page.rel);
|
|
459
|
+
// (an `includes/` page is not written as a page, so it is not counted as one)
|
|
460
|
+
if (template && !page.fragment)
|
|
461
|
+
templatePages.push(slug);
|
|
125
462
|
pages.push({
|
|
126
463
|
slug,
|
|
127
464
|
title: page.title,
|
|
128
465
|
tree,
|
|
129
466
|
headings: extractHeadings(tree),
|
|
130
467
|
frontmatter: page.frontmatter,
|
|
468
|
+
...(template ? { template: true } : {}),
|
|
131
469
|
// Include fragments are emitted so their text is not lost, but marked so
|
|
132
470
|
// the CLI writes them as include targets rather than routable pages.
|
|
133
471
|
...(page.fragment ? { fragment: true } : {}),
|
|
134
472
|
});
|
|
135
473
|
}
|
|
474
|
+
// The shared texts the pages include, each translated once (one may include another).
|
|
475
|
+
const fragments = [];
|
|
476
|
+
const files = [];
|
|
477
|
+
if (store) {
|
|
478
|
+
const done = new Set();
|
|
479
|
+
for (;;) {
|
|
480
|
+
const next = [...store.shared].find(([path]) => !done.has(path));
|
|
481
|
+
if (!next)
|
|
482
|
+
break;
|
|
483
|
+
const [path, { source: file, kind }] = next;
|
|
484
|
+
done.add(path);
|
|
485
|
+
if (kind === "example") {
|
|
486
|
+
files.push({ path, source: file });
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
// (a shortcode's own file is outside the content folder: named from the site's root)
|
|
490
|
+
const inContent = posixPath(relative(contentRoot, file));
|
|
491
|
+
// (any other file keeps the name page-relative lookups made from it are resolved with)
|
|
492
|
+
const rel = kind === "shortcode" && inContent.startsWith("..") ? posixPath(relative(source, file)) : inContent;
|
|
493
|
+
// Pages already include this file: one that cannot be written must stop the migration,
|
|
494
|
+
// not leave them including nothing.
|
|
495
|
+
let text;
|
|
496
|
+
try {
|
|
497
|
+
text = readFileSync(file, "utf-8");
|
|
498
|
+
}
|
|
499
|
+
catch (err) {
|
|
500
|
+
throw new Error(`shared file ${rel} (included by pages) could not be read: ${err instanceof Error ? err.message : String(err)}`);
|
|
501
|
+
}
|
|
502
|
+
// a content file's front matter is not part of what it says; a shortcode's file has none
|
|
503
|
+
// (the text shared is the text that was checked)
|
|
504
|
+
const body = kind === "shortcode" ? text.replace(/^\uFEFF/, "") : splitFrontmatter(text).body;
|
|
505
|
+
const { markdown, ledger: entries } = translateHugo(body, {
|
|
506
|
+
config,
|
|
507
|
+
file: rel,
|
|
508
|
+
resolveRef,
|
|
509
|
+
readContentFile,
|
|
510
|
+
i18n,
|
|
511
|
+
adapters,
|
|
512
|
+
// a shared text is included into pages that are not all template pages
|
|
513
|
+
intent: { store, pagePath: path, allowTemplate: false },
|
|
514
|
+
locateContentFile,
|
|
515
|
+
sharedPath,
|
|
516
|
+
listContentDir,
|
|
517
|
+
readSiteData,
|
|
518
|
+
shortcodeFile,
|
|
519
|
+
});
|
|
520
|
+
ledger.push(...entries);
|
|
521
|
+
try {
|
|
522
|
+
const fragmentTree = dogsbayMdToTree(markdown).tree;
|
|
523
|
+
htmlValues(fragmentTree, rel);
|
|
524
|
+
fragments.push({ path, tree: fragmentTree });
|
|
525
|
+
}
|
|
526
|
+
catch (err) {
|
|
527
|
+
throw new Error(`shared file ${rel} (included by pages) could not be parsed: ${err instanceof Error ? err.message : String(err)}`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
// What the pages show from the site's static folder moves to `_assets/`, and the pages with
|
|
532
|
+
// it. Only for a caller that copies the files (`staticAssets: true`): otherwise the pages
|
|
533
|
+
// keep the paths the site wrote.
|
|
534
|
+
let staticFiles = [];
|
|
535
|
+
if (opts.staticAssets === true) {
|
|
536
|
+
let basePath = "";
|
|
537
|
+
try {
|
|
538
|
+
basePath = config.baseURL ? new URL(config.baseURL).pathname.replace(/\/+$/, "") : "";
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
basePath = ""; // not an address: the site is served at the root
|
|
542
|
+
}
|
|
543
|
+
const assets = staticAssets(source, resolve(source, "static"), { contentRoots: [contentRoot], basePath });
|
|
544
|
+
const missing = (file, paths) => {
|
|
545
|
+
for (const path of paths) {
|
|
546
|
+
ledger.push({
|
|
547
|
+
shortcode: "(asset)",
|
|
548
|
+
action: "degraded",
|
|
549
|
+
note: `${path.slice(0, 300)}: a picture or video the page shows could not be taken from the site's static folder (it is not there, or a content file already has its place under _assets/): the page keeps the path the site wrote`,
|
|
550
|
+
line: 0,
|
|
551
|
+
file,
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
for (const page of pages)
|
|
556
|
+
missing(sourceOf.get(page.slug) ?? page.slug, assets.rewrite(page.tree));
|
|
557
|
+
for (const fragment of fragments)
|
|
558
|
+
missing(fragment.path, assets.rewrite(fragment.tree));
|
|
559
|
+
staticFiles = assets.files();
|
|
560
|
+
if (assets.full())
|
|
561
|
+
ledger.push({ shortcode: "(asset)", action: "degraded", note: "the pages refer to more than 100,000 static files: the rest are not copied, and keep the paths the site wrote", line: 0 });
|
|
562
|
+
}
|
|
136
563
|
return {
|
|
564
|
+
staticFiles,
|
|
565
|
+
...(store
|
|
566
|
+
? { intent: { attributes: Object.fromEntries(store.attributes), templatePages, fragments, files, // (a data file is for template pages to read: with none, nothing reads it)
|
|
567
|
+
data: templatePages.length > 0 ? Object.fromEntries(store.data) : {},
|
|
568
|
+
choices: Object.fromEntries(store.choices) } }
|
|
569
|
+
: {}),
|
|
570
|
+
adapters: chosen.map((c) => ({ name: c.adapter.name, description: c.adapter.description, why: c.why })),
|
|
137
571
|
pages,
|
|
138
572
|
nav,
|
|
139
573
|
ledger,
|
|
@@ -176,7 +610,7 @@ export const plugin = {
|
|
|
176
610
|
{ flags: "--lang <code>", description: "language directory to import (multilingual sites)" },
|
|
177
611
|
{ flags: "--section <path>", description: "import only this subtree, e.g. docs" },
|
|
178
612
|
{ flags: "--include-drafts", description: "import pages marked draft/headless" },
|
|
179
|
-
{ flags: "--
|
|
613
|
+
{ flags: "--adapters <names>", description: `theme and site adapters: auto (default: the ones the site matches), none, or a list of ${HUGO_ADAPTERS.map((a) => a.name).join(", ")}` },
|
|
180
614
|
],
|
|
181
615
|
async import(source, opts) {
|
|
182
616
|
const result = await importHugo(source, opts);
|
|
@@ -187,3 +621,24 @@ export const plugin = {
|
|
|
187
621
|
};
|
|
188
622
|
},
|
|
189
623
|
};
|
|
624
|
+
/**
|
|
625
|
+
* Put each lifted block of template text back: the paragraph that holds only the block's word
|
|
626
|
+
* becomes the block, written out exactly. Returns how many were put back.
|
|
627
|
+
*/
|
|
628
|
+
function restoreTemplateBlocks(nodes, restore) {
|
|
629
|
+
let count = 0;
|
|
630
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
631
|
+
const node = nodes[i];
|
|
632
|
+
const only = node.type === "paragraph" && node.inline?.length === 1 && node.inline[0].type === "text"
|
|
633
|
+
? restore.get(String(node.inline[0].text ?? "").trim())
|
|
634
|
+
: undefined;
|
|
635
|
+
if (only !== undefined) {
|
|
636
|
+
nodes[i] = { type: "directive-raw", props: { source: only } };
|
|
637
|
+
count++;
|
|
638
|
+
}
|
|
639
|
+
else if (node.children) {
|
|
640
|
+
count += restoreTemplateBlocks(node.children, restore);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
return count;
|
|
644
|
+
}
|
package/dist/config.d.ts
CHANGED
|
@@ -14,8 +14,12 @@ export interface HugoConfig {
|
|
|
14
14
|
params: Record<string, unknown>;
|
|
15
15
|
/** Declared languages, empty when the site is monolingual. */
|
|
16
16
|
languages: HugoLanguage[];
|
|
17
|
+
/** `defaultContentLanguage` (Hugo's default: `en`): what a translation falls back to. */
|
|
18
|
+
defaultLanguage?: string;
|
|
17
19
|
/** Theme names, in order. `docsy` here is what selects the Docsy mappings. */
|
|
18
20
|
themes: string[];
|
|
21
|
+
/** Hugo module imports (`module.imports[].path`): a theme used as a module is named here. */
|
|
22
|
+
modules: string[];
|
|
19
23
|
/**
|
|
20
24
|
* `[permalinks]` — per-section URL patterns.
|
|
21
25
|
*
|
|
@@ -69,3 +73,15 @@ export declare function lookupParam(params: Record<string, unknown>, path: strin
|
|
|
69
73
|
* import run: mixing them produces a page set with colliding slugs.
|
|
70
74
|
*/
|
|
71
75
|
export declare function contentDirFor(config: HugoConfig, lang?: string): string;
|
|
76
|
+
/**
|
|
77
|
+
* The site's translated strings for a language: `i18n/<lang>.toml|yaml|json`,
|
|
78
|
+
* or every such file in `i18n/<lang>/` (kubernetes keeps `i18n/en/en.toml`).
|
|
79
|
+
* Keys the language lacks come from `fallbackLang`, as Hugo falls back to the
|
|
80
|
+
* default language. A value is `other = "…"` (the plural form Hugo uses when no
|
|
81
|
+
* count is given), a bare string, or a list entry `{ id, translation }`.
|
|
82
|
+
* Strings a THEME ships (`themes/<name>/i18n`) are not read.
|
|
83
|
+
*
|
|
84
|
+
* `{{< heading "whatsnext" >}}` prints the string `whatsnext_heading`; without
|
|
85
|
+
* this file the import wrote the key itself, title-cased ("Whatsnext").
|
|
86
|
+
*/
|
|
87
|
+
export declare function loadHugoI18n(dir: string, lang: string, fallbackLang?: string): Record<string, string>;
|