@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/config.js CHANGED
@@ -93,13 +93,36 @@ export function loadHugoConfig(dir) {
93
93
  }
94
94
  const params = (raw.params ?? {});
95
95
  const contentDir = typeof raw.contentDir === "string" ? raw.contentDir : "content";
96
+ // A site may mount a folder as a language's content instead of naming a `contentDir`
97
+ // (`module.mounts`: `source: content/en`, `target: content`, `lang: en`; Datadog's docs do).
98
+ // Only a mount of the WHOLE content tree is read, and only a plain path inside the site: the
99
+ // first for each language, as Hugo layers them.
100
+ const mounted = new Map();
101
+ const mountsRaw = raw.module?.mounts;
102
+ for (const entry of (Array.isArray(mountsRaw) ? mountsRaw : []).slice(0, 1000)) {
103
+ if (!entry || typeof entry !== "object")
104
+ continue;
105
+ const { source, target, lang } = entry;
106
+ if (typeof source !== "string" || typeof target !== "string" || typeof lang !== "string")
107
+ continue;
108
+ if (target.replace(/\/+$/, "") !== "content")
109
+ continue;
110
+ const clean = source.replace(/\/+$/, "");
111
+ if (!/^[A-Za-z0-9_-][A-Za-z0-9_.-]*(\/[A-Za-z0-9_-][A-Za-z0-9_.-]*)*$/.test(clean))
112
+ continue;
113
+ if (!mounted.has(lang))
114
+ mounted.set(lang, clean);
115
+ }
116
+ // (with no `contentDir` of its own, the site's content is its default language's mount)
117
+ const defaultMount = typeof raw.contentDir === "string" ? undefined
118
+ : mounted.get(typeof raw.defaultContentLanguage === "string" ? raw.defaultContentLanguage : "en");
96
119
  const languages = [];
97
120
  const langBlock = raw.languages;
98
121
  if (langBlock && typeof langBlock === "object") {
99
122
  for (const [code, cfg] of Object.entries(langBlock)) {
100
123
  languages.push({
101
124
  code,
102
- contentDir: typeof cfg?.contentDir === "string" ? cfg.contentDir : contentDir,
125
+ contentDir: typeof cfg?.contentDir === "string" ? cfg.contentDir : mounted.get(code) ?? contentDir,
103
126
  title: typeof cfg?.title === "string" ? cfg.title : undefined,
104
127
  weight: typeof cfg?.weight === "number" ? cfg.weight : undefined,
105
128
  });
@@ -129,13 +152,21 @@ export function loadHugoConfig(dir) {
129
152
  : typeof themeRaw === "string"
130
153
  ? [themeRaw]
131
154
  : [];
155
+ const importsRaw = raw.module?.imports;
156
+ const modules = (Array.isArray(importsRaw) ? importsRaw : [])
157
+ // an import switched off (`disable = true`) is not used
158
+ .map((entry) => (entry && typeof entry === "object" && entry.disable !== true ? entry.path : undefined))
159
+ .filter((path) => typeof path === "string")
160
+ .slice(0, 100);
132
161
  return {
133
162
  title: typeof raw.title === "string" ? raw.title : undefined,
134
163
  baseURL: typeof raw.baseURL === "string" ? raw.baseURL : undefined,
135
- contentDir,
164
+ contentDir: defaultMount ?? contentDir,
136
165
  params,
137
166
  languages,
167
+ defaultLanguage: typeof raw.defaultContentLanguage === "string" ? raw.defaultContentLanguage : "en",
138
168
  themes,
169
+ modules,
139
170
  permalinks,
140
171
  sectionPermalinks,
141
172
  sources,
@@ -188,3 +219,91 @@ export function contentDirFor(config, lang) {
188
219
  }
189
220
  return `${config.contentDir}/${lang}`;
190
221
  }
222
+ const I18N_LANG = /^[A-Za-z0-9_-]+$/;
223
+ /**
224
+ * The site's translated strings for a language: `i18n/<lang>.toml|yaml|json`,
225
+ * or every such file in `i18n/<lang>/` (kubernetes keeps `i18n/en/en.toml`).
226
+ * Keys the language lacks come from `fallbackLang`, as Hugo falls back to the
227
+ * default language. A value is `other = "…"` (the plural form Hugo uses when no
228
+ * count is given), a bare string, or a list entry `{ id, translation }`.
229
+ * Strings a THEME ships (`themes/<name>/i18n`) are not read.
230
+ *
231
+ * `{{< heading "whatsnext" >}}` prints the string `whatsnext_heading`; without
232
+ * this file the import wrote the key itself, title-cased ("Whatsnext").
233
+ */
234
+ export function loadHugoI18n(dir, lang, fallbackLang) {
235
+ const out = Object.create(null);
236
+ // `translation` may itself be a plural map: its `other` form is the one with no count
237
+ const plural = (v) => v && typeof v === "object" ? v.other : v;
238
+ const take = (key, value) => {
239
+ if (key in out)
240
+ return;
241
+ const text = typeof value === "string"
242
+ ? value
243
+ : value && typeof value === "object"
244
+ ? plural(value.other ?? value.translation)
245
+ : undefined;
246
+ if (typeof text === "string")
247
+ out[key] = text;
248
+ };
249
+ const readFile = (path) => {
250
+ let parsed;
251
+ try {
252
+ const text = readFileSync(path, "utf-8");
253
+ const ext = extname(path).toLowerCase();
254
+ parsed = ext === ".toml" ? parseToml(text) : ext === ".json" ? JSON.parse(text) : parseYaml(text);
255
+ }
256
+ catch {
257
+ return; // strings we cannot read are keys we do not have
258
+ }
259
+ if (Array.isArray(parsed)) {
260
+ for (const entry of parsed) {
261
+ if (entry && typeof entry === "object" && typeof entry.id === "string") {
262
+ take(entry.id, entry);
263
+ }
264
+ }
265
+ }
266
+ else if (parsed && typeof parsed === "object") {
267
+ for (const [key, value] of Object.entries(parsed))
268
+ take(key, value);
269
+ }
270
+ };
271
+ const base = join(dir, "i18n");
272
+ let entries = [];
273
+ try {
274
+ entries = readdirSync(base).sort();
275
+ }
276
+ catch {
277
+ // no i18n folder: no strings
278
+ }
279
+ // Hugo matches the language code whatever its case (`zh-CN` reads `zh-cn.toml`).
280
+ const readLang = (code) => {
281
+ if (!code || !I18N_LANG.test(code))
282
+ return;
283
+ const want = code.toLowerCase();
284
+ for (const name of entries) {
285
+ const lower = name.toLowerCase();
286
+ const path = join(base, name);
287
+ let isDir = false;
288
+ try {
289
+ isDir = statSync(path).isDirectory();
290
+ }
291
+ catch {
292
+ continue;
293
+ }
294
+ if (isDir && lower === want) {
295
+ for (const inner of readdirSync(path).sort()) {
296
+ if (CONFIG_EXTS.includes(extname(inner).toLowerCase()))
297
+ readFile(join(path, inner));
298
+ }
299
+ }
300
+ else if (!isDir && CONFIG_EXTS.includes(extname(lower)) && lower.slice(0, -extname(lower).length) === want) {
301
+ readFile(path);
302
+ }
303
+ }
304
+ };
305
+ readLang(lang);
306
+ if (fallbackLang !== lang)
307
+ readLang(fallbackLang);
308
+ return out;
309
+ }
@@ -0,0 +1,42 @@
1
+ import type { HugoLedgerEntry } from "./registry.js";
2
+ /** One kind of HTML block an adapter turns into a directive. */
3
+ export interface HtmlBlockMapping {
4
+ /** What the ledger calls it ("alert div"). */
5
+ name: string;
6
+ /** The element, lower case ("div"). */
7
+ tag: string;
8
+ /** The directive for an element with these classes, or undefined when it is not this mapping's. */
9
+ directive: (classes: readonly string[]) => string | undefined;
10
+ }
11
+ type Report = (entry: Omit<HugoLedgerEntry, "file">) => void;
12
+ export declare function rewriteHtmlBlocks(markdown: string, mappings: readonly HtmlBlockMapping[], report: Report,
13
+ /** The page as written, to give a box's ledger entry its source line. */
14
+ source: string): string;
15
+ interface Tag {
16
+ name: string;
17
+ closing: boolean;
18
+ selfClosing: boolean;
19
+ attrs: Record<string, string>;
20
+ /** Index after the `>`. */
21
+ end: number;
22
+ }
23
+ /** An HTML tag at `at` (`<` there), read by hand: one pass, no backtracking, at most MAX_TAG_CHARS. */
24
+ export declare function readTag(text: string, at: number): Tag | undefined;
25
+ /**
26
+ * A line of literal text that would start a Markdown block (a heading, a list item, a quote, a
27
+ * table row, a thematic break or setext underline, a fence, a directive, an HTML block, math)
28
+ * gets a backslash before it.
29
+ */
30
+ export declare function escapeLineStart(line: string): string;
31
+ /**
32
+ * A box's inner HTML as Markdown, or why not. Inline tags only: `code`, `a` (a safe `href`),
33
+ * `strong`/`b`, `em`/`i`, `kbd`, a plain `span`, `br` and `p` (paragraph breaks), and flat
34
+ * `ul`/`ol` lists of inline items. Up to the first blank line the text is HTML: each line's
35
+ * indent is dropped and a line that would start a Markdown block is escaped. After it, Hugo reads
36
+ * Markdown: the text keeps its lines, out-dented by the indent they share.
37
+ */
38
+ export declare function convertInlineHtml(html: string): string | {
39
+ ok: false;
40
+ why: string;
41
+ };
42
+ export {};