@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,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names Hugo sites keep reinventing.
|
|
3
|
+
*
|
|
4
|
+
* Hugo has no callout, tab or include shortcode of its own, so nearly every docs site defines
|
|
5
|
+
* `note`, `warning`, `tabs`, `include` in its own `layouts/shortcodes/`, with nearly the same
|
|
6
|
+
* meaning each time. These mappings are that usual meaning. They are the LOWEST adapter: a
|
|
7
|
+
* theme's or a site's adapter that maps the same name wins.
|
|
8
|
+
*
|
|
9
|
+
* The counts in the comments are occurrences in `kubernetes/website/content/en`, which defines
|
|
10
|
+
* every one of these itself.
|
|
11
|
+
*/
|
|
12
|
+
import { type HugoSiteAdapter, type ShortcodeMapping } from "../registry.js";
|
|
13
|
+
/** One tab of a group: `name=`, `header=` or `title=` names it (Docsy's `tab` is the same shape). */
|
|
14
|
+
export declare const tabMapping: ShortcodeMapping;
|
|
15
|
+
export declare const CONVENTIONS: HugoSiteAdapter;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The names Hugo sites keep reinventing.
|
|
3
|
+
*
|
|
4
|
+
* Hugo has no callout, tab or include shortcode of its own, so nearly every docs site defines
|
|
5
|
+
* `note`, `warning`, `tabs`, `include` in its own `layouts/shortcodes/`, with nearly the same
|
|
6
|
+
* meaning each time. These mappings are that usual meaning. They are the LOWEST adapter: a
|
|
7
|
+
* theme's or a site's adapter that maps the same name wins.
|
|
8
|
+
*
|
|
9
|
+
* The counts in the comments are occurrences in `kubernetes/website/content/en`, which defines
|
|
10
|
+
* every one of these itself.
|
|
11
|
+
*/
|
|
12
|
+
import { arg, attrBlock, directive } from "../registry.js";
|
|
13
|
+
import { looksLikeFile } from "../resolve.js";
|
|
14
|
+
import { splitFrontmatter } from "../nav.js";
|
|
15
|
+
import { bakeIntent, includeRef, showsTemplateText } from "../intent.js";
|
|
16
|
+
import { dependsOnPage, linkReferences } from "../references.js";
|
|
17
|
+
/**
|
|
18
|
+
* Keeping intent, a content file a page pulls in: written once as a shared file, and included.
|
|
19
|
+
* Undefined when intent is not kept or the file cannot be shared (the caller copies its text).
|
|
20
|
+
*/
|
|
21
|
+
function keptInclude(target, ctx) {
|
|
22
|
+
if (!ctx.intent || !ctx.locateContentFile || !ctx.sharedPath)
|
|
23
|
+
return undefined;
|
|
24
|
+
const located = ctx.locateContentFile(target, ctx.file);
|
|
25
|
+
const out = located && ctx.sharedPath(located, "include");
|
|
26
|
+
if (!located || !out)
|
|
27
|
+
return undefined;
|
|
28
|
+
const text = bakeIntent(ctx.translate(splitFrontmatter(located.text).body), ctx.intent.store);
|
|
29
|
+
// A shared text that shows template syntax (Go or Helm in its prose, a shortcode kept
|
|
30
|
+
// as written) is copied in: included on a template page, that text would run. Copied
|
|
31
|
+
// in, it is part of the page, which then is not made a template page.
|
|
32
|
+
return showsTemplateText(text) ? text : includeRef(ctx.intent, out, located.path, "include", text);
|
|
33
|
+
}
|
|
34
|
+
const SHORTCODES = {
|
|
35
|
+
// ── Callouts ────────────────────────────────────────────
|
|
36
|
+
//
|
|
37
|
+
// Hugo sites all grow the same three, and Kubernetes uses them 1,071
|
|
38
|
+
// times between them. The body may be markdown (`{{% note %}}`) or raw
|
|
39
|
+
// (`{{< note >}}`); either way it is content and gets translated.
|
|
40
|
+
note: (node, ctx) => directive("note", "", ctx.translate(node.inner)),
|
|
41
|
+
caution: (node, ctx) => directive("caution", "", ctx.translate(node.inner)),
|
|
42
|
+
warning: (node, ctx) => directive("warning", "", ctx.translate(node.inner)),
|
|
43
|
+
// ── Tabs ────────────────────────────────────────────────
|
|
44
|
+
//
|
|
45
|
+
// `{{< tabs name="x" >}}` wrapping `{{% tab name="Linux" %}}`. 117 groups,
|
|
46
|
+
// 287 tabs. Note the delimiter mix: the wrapper is angle, the tabs are
|
|
47
|
+
// percent, which is exactly why the scanner has to handle both.
|
|
48
|
+
tabs: (node, ctx) => directive("tabs", "", ctx.translate(node.inner)),
|
|
49
|
+
tab: (node, ctx) => {
|
|
50
|
+
const title = arg(node.args, "name", "header", "title") ?? "";
|
|
51
|
+
// `{{< tab name="x" include="file.md" />}}` — the kubeadm reference pages
|
|
52
|
+
// use this 118 times to pull in generated content.
|
|
53
|
+
// `include=` is not always a path: `{{< feature-gate-table include="alpha,beta" >}}`
|
|
54
|
+
// uses the same attribute name as a FILTER, and treating that as a file
|
|
55
|
+
// produced a guaranteed-failing read plus a misleading ledger entry.
|
|
56
|
+
const include = node.args.named.include;
|
|
57
|
+
if (looksLikeFile(include)) {
|
|
58
|
+
// Keeping intent: the tab's text stays the one file (kubeadm's reference pages are
|
|
59
|
+
// generated files, and can go on being generated)
|
|
60
|
+
const kept = keptInclude(include, ctx);
|
|
61
|
+
if (kept !== undefined)
|
|
62
|
+
return directive("tab", attrBlock({ title }), kept);
|
|
63
|
+
const content = ctx.readContentFile?.(include, ctx.file);
|
|
64
|
+
if (content !== undefined) {
|
|
65
|
+
return directive("tab", attrBlock({ title }), ctx.translate(splitFrontmatter(content).body));
|
|
66
|
+
}
|
|
67
|
+
ctx.report({
|
|
68
|
+
shortcode: "tab",
|
|
69
|
+
action: "degraded",
|
|
70
|
+
note: `could not read included file "${include}" — tab is empty`,
|
|
71
|
+
line: node.line,
|
|
72
|
+
});
|
|
73
|
+
return directive("tab", attrBlock({ title }), "");
|
|
74
|
+
}
|
|
75
|
+
return directive("tab", attrBlock({ title }), ctx.translate(node.inner));
|
|
76
|
+
},
|
|
77
|
+
// ── Other block constructs ──────────────────────────────
|
|
78
|
+
//
|
|
79
|
+
// 53 uses. A mermaid diagram is a code block with a `mermaid` language
|
|
80
|
+
// everywhere outside Hugo.
|
|
81
|
+
mermaid: (node) => {
|
|
82
|
+
const body = node.inner.replace(/^\n+|\n+$/g, "");
|
|
83
|
+
return `\`\`\`mermaid\n${body}\n\`\`\``;
|
|
84
|
+
},
|
|
85
|
+
// 59 uses. `{{< table caption="…" >}}` wraps a markdown table to caption it.
|
|
86
|
+
table: (node, ctx) => {
|
|
87
|
+
const caption = node.args.named.caption;
|
|
88
|
+
const body = ctx.translate(node.inner).replace(/^\n+|\n+$/g, "");
|
|
89
|
+
// Dogsbay-MD captions a table with a trailing `{caption}` line.
|
|
90
|
+
return caption ? `${body}\n{${caption}}` : body;
|
|
91
|
+
},
|
|
92
|
+
// `{{< include "file.md" >}}` — 171 uses. Pulls in another content file.
|
|
93
|
+
include: (node, ctx) => {
|
|
94
|
+
const target = arg(node.args, "file", "path") ?? "";
|
|
95
|
+
const kept = keptInclude(target, ctx);
|
|
96
|
+
if (kept !== undefined)
|
|
97
|
+
return kept;
|
|
98
|
+
const content = ctx.readContentFile?.(target, ctx.file);
|
|
99
|
+
if (content === undefined) {
|
|
100
|
+
ctx.report({
|
|
101
|
+
shortcode: "include",
|
|
102
|
+
action: "degraded",
|
|
103
|
+
note: `could not read included file "${target}"`,
|
|
104
|
+
line: node.line,
|
|
105
|
+
});
|
|
106
|
+
return "";
|
|
107
|
+
}
|
|
108
|
+
// the file's own front matter is not part of what it says
|
|
109
|
+
return ctx.translate(splitFrontmatter(content).body);
|
|
110
|
+
},
|
|
111
|
+
// 12 uses. An author note that never renders — dropping it is correct, and
|
|
112
|
+
// recording the drop is what makes it not-silent.
|
|
113
|
+
comment: (node, ctx) => {
|
|
114
|
+
ctx.report({
|
|
115
|
+
shortcode: "comment",
|
|
116
|
+
action: "stripped",
|
|
117
|
+
note: "author comment — not rendered by Hugo either",
|
|
118
|
+
line: node.line,
|
|
119
|
+
});
|
|
120
|
+
return null;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
/** What a shortcode's Markdown file is, worked out once per file (a file is used on many pages). */
|
|
124
|
+
const markdownFiles = new WeakMap();
|
|
125
|
+
/** A shortcode's Markdown file as the rule reads it; null when it holds template code. */
|
|
126
|
+
function markdownFile(located) {
|
|
127
|
+
let file = markdownFiles.get(located);
|
|
128
|
+
if (file === undefined) {
|
|
129
|
+
const text = located.text.replace(/^\uFEFF/, "");
|
|
130
|
+
file = text.includes("{{") ? null : { text, references: linkReferences(text) };
|
|
131
|
+
markdownFiles.set(located, file);
|
|
132
|
+
}
|
|
133
|
+
return file;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The texts that meet in one document when Hugo reads a page: the page's own, and each
|
|
137
|
+
* Markdown shortcode file it calls (at most 500 names are looked at). Worked out once per page.
|
|
138
|
+
*/
|
|
139
|
+
const documents = new WeakMap();
|
|
140
|
+
function documentOf(ctx) {
|
|
141
|
+
const page = ctx.pageReferences?.();
|
|
142
|
+
if (!page)
|
|
143
|
+
return [];
|
|
144
|
+
let parts = documents.get(page);
|
|
145
|
+
if (!parts) {
|
|
146
|
+
parts = [page];
|
|
147
|
+
const names = new Set();
|
|
148
|
+
for (const m of (ctx.pageSource ?? "").matchAll(/\{\{%\s*([A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*)\s*\/?%\}\}/g)) {
|
|
149
|
+
if (names.size >= 500)
|
|
150
|
+
break;
|
|
151
|
+
names.add(m[1]);
|
|
152
|
+
}
|
|
153
|
+
for (const name of names) {
|
|
154
|
+
const located = ctx.shortcodeFile?.(name);
|
|
155
|
+
const file = located && markdownFile(located);
|
|
156
|
+
if (file)
|
|
157
|
+
parts.push(file.references);
|
|
158
|
+
}
|
|
159
|
+
documents.set(page, parts);
|
|
160
|
+
}
|
|
161
|
+
return parts;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* A shortcode the site wrote as a Markdown file with no template code in it prints that
|
|
165
|
+
* Markdown, whatever its name: it is an include. (Datadog's docs have about 150 of these in
|
|
166
|
+
* English, about 1,000 uses.) Not read, so the call is kept as written and reported: a file
|
|
167
|
+
* with any Go template code, and a call with arguments, inner text or `{{< >}}` (the file
|
|
168
|
+
* cannot use the first two, and Hugo does not read Markdown in the output of the third).
|
|
169
|
+
*/
|
|
170
|
+
const markdownShortcode = (node, ctx) => {
|
|
171
|
+
if (node.delimiter !== "percent" || !node.selfClosing || node.inner !== "")
|
|
172
|
+
return undefined;
|
|
173
|
+
if (node.args.positional.length > 0 || Object.keys(node.args.named).length > 0)
|
|
174
|
+
return undefined;
|
|
175
|
+
const located = ctx.shortcodeFile?.(node.name);
|
|
176
|
+
if (!located)
|
|
177
|
+
return undefined;
|
|
178
|
+
const file = markdownFile(located);
|
|
179
|
+
if (!file)
|
|
180
|
+
return undefined;
|
|
181
|
+
if (ctx.intent && ctx.sharedPath) {
|
|
182
|
+
// Hugo puts the text into the page BEFORE reading its Markdown, so a link reference in it
|
|
183
|
+
// and a definition in the page, or in another shortcode's file, meet. A shared file is
|
|
184
|
+
// read alone: only a text that does not depend on the others that way can be one. Any
|
|
185
|
+
// other is copied in, and then the page's Markdown is read as Hugo reads it.
|
|
186
|
+
const why = /^---[ \t]*\r?\n/.test(file.text)
|
|
187
|
+
? "it starts with a line of dashes, which a file read alone takes as front matter"
|
|
188
|
+
: dependsOnPage(file.references, documentOf(ctx));
|
|
189
|
+
const out = why ? undefined : ctx.sharedPath(located, "shortcode");
|
|
190
|
+
if (out) {
|
|
191
|
+
const baked = bakeIntent(ctx.translate(file.text), ctx.intent.store);
|
|
192
|
+
// as for a content file: a text that shows template syntax is copied in
|
|
193
|
+
return showsTemplateText(baked) ? baked : includeRef(ctx.intent, out, located.path, "shortcode", baked);
|
|
194
|
+
}
|
|
195
|
+
ctx.report({
|
|
196
|
+
shortcode: node.name,
|
|
197
|
+
action: "degraded",
|
|
198
|
+
note: `a shortcode that is a Markdown file is copied into the page, not included: ${why ?? "another file is already shared under its name"}`,
|
|
199
|
+
line: node.line,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return ctx.translate(file.text);
|
|
203
|
+
};
|
|
204
|
+
/** One tab of a group: `name=`, `header=` or `title=` names it (Docsy's `tab` is the same shape). */
|
|
205
|
+
export const tabMapping = SHORTCODES.tab;
|
|
206
|
+
export const CONVENTIONS = {
|
|
207
|
+
name: "conventions",
|
|
208
|
+
description: "note, caution, warning, tabs, tab, table, mermaid, include, comment: by their usual meaning; a shortcode that is a Markdown file: its text",
|
|
209
|
+
// Not detected: there is nothing to tell a site's `note` from another's. On unless turned off.
|
|
210
|
+
detect: () => "names most Hugo docs sites define, mapped by their usual meaning",
|
|
211
|
+
shortcodes: SHORTCODES,
|
|
212
|
+
fallback: markdownShortcode,
|
|
213
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Datadog's docs (`DataDog/documentation`, the Hugo site in its `hugo/` folder).
|
|
3
|
+
*
|
|
4
|
+
* The site defines about 465 shortcodes. The ones mapped here are the ones that are a general
|
|
5
|
+
* construct under a Datadog name (bold UI labels, images, code blocks, collapsible sections,
|
|
6
|
+
* link lists, language tabs): about 31,000 uses in the English content. Its roughly 150
|
|
7
|
+
* shortcodes that are Markdown files are read by the `conventions` adapter's rule.
|
|
8
|
+
*
|
|
9
|
+
* The reader's Datadog site (plans/reader-choices.md, phase 3): Hugo ships every region's content
|
|
10
|
+
* and a script shows the reader's. A migration (`keepIntent`) with the region table
|
|
11
|
+
* (`.Site.Data.regions`, which Datadog mounts from `../shared/regions.yaml`: outside the Hugo
|
|
12
|
+
* folder, so the host passes it, `migrate-hugo --site-data regions=<file>`) carries that as a
|
|
13
|
+
* reader choice, `site`: `site-region` → `:::when{site="…"}`, `region-param` → `:choice-value`
|
|
14
|
+
* (in code too, the code marked `{choices}`), the table → `choices.site`. Without the table, or
|
|
15
|
+
* in a conversion that keeps no intent, both are kept as written and reported.
|
|
16
|
+
*
|
|
17
|
+
* Not mapped, and so kept as written and reported: the shortcodes that compute their output
|
|
18
|
+
* from data files or the Datadog API.
|
|
19
|
+
*
|
|
20
|
+
* Counts in the comments are uses in `content/en` at commit c766537 (2026-11-23).
|
|
21
|
+
*/
|
|
22
|
+
import { type HugoSiteAdapter } from "../registry.js";
|
|
23
|
+
export declare const DATADOG: HugoSiteAdapter;
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Datadog's docs (`DataDog/documentation`, the Hugo site in its `hugo/` folder).
|
|
3
|
+
*
|
|
4
|
+
* The site defines about 465 shortcodes. The ones mapped here are the ones that are a general
|
|
5
|
+
* construct under a Datadog name (bold UI labels, images, code blocks, collapsible sections,
|
|
6
|
+
* link lists, language tabs): about 31,000 uses in the English content. Its roughly 150
|
|
7
|
+
* shortcodes that are Markdown files are read by the `conventions` adapter's rule.
|
|
8
|
+
*
|
|
9
|
+
* The reader's Datadog site (plans/reader-choices.md, phase 3): Hugo ships every region's content
|
|
10
|
+
* and a script shows the reader's. A migration (`keepIntent`) with the region table
|
|
11
|
+
* (`.Site.Data.regions`, which Datadog mounts from `../shared/regions.yaml`: outside the Hugo
|
|
12
|
+
* folder, so the host passes it, `migrate-hugo --site-data regions=<file>`) carries that as a
|
|
13
|
+
* reader choice, `site`: `site-region` → `:::when{site="…"}`, `region-param` → `:choice-value`
|
|
14
|
+
* (in code too, the code marked `{choices}`), the table → `choices.site`. Without the table, or
|
|
15
|
+
* in a conversion that keeps no intent, both are kept as written and reported.
|
|
16
|
+
*
|
|
17
|
+
* Not mapped, and so kept as written and reported: the shortcodes that compute their output
|
|
18
|
+
* from data files or the Datadog API.
|
|
19
|
+
*
|
|
20
|
+
* Counts in the comments are uses in `content/en` at commit c766537 (2026-11-23).
|
|
21
|
+
*/
|
|
22
|
+
import { attrBlock, directive } from "../registry.js";
|
|
23
|
+
import { choiceValueRef } from "../intent.js";
|
|
24
|
+
/** Text inside `[…]` of a link or an image: one line, its brackets escaped. */
|
|
25
|
+
const label = (text) => text.replace(/\s+/g, " ").trim().replace(/\\[\s\S]|[\\[\]]/g, (m) => (m.length === 2 ? m : `\\${m}`));
|
|
26
|
+
/** Text Hugo prints as plain text (front matter), inside `[…]`: nothing in it is Markdown or HTML. */
|
|
27
|
+
const plain = (text) => text.replace(/\s+/g, " ").trim().replace(/([\\[\]*_`<>&~|:!{}])/g, "\\$1");
|
|
28
|
+
/**
|
|
29
|
+
* One line of the site's text for a quoted attribute. A run of three colons would be read as
|
|
30
|
+
* a block's fence, wherever it is written: it is made two.
|
|
31
|
+
*/
|
|
32
|
+
const attrText = (text) => text.replace(/\s+/g, " ").trim().replace(/:{3,}/g, "::");
|
|
33
|
+
/** A text without the blank lines around it (a loop: a pattern is slow on a long run of them). */
|
|
34
|
+
function trimNewlines(text) {
|
|
35
|
+
let start = 0;
|
|
36
|
+
let end = text.length;
|
|
37
|
+
while (start < end && text.charCodeAt(start) === 10)
|
|
38
|
+
start++;
|
|
39
|
+
while (end > start && text.charCodeAt(end - 1) === 10)
|
|
40
|
+
end--;
|
|
41
|
+
return text.slice(start, end);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A link target the site wrote, as Datadog's templates read one: `http…` as it is, anything
|
|
45
|
+
* else a path of the site. Undefined when it could not be written inside `(…)` or an attribute.
|
|
46
|
+
*/
|
|
47
|
+
function target(href) {
|
|
48
|
+
const value = (href ?? "").trim();
|
|
49
|
+
if (value === "" || /[\s()<>"\\{}]/.test(value))
|
|
50
|
+
return undefined;
|
|
51
|
+
if (/^https?:\/\//i.test(value))
|
|
52
|
+
return value;
|
|
53
|
+
// a scheme other than http is not a link these shortcodes make (`javascript:`)
|
|
54
|
+
if (/^[A-Za-z][A-Za-z0-9+.-]*:/.test(value))
|
|
55
|
+
return undefined;
|
|
56
|
+
return value.startsWith("/") || value.startsWith("#") ? value : `/${value}`;
|
|
57
|
+
}
|
|
58
|
+
/** A fenced code block, its fence longer than any run of backticks in the code. */
|
|
59
|
+
function fenced(code, info) {
|
|
60
|
+
const fence = "`".repeat(Math.max(3, ...[...code.matchAll(/`{3,}/g)].map((m) => m[0].length + 1)));
|
|
61
|
+
return `${fence}${info}\n${code}\n${fence}`;
|
|
62
|
+
}
|
|
63
|
+
/** Each line from its first character: a list written indented inside a shortcode is not code. */
|
|
64
|
+
const flushLeft = (text) => text.split("\n").map((line) => line.trim()).filter(Boolean).join("\n");
|
|
65
|
+
/** The pages use the choice: its config goes into the site's `choices:`. */
|
|
66
|
+
function regionsUsed(ctx, regions) {
|
|
67
|
+
if (!ctx.intent.store.choices.has("site"))
|
|
68
|
+
ctx.intent.store.choices.set("site", regions.config);
|
|
69
|
+
}
|
|
70
|
+
const degraded = (ctx, node, note) => ctx.report({ shortcode: node.name, action: "degraded", note, line: node.line });
|
|
71
|
+
/** One line of a link list: the link, and its tag as a badge. `text` is already escaped. */
|
|
72
|
+
function linkItem(text, href, tag) {
|
|
73
|
+
const badge = typeof tag === "string" && tag.trim() ? ` :badge[${plain(tag).toUpperCase()}]{variant="outline"}` : "";
|
|
74
|
+
return `- [${text}](${href})${badge}`;
|
|
75
|
+
}
|
|
76
|
+
const REGION = /^[a-z][a-z0-9-]{0,39}$/;
|
|
77
|
+
const VALUE_KEY = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$/;
|
|
78
|
+
const tables = new WeakMap();
|
|
79
|
+
/** The regions of the `site-region` blocks being translated, innermost last (per import). */
|
|
80
|
+
const openRegions = new WeakMap();
|
|
81
|
+
/**
|
|
82
|
+
* `.Site.Data.regions` read as the choice `site`, once per import (the intent store's). Undefined
|
|
83
|
+
* when the import keeps no intent or there is no usable table: the mappings then keep the calls.
|
|
84
|
+
*/
|
|
85
|
+
function regionsOf(ctx) {
|
|
86
|
+
const store = ctx.intent?.store;
|
|
87
|
+
if (!store)
|
|
88
|
+
return undefined;
|
|
89
|
+
if (tables.has(store))
|
|
90
|
+
return tables.get(store) ?? undefined;
|
|
91
|
+
const raw = ctx.readSiteData?.("regions");
|
|
92
|
+
const list = Array.isArray(raw?.regions) ? raw.regions : [];
|
|
93
|
+
const rows = list
|
|
94
|
+
.filter((r) => r && typeof r === "object" && typeof r.key === "string" && REGION.test(r.key))
|
|
95
|
+
.slice(0, 100)
|
|
96
|
+
.sort((a, b) => (Number(a.weight) || 0) - (Number(b.weight) || 0));
|
|
97
|
+
if (rows.length < 2) {
|
|
98
|
+
tables.set(store, null);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const keys = new Set();
|
|
102
|
+
for (const r of rows) {
|
|
103
|
+
const v = r.values && typeof r.values === "object" ? r.values : {};
|
|
104
|
+
for (const k of Object.keys(v).slice(0, 1000))
|
|
105
|
+
if (VALUE_KEY.test(k) && k !== "label" && k !== "platform")
|
|
106
|
+
keys.add(k);
|
|
107
|
+
}
|
|
108
|
+
let html = false;
|
|
109
|
+
const entries = {};
|
|
110
|
+
for (const r of rows) {
|
|
111
|
+
const v = r.values && typeof r.values === "object" ? r.values : {};
|
|
112
|
+
const entry = { label: typeof r.label === "string" && r.label.trim() ? r.label.trim() : r.key };
|
|
113
|
+
for (const k of [...keys].sort()) {
|
|
114
|
+
const x = Object.prototype.hasOwnProperty.call(v, k) ? v[k] : "";
|
|
115
|
+
let text = typeof x === "string" ? x : typeof x === "number" || typeof x === "boolean" ? String(x) : "";
|
|
116
|
+
// a value is text on the page; Datadog's script wrote some as HTML (`a<br>b`): a list
|
|
117
|
+
if (/<br\s*\/?>/i.test(text)) {
|
|
118
|
+
html = true;
|
|
119
|
+
text = text.replace(/\s*<br\s*\/?>\s*/gi, ", ");
|
|
120
|
+
}
|
|
121
|
+
entry[k] = text;
|
|
122
|
+
}
|
|
123
|
+
entries[r.key] = entry;
|
|
124
|
+
}
|
|
125
|
+
const values = rows.map((r) => r.key);
|
|
126
|
+
const def = values.includes("us") ? "us" : values[0];
|
|
127
|
+
const regions = {
|
|
128
|
+
values,
|
|
129
|
+
defaults: Object.fromEntries([...keys].map((k) => [k, entries[def][k]])),
|
|
130
|
+
keys,
|
|
131
|
+
config: { label: "Datadog site", param: "site", default: def, values: entries },
|
|
132
|
+
};
|
|
133
|
+
if (html)
|
|
134
|
+
ctx.report({ shortcode: "region-param", action: "degraded", note: "a region value written as HTML (`<br>` between hosts) is text on the page: its line breaks are written as commas", line: 0 });
|
|
135
|
+
tables.set(store, regions);
|
|
136
|
+
return regions;
|
|
137
|
+
}
|
|
138
|
+
const NO_TABLE = "kept as written: the region table was not read (pass the file Datadog mounts as data/regions.yaml with `--site-data regions=<file>`)";
|
|
139
|
+
const SHORTCODES = {
|
|
140
|
+
// 502 openers. Content for some Datadog sites: shown when the reader's is one of them.
|
|
141
|
+
"site-region": (node, ctx) => {
|
|
142
|
+
if (!ctx.intent)
|
|
143
|
+
return undefined;
|
|
144
|
+
const regions = regionsOf(ctx);
|
|
145
|
+
if (!regions) {
|
|
146
|
+
ctx.report({ shortcode: node.name, action: "preserved", note: NO_TABLE, line: node.line });
|
|
147
|
+
return node.raw;
|
|
148
|
+
}
|
|
149
|
+
const named = [...new Set((node.args.named.region ?? "").split(/[\s,]+/).map((r) => r.trim().toLowerCase()).filter(Boolean))];
|
|
150
|
+
const unknown = named.filter((r) => !regions.values.includes(r));
|
|
151
|
+
if (named.length === 0 || unknown.length) {
|
|
152
|
+
ctx.report({ shortcode: node.name, action: "preserved", note: `kept as written: ${named.length ? `"${unknown[0].slice(0, 20)}" is not a region of the table` : "it names no region"}`, line: node.line });
|
|
153
|
+
return node.raw;
|
|
154
|
+
}
|
|
155
|
+
// inside another block: shown only for the regions both name (Datadog nests a `us` block's
|
|
156
|
+
// `eu` text in places: Hugo shows it to no reader, and the build would refuse it)
|
|
157
|
+
const stack = openRegions.get(ctx.intent.store) ?? [];
|
|
158
|
+
const outer = stack[stack.length - 1];
|
|
159
|
+
if (outer && !named.some((r) => outer.includes(r))) {
|
|
160
|
+
ctx.report({ shortcode: node.name, action: "stripped", note: `left out: it is inside a block for ${outer.join(", ")} and names none of them, so Hugo shows it to no reader`, line: node.line });
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
regionsUsed(ctx, regions);
|
|
164
|
+
stack.push(outer ? named.filter((r) => outer.includes(r)) : named);
|
|
165
|
+
openRegions.set(ctx.intent.store, stack);
|
|
166
|
+
let body;
|
|
167
|
+
try {
|
|
168
|
+
// (its first and last lines, blank or only the indent the closer was written at, go)
|
|
169
|
+
body = ctx.translate(node.inner).replace(/^[ \t]*\n/, "").replace(/\n[ \t]*$/, "");
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
stack.pop();
|
|
173
|
+
}
|
|
174
|
+
const all = regions.values.every((r) => named.includes(r));
|
|
175
|
+
const attrs = `{site="${regions.values.filter((r) => named.includes(r)).join(" ")}"}`;
|
|
176
|
+
if (!node.raw.includes("\n") && !ctx.alone?.()) {
|
|
177
|
+
// written inside a line (a table cell, a sentence): a span, or the line would be split
|
|
178
|
+
const inner = body.trim();
|
|
179
|
+
if (all)
|
|
180
|
+
return inner;
|
|
181
|
+
if (/[\n\]]|:{3,}/.test(inner)) {
|
|
182
|
+
ctx.report({ shortcode: node.name, action: "preserved", note: "kept as written: written inside a line, and its text cannot be a span", line: node.line });
|
|
183
|
+
return node.raw;
|
|
184
|
+
}
|
|
185
|
+
return `:when[${inner}]${attrs}`;
|
|
186
|
+
}
|
|
187
|
+
// every region: no condition at all
|
|
188
|
+
if (all)
|
|
189
|
+
return trimNewlines(body);
|
|
190
|
+
return directive("when", attrs, body);
|
|
191
|
+
},
|
|
192
|
+
// 1,125 calls (419 in code). One of the reader's site's values, by key: text, inline code or a link.
|
|
193
|
+
"region-param": (node, ctx) => {
|
|
194
|
+
if (!ctx.intent)
|
|
195
|
+
return undefined;
|
|
196
|
+
const regions = regionsOf(ctx);
|
|
197
|
+
if (!regions) {
|
|
198
|
+
if (!node.inCode)
|
|
199
|
+
ctx.report({ shortcode: node.name, action: "preserved", note: NO_TABLE, line: node.line });
|
|
200
|
+
return node.raw;
|
|
201
|
+
}
|
|
202
|
+
// (written inside a JSON string in code: `key=\"dd_site\"`, which Hugo reads as the name)
|
|
203
|
+
const key = (node.args.named.key ?? "").trim().replace(/^\\?"|\\?"$/g, "");
|
|
204
|
+
if (!regions.keys.has(key)) {
|
|
205
|
+
if (!node.inCode)
|
|
206
|
+
ctx.report({ shortcode: node.name, action: "preserved", note: `kept as written: "${key.slice(0, 40)}" is not a key of the region table`, line: node.line });
|
|
207
|
+
return node.raw;
|
|
208
|
+
}
|
|
209
|
+
regionsUsed(ctx, regions);
|
|
210
|
+
const form = node.args.named.code === "true" ? " code" : node.args.named.link === "true" ? " link" : "";
|
|
211
|
+
// the site's text, and kept on a template page: `{` written as a character reference, so
|
|
212
|
+
// `{{`, `{%` and `{#` in it are text, never template syntax
|
|
213
|
+
const text = form === " link" ? label(node.args.named.text ?? "").replace(/\{/g, "{") : "";
|
|
214
|
+
return choiceValueRef(ctx.intent, `:choice-value[${text}]{site="${key}"${form}}`, regions.defaults[key] ?? "");
|
|
215
|
+
},
|
|
216
|
+
// 16,788 uses. `<span class="ui">`: the name of something on screen, shown bold.
|
|
217
|
+
ui: (node, ctx) => {
|
|
218
|
+
const inner = ctx.translate(node.inner);
|
|
219
|
+
const text = inner.trim();
|
|
220
|
+
if (text === "")
|
|
221
|
+
return "";
|
|
222
|
+
// emphasis does not open or close beside a space: the spaces go outside it
|
|
223
|
+
const lead = inner.slice(0, inner.length - inner.trimStart().length);
|
|
224
|
+
const tail = inner.slice(inner.trimEnd().length);
|
|
225
|
+
return `${lead}**${text}**${tail}`;
|
|
226
|
+
},
|
|
227
|
+
// 1,617 uses, in tables: a check mark (an icon font's, in Hugo).
|
|
228
|
+
X: () => "✓",
|
|
229
|
+
// 5,382 uses. An image under the site's `images/` folder; `video="true"` makes it a video.
|
|
230
|
+
// The width set in `style=` and the pop-up are presentation, and are not carried.
|
|
231
|
+
img: (node, ctx) => {
|
|
232
|
+
const src = (node.args.named.src ?? "").trim().replace(/^\/+/, "");
|
|
233
|
+
if (src === "" || !/^[\w./@ +~-]+$/.test(src) || src.split("/").includes(".."))
|
|
234
|
+
return undefined;
|
|
235
|
+
const url = `/images/${src.replace(/ /g, "%20")}`;
|
|
236
|
+
const said = (node.args.named.alt ?? "").replace(/\s+/g, " ").trim();
|
|
237
|
+
const alt = label(said);
|
|
238
|
+
if (node.args.named.video === "true") {
|
|
239
|
+
const title = said ? ` title="${said.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">")}"` : "";
|
|
240
|
+
return `<video src="${url}"${title} controls muted loop playsinline></video>`;
|
|
241
|
+
}
|
|
242
|
+
const image = ``;
|
|
243
|
+
const caption = node.args.named.caption ? attrText(node.args.named.caption) : "";
|
|
244
|
+
return caption ? directive("figure", attrBlock({ caption }), image) : image;
|
|
245
|
+
},
|
|
246
|
+
// 1,858 uses. A code block with a language, and often a file name shown above it.
|
|
247
|
+
"code-block": (node, ctx) => {
|
|
248
|
+
const lang = /^[\w+#.-]+$/.test(node.args.named.lang ?? "") ? node.args.named.lang : "";
|
|
249
|
+
let filename = node.args.named.filename ? attrText(node.args.named.filename) : "";
|
|
250
|
+
if (filename.includes("`")) {
|
|
251
|
+
// (a fence's first line cannot hold a backtick)
|
|
252
|
+
degraded(ctx, node, "the file name holds a backtick and is not shown above the code");
|
|
253
|
+
filename = "";
|
|
254
|
+
}
|
|
255
|
+
// Hugo runs the shortcodes written in the code (a value that depends on the site)
|
|
256
|
+
const code = trimNewlines(ctx.translate(node.inner));
|
|
257
|
+
const title = filename ? `${lang ? " " : ""}${attrBlock({ title: filename })}` : "";
|
|
258
|
+
return fenced(code, `${lang}${title}`);
|
|
259
|
+
},
|
|
260
|
+
// 947 uses. A collapsible section; its heading level is presentation.
|
|
261
|
+
"collapse-content": (node, ctx) => {
|
|
262
|
+
const title = attrText(node.args.named.title ?? "");
|
|
263
|
+
const id = /^[A-Za-z][\w-]*$/.test(node.args.named.id ?? "") ? `#${node.args.named.id} ` : "";
|
|
264
|
+
const open = node.args.named.expanded === "true" ? " open" : "";
|
|
265
|
+
// (no title: the section's default label, not an empty one)
|
|
266
|
+
const named = attrBlock({ title }).slice(1, -1);
|
|
267
|
+
const inside = `${id}${named}${open}`.trim();
|
|
268
|
+
const attrs = inside ? `{${inside}}` : "";
|
|
269
|
+
return directive("details", attrs, ctx.translate(node.inner));
|
|
270
|
+
},
|
|
271
|
+
// 178 uses. A box inviting the reader to a preview, with a "Request Access" button.
|
|
272
|
+
callout: (node, ctx) => {
|
|
273
|
+
const header = node.args.named.header;
|
|
274
|
+
const title = header === "false" ? undefined : attrText(header ?? "Join the Preview!");
|
|
275
|
+
let body = trimNewlines(ctx.translate(node.inner));
|
|
276
|
+
if (node.args.named.btn_hidden !== "true") {
|
|
277
|
+
const href = target(node.args.named.url);
|
|
278
|
+
if (href)
|
|
279
|
+
body += `${body ? "\n\n" : ""}:button[Request Access]{href="${href}"}`;
|
|
280
|
+
else if (node.args.named.url)
|
|
281
|
+
degraded(ctx, node, "the button's address could not be written as a link: the box has no button");
|
|
282
|
+
}
|
|
283
|
+
return directive("info", attrBlock({ title }), body);
|
|
284
|
+
},
|
|
285
|
+
// 413 uses. A sentence and a list of links, each a `nextlink` (2,176 uses).
|
|
286
|
+
whatsnext: (node, ctx) => {
|
|
287
|
+
const desc = node.args.named.desc?.replace(/\s+/g, " ").trim() || `${ctx.i18n?.additional_sentence ?? "Further Reading"}:`;
|
|
288
|
+
return `${desc}\n\n${flushLeft(ctx.translate(node.inner))}\n`;
|
|
289
|
+
},
|
|
290
|
+
nextlink: (node, ctx) => {
|
|
291
|
+
const href = target(node.args.named.href);
|
|
292
|
+
const text = ctx.translate(node.inner);
|
|
293
|
+
if (!href) {
|
|
294
|
+
degraded(ctx, node, "the link's address could not be written as a link: its text is kept");
|
|
295
|
+
return `- ${label(text)}\n`;
|
|
296
|
+
}
|
|
297
|
+
return `${linkItem(label(text), href, node.args.named.tag)}\n`;
|
|
298
|
+
},
|
|
299
|
+
// 2,441 uses, 2,381 of them the "Further Reading" list: the links of the page's own front
|
|
300
|
+
// matter (`further_reading`). Any other partial is the site's template code: kept as written.
|
|
301
|
+
partial: (node, ctx) => {
|
|
302
|
+
if (node.args.named.name !== "whats-next/whats-next.html")
|
|
303
|
+
return undefined;
|
|
304
|
+
// (a shared file is read without a page: the list is each including page's own, so the
|
|
305
|
+
// call is kept as written there rather than dropped)
|
|
306
|
+
if (!ctx.pageParams)
|
|
307
|
+
return undefined;
|
|
308
|
+
const links = ctx.pageParams.further_reading;
|
|
309
|
+
const items = [];
|
|
310
|
+
if (Array.isArray(links)) {
|
|
311
|
+
if (links.length > 200)
|
|
312
|
+
degraded(ctx, node, "the page lists more than 200 further reading entries: the first 200 are written");
|
|
313
|
+
for (const entry of links.slice(0, 200)) {
|
|
314
|
+
const { link, text, tag } = (entry && typeof entry === "object" ? entry : {});
|
|
315
|
+
const href = typeof link === "string" ? target(link) : undefined;
|
|
316
|
+
const said = typeof text === "string" ? plain(text) : "";
|
|
317
|
+
if (href && said)
|
|
318
|
+
items.push(linkItem(said, href, tag));
|
|
319
|
+
else if (said) {
|
|
320
|
+
degraded(ctx, node, "a further reading entry's address could not be written as a link: its text is kept");
|
|
321
|
+
items.push(`- ${said}`);
|
|
322
|
+
}
|
|
323
|
+
else
|
|
324
|
+
degraded(ctx, node, "a further reading entry has no text: it is left out");
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (items.length === 0) {
|
|
328
|
+
ctx.report({ shortcode: "partial", action: "stripped", note: "the page's front matter lists no further reading: the list is empty in Hugo too", line: node.line });
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
return `${ctx.i18n?.additional_sentence ?? "Further Reading"}:\n\n${items.join("\n")}\n`;
|
|
332
|
+
},
|
|
333
|
+
// 63 groups. Tabs, one per programming language; a tab's name is looked up in the site's
|
|
334
|
+
// `code_language_ids` (`dotnet` is ".NET").
|
|
335
|
+
"programming-lang-wrapper": (node, ctx) => directive("tabs", "", ctx.translate(node.inner)),
|
|
336
|
+
"programming-lang": (node, ctx) => {
|
|
337
|
+
const given = (node.args.named.lang ?? "").trim();
|
|
338
|
+
const id = given === ".NET" ? "dotnet" : given.toLowerCase();
|
|
339
|
+
const names = ctx.config.params.code_language_ids;
|
|
340
|
+
const known = names && typeof names === "object" ? names[id] : undefined;
|
|
341
|
+
const title = attrText(typeof known === "string" && known.trim() ? known : given);
|
|
342
|
+
return directive("tab", attrBlock({ title }), ctx.translate(node.inner));
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
/** Datadog's alert colours → callout types (its `alert-info` is blue, `alert-tip` green). */
|
|
346
|
+
const ALERT_TYPES = {
|
|
347
|
+
"alert-info": "info",
|
|
348
|
+
"alert-tip": "tip",
|
|
349
|
+
"alert-warning": "warning",
|
|
350
|
+
"alert-danger": "danger",
|
|
351
|
+
};
|
|
352
|
+
export const DATADOG = {
|
|
353
|
+
name: "datadog",
|
|
354
|
+
description: "Datadog's docs: ui, img, code-block, collapse-content, callout, whatsnext, the further reading list, language tabs",
|
|
355
|
+
// Three shortcodes only this site defines, all of them: one alone could be another site's.
|
|
356
|
+
detect: (site) => ["site-region", "region-param", "programming-lang-wrapper"].every((name) => site.shortcodes.has(name))
|
|
357
|
+
? "the site defines Datadog's own shortcodes"
|
|
358
|
+
: undefined,
|
|
359
|
+
shortcodes: SHORTCODES,
|
|
360
|
+
// Hugo runs shortcodes before Markdown: a value in a code block is a call (Datadog's API hosts)
|
|
361
|
+
valueShortcodes: ["region-param"],
|
|
362
|
+
// 2,327 `<div class="alert alert-…">` boxes in the English docs: Datadog's notes are HTML,
|
|
363
|
+
// not a shortcode. As callouts, a region value in one switches with the reader's site.
|
|
364
|
+
htmlBlocks: [
|
|
365
|
+
{
|
|
366
|
+
name: "alert div",
|
|
367
|
+
tag: "div",
|
|
368
|
+
directive: (classes) => {
|
|
369
|
+
for (const c of classes)
|
|
370
|
+
if (ALERT_TYPES[c])
|
|
371
|
+
return ALERT_TYPES[c];
|
|
372
|
+
return classes.includes("alert") ? "note" : undefined;
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docsy, the theme of kubernetes.io and of many other Hugo docs sites.
|
|
3
|
+
*
|
|
4
|
+
* Its shortcodes come with the theme, so a site that uses Docsy has them without defining them.
|
|
5
|
+
* Only the ones measured on real content are mapped; `blocks/*`, `cardpane`, `imgproc`,
|
|
6
|
+
* `readfile`, `swaggerui` are left as written and reported.
|
|
7
|
+
*/
|
|
8
|
+
import { type HugoSiteAdapter } from "../registry.js";
|
|
9
|
+
export declare const DOCSY: HugoSiteAdapter;
|