@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/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { scanShortcodes, parseArgs, stripContentTypeMarkers, CONTENT_TYPE_MARKERS, } from "./shortcodes.js";
|
|
2
|
-
export { detectHugo, loadHugoConfig, lookupParam, contentDirFor, } from "./config.js";
|
|
2
|
+
export { detectHugo, loadHugoConfig, lookupParam, contentDirFor, loadHugoI18n, } from "./config.js";
|
|
3
3
|
export { arg, attrBlock, directive, composeRegistry, } 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 } from "./adapters/index.js";
|
|
6
7
|
export { makeContentResolver, looksLikeFile } from "./resolve.js";
|
|
7
8
|
export { translateHugo } from "./translate.js";
|
|
9
|
+
export { rewriteHtmlBlocks, convertInlineHtml } from "./html-blocks.js";
|
|
8
10
|
export { collectHugoPages, buildHugoNav, makeRefResolver, splitFrontmatter, } from "./nav.js";
|
|
9
11
|
export { importHugo, summariseHugoLedger, plugin, } from "./cli.js";
|
package/dist/intent.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/** What an import collects while keeping intent, shared by all its pages. */
|
|
2
|
+
export interface HugoIntentStore {
|
|
3
|
+
/** Site attributes the pages refer to (name → value), for the config's `attributes:`. */
|
|
4
|
+
attributes: Map<string, string>;
|
|
5
|
+
/**
|
|
6
|
+
* Text written in front of an attribute where the site param has it and the attribute does
|
|
7
|
+
* not: kubernetes' `version` param is `v1.36`, and its `skew` shortcode prints `1.36`. The
|
|
8
|
+
* attribute holds the number; `{{< param "version" >}}` becomes `v{{ version }}`.
|
|
9
|
+
*/
|
|
10
|
+
prefixes: Map<string, string>;
|
|
11
|
+
/** Files written once and included by pages: output path in the content folder → source file. */
|
|
12
|
+
shared: Map<string, {
|
|
13
|
+
source: string;
|
|
14
|
+
kind: "include" | "example" | "shortcode";
|
|
15
|
+
}>;
|
|
16
|
+
/** Data files the pages read (`data.<name>` on a template page): name → what the file holds. */
|
|
17
|
+
data: Map<string, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Reader choices the pages use (plans/reader-choices.md), by name: each choice's config as
|
|
20
|
+
* the site's `choices:` takes it (Datadog's `site`, from its region table).
|
|
21
|
+
*/
|
|
22
|
+
choices: Map<string, unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* This import's key, written into every mark the import makes (below). Text from the site
|
|
25
|
+
* (a page, an included file, a translated label, a param) cannot know it, so it cannot hold a
|
|
26
|
+
* mark of its own: without it, a label of the site's that spelled out a mark was read as one
|
|
27
|
+
* and its expression ran on the page (review 2026-11-23).
|
|
28
|
+
*/
|
|
29
|
+
key: string;
|
|
30
|
+
}
|
|
31
|
+
export declare function newIntentStore(): HugoIntentStore;
|
|
32
|
+
/** Keeping intent for ONE page (or one shared fragment). */
|
|
33
|
+
export interface HugoIntent {
|
|
34
|
+
store: HugoIntentStore;
|
|
35
|
+
/** Where the page is written, relative to the content folder, with `/` (for include paths). */
|
|
36
|
+
pagePath: string;
|
|
37
|
+
/** False for a shared fragment: it is included into pages that may not be template pages. */
|
|
38
|
+
allowTemplate: boolean;
|
|
39
|
+
/** With `allowTemplate` false for another reason: that reason, for the report. */
|
|
40
|
+
whyNotTemplate?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Values this page computes: name → expression. A template page sets each at its top
|
|
43
|
+
* (`{% set prev_minor_version = … %}`) and its text says `{{ prev_minor_version }}`: an
|
|
44
|
+
* expression written where it is used does not survive a table cell (its `|`) or a link.
|
|
45
|
+
*/
|
|
46
|
+
definitions?: Map<string, string>;
|
|
47
|
+
}
|
|
48
|
+
/** Source text with no character the markers use. */
|
|
49
|
+
export declare const withoutMarkerChars: (text: string) => string;
|
|
50
|
+
/** Whether text holds one of the characters the markers use. */
|
|
51
|
+
export declare const hasMarkerChars: (text: string) => boolean;
|
|
52
|
+
/** A site attribute name a page can write as `{{ name }}`. */
|
|
53
|
+
export declare const isAttributeName: (name: string) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* A site param as its attribute: `{{ name }}`, with the prefix the attribute dropped. `bare`
|
|
56
|
+
* leaves the prefix out (kubernetes' `skew`, which prints the number). `value` is the param's
|
|
57
|
+
* value as the site config has it.
|
|
58
|
+
*/
|
|
59
|
+
export declare function attributeRef(intent: HugoIntent, name: string, value: string, bare?: boolean): string;
|
|
60
|
+
/**
|
|
61
|
+
* A reader choice's value (plans/reader-choices.md): `:choice-value[…]{site="key" …}` as written
|
|
62
|
+
* in prose (in code, the plain form, and the code marked `{choices}`), with the default's value.
|
|
63
|
+
* The caller answers for the text: the choice and key are checked names.
|
|
64
|
+
*/
|
|
65
|
+
export declare function choiceValueRef(intent: HugoIntent, text: string, baked: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* A value only a template page computes, by name, with the value it has today. `print` is what
|
|
68
|
+
* the page says where the value is used (default: the name); it must use the name as a word.
|
|
69
|
+
*/
|
|
70
|
+
export declare function expressionRef(intent: HugoIntent, name: string, expression: string, baked: string, print?: string): string;
|
|
71
|
+
/**
|
|
72
|
+
* Template text only a template page runs (a loop over a data file), with what it gives today.
|
|
73
|
+
* It may use names set with `define`. The caller answers for the text: nothing of the site's
|
|
74
|
+
* goes into it unchecked.
|
|
75
|
+
*/
|
|
76
|
+
export declare function templateRef(intent: HugoIntent, template: string, baked: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* As `templateRef`, for lines of template text that stand as a block of their own (a loop that
|
|
79
|
+
* writes a table's rows). Markdown would read those lines as its own (a statement beside a
|
|
80
|
+
* table row becomes a row), so the block is named in `ResolvedIntent.blocks` for whoever reads
|
|
81
|
+
* the page as Markdown to carry it across untouched (`liftTemplateBlocks`).
|
|
82
|
+
*/
|
|
83
|
+
export declare function templateBlock(intent: HugoIntent, template: string, baked: string): string;
|
|
84
|
+
/** A value a template page sets at its top, for `templateRef` text to use by name. */
|
|
85
|
+
export declare function define(intent: HugoIntent, name: string, expression: string): void;
|
|
86
|
+
/** The `{% set %}` lines a template page starts with, for the computed values it kept. */
|
|
87
|
+
export declare function definitionLines(intent: HugoIntent, names: string[]): string;
|
|
88
|
+
/**
|
|
89
|
+
* An include of a shared file, with the text it stands for. `outPath` is where the file is
|
|
90
|
+
* written in the content folder; the statement names it relative to the page, as includes
|
|
91
|
+
* resolve. `trim` writes `-%}`, so the file's last newline is not a blank line in a code block.
|
|
92
|
+
*/
|
|
93
|
+
export declare function includeRef(intent: HugoIntent, outPath: string, source: string, kind: "include" | "example" | "shortcode", baked: string, trim?: boolean): string;
|
|
94
|
+
/** Text with every marker replaced by its value: for content that is itself copied into a page. */
|
|
95
|
+
export declare const bakeIntent: (text: string, store: HugoIntentStore) => string;
|
|
96
|
+
/**
|
|
97
|
+
* Whether markdown shows template syntax outside code. A shared file that does cannot be
|
|
98
|
+
* included: on a template page its text would run, and a page that includes it could not be
|
|
99
|
+
* told apart from one that does not.
|
|
100
|
+
*/
|
|
101
|
+
export declare function showsTemplateText(markdown: string): boolean;
|
|
102
|
+
export interface ResolvedIntent {
|
|
103
|
+
markdown: string;
|
|
104
|
+
/** The page needs `template: jinja`. */
|
|
105
|
+
template: boolean;
|
|
106
|
+
/** The computed values the page kept by name (`expressionRef`): each needs its `{% set %}`. */
|
|
107
|
+
computed: string[];
|
|
108
|
+
/** One line for each place the value was written where the intent form could not be. */
|
|
109
|
+
notes: string[];
|
|
110
|
+
/** The blocks of template text the page kept (`templateBlock`), as written in `markdown`. */
|
|
111
|
+
blocks: string[];
|
|
112
|
+
}
|
|
113
|
+
/** Pick, for every marker of a page, the intent form or the value. */
|
|
114
|
+
export declare function resolveIntent(marked: string, allowTemplate: boolean, store: HugoIntentStore, whyNotTemplate?: string): ResolvedIntent;
|
|
115
|
+
/**
|
|
116
|
+
* A page's blocks of template text, each replaced by a word on a line of its own, so the page
|
|
117
|
+
* can be read as Markdown: `restore` gives a word's block back. The words carry `key` (the
|
|
118
|
+
* import's own), which no text of the site's can hold.
|
|
119
|
+
*/
|
|
120
|
+
export declare function liftTemplateBlocks(markdown: string, blocks: readonly string[], key: string): {
|
|
121
|
+
markdown: string;
|
|
122
|
+
restore: Map<string, string>;
|
|
123
|
+
};
|
package/dist/intent.js
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping the source's INTENT, not only its output.
|
|
3
|
+
*
|
|
4
|
+
* A Hugo page says `{{< param "version" >}}`, `{{< include "prereqs.md" >}}`: one value, one
|
|
5
|
+
* text, used in many places. Writing the value or the text into every page gives the right
|
|
6
|
+
* output today and a site where a release means finding and editing each copy. A migration to
|
|
7
|
+
* a Dogsbay site therefore writes what Dogsbay has for the same intent:
|
|
8
|
+
*
|
|
9
|
+
* a site param → `{{ version }}`, defined once under `attributes:` in the config
|
|
10
|
+
* version arithmetic → an expression on that attribute, on a template page
|
|
11
|
+
* an included text → `{% include %}` of one shared file
|
|
12
|
+
* an example file → a code block that includes the file
|
|
13
|
+
*
|
|
14
|
+
* A mapping cannot know, where it runs, whether its intent form is usable: a variable inside a
|
|
15
|
+
* code block is read only when the block is marked `{minja}`, and marking it would also run any
|
|
16
|
+
* Go or Helm template text the block shows; an expression needs a template page, which would
|
|
17
|
+
* also run template text the page's prose shows; an include must stand on its own line. So a
|
|
18
|
+
* mapping writes BOTH forms into a marker, and `resolveIntent` picks one for each, once the
|
|
19
|
+
* whole page is known. The picked-against form is the value as Hugo prints it, never nothing.
|
|
20
|
+
*/
|
|
21
|
+
import { posix } from "node:path";
|
|
22
|
+
import { codeRanges, literalRanges } from "./shortcodes.js";
|
|
23
|
+
export function newIntentStore() {
|
|
24
|
+
const bytes = new Uint8Array(12);
|
|
25
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
26
|
+
const key = [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
27
|
+
return { attributes: new Map(), prefixes: new Map(), shared: new Map(), data: new Map(), choices: new Map(), key };
|
|
28
|
+
}
|
|
29
|
+
// Unicode NONCHARACTERS: they are not text (never an icon-font glyph, as a private-use character
|
|
30
|
+
// could be), so taking them out of a source page loses nothing a reader would see.
|
|
31
|
+
const OPEN = "\uFDD0";
|
|
32
|
+
const CLOSE = "\uFDD1";
|
|
33
|
+
const SEP = "\uFDD2";
|
|
34
|
+
const SLOT = "\uFDD3";
|
|
35
|
+
const PRIVATE = /[\uFDD0-\uFDD3]/g;
|
|
36
|
+
/** The marks of one import: only those written with its key. */
|
|
37
|
+
const markerOf = (store) => new RegExp(`\uFDD0${store.key}([VTIBC])\uFDD2([^\uFDD0-\uFDD3]*)\uFDD2([^\uFDD0-\uFDD3]*)\uFDD1`, "g");
|
|
38
|
+
/** Source text with no character the markers use. */
|
|
39
|
+
export const withoutMarkerChars = (text) => text.replace(PRIVATE, "");
|
|
40
|
+
/** Whether text holds one of the characters the markers use. */
|
|
41
|
+
export const hasMarkerChars = (text) => /[\uFDD0-\uFDD3]/.test(text);
|
|
42
|
+
const mark = (store, kind, intent, baked) => `${OPEN}${store.key}${kind}${SEP}${withoutMarkerChars(intent)}${SEP}${withoutMarkerChars(baked)}${CLOSE}`;
|
|
43
|
+
/** A site attribute name a page can write as `{{ name }}`. */
|
|
44
|
+
export const isAttributeName = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
|
|
45
|
+
/**
|
|
46
|
+
* A site param as its attribute: `{{ name }}`, with the prefix the attribute dropped. `bare`
|
|
47
|
+
* leaves the prefix out (kubernetes' `skew`, which prints the number). `value` is the param's
|
|
48
|
+
* value as the site config has it.
|
|
49
|
+
*/
|
|
50
|
+
export function attributeRef(intent, name, value, bare = false) {
|
|
51
|
+
const { store } = intent;
|
|
52
|
+
if (!store.attributes.has(name))
|
|
53
|
+
store.attributes.set(name, value.slice((store.prefixes.get(name) ?? "").length));
|
|
54
|
+
const prefix = store.prefixes.get(name) ?? "";
|
|
55
|
+
const number = store.attributes.get(name);
|
|
56
|
+
return bare ? mark(store, "V", `{{ ${name} }}`, number) : mark(store, "V", `${prefix}{{ ${name} }}`, prefix + number);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A reader choice's value (plans/reader-choices.md): `:choice-value[…]{site="key" …}` as written
|
|
60
|
+
* in prose (in code, the plain form, and the code marked `{choices}`), with the default's value.
|
|
61
|
+
* The caller answers for the text: the choice and key are checked names.
|
|
62
|
+
*/
|
|
63
|
+
export function choiceValueRef(intent, text, baked) {
|
|
64
|
+
return mark(intent.store, "C", text, baked);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A value only a template page computes, by name, with the value it has today. `print` is what
|
|
68
|
+
* the page says where the value is used (default: the name); it must use the name as a word.
|
|
69
|
+
*/
|
|
70
|
+
export function expressionRef(intent, name, expression, baked, print = name) {
|
|
71
|
+
(intent.definitions ??= new Map()).set(name, expression);
|
|
72
|
+
return mark(intent.store, "T", `{{ ${print} }}`, baked);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Template text only a template page runs (a loop over a data file), with what it gives today.
|
|
76
|
+
* It may use names set with `define`. The caller answers for the text: nothing of the site's
|
|
77
|
+
* goes into it unchecked.
|
|
78
|
+
*/
|
|
79
|
+
export function templateRef(intent, template, baked) {
|
|
80
|
+
return mark(intent.store, "T", template, baked);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* As `templateRef`, for lines of template text that stand as a block of their own (a loop that
|
|
84
|
+
* writes a table's rows). Markdown would read those lines as its own (a statement beside a
|
|
85
|
+
* table row becomes a row), so the block is named in `ResolvedIntent.blocks` for whoever reads
|
|
86
|
+
* the page as Markdown to carry it across untouched (`liftTemplateBlocks`).
|
|
87
|
+
*/
|
|
88
|
+
export function templateBlock(intent, template, baked) {
|
|
89
|
+
return mark(intent.store, "B", template, baked);
|
|
90
|
+
}
|
|
91
|
+
/** A value a template page sets at its top, for `templateRef` text to use by name. */
|
|
92
|
+
export function define(intent, name, expression) {
|
|
93
|
+
(intent.definitions ??= new Map()).set(name, expression);
|
|
94
|
+
}
|
|
95
|
+
/** The `{% set %}` lines a template page starts with, for the computed values it kept. */
|
|
96
|
+
export function definitionLines(intent, names) {
|
|
97
|
+
return [...new Set(names)]
|
|
98
|
+
.filter((name) => intent.definitions?.has(name))
|
|
99
|
+
.sort()
|
|
100
|
+
.map((name) => `{% set ${name} = ${intent.definitions.get(name)} %}`)
|
|
101
|
+
.join("\n");
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* An include of a shared file, with the text it stands for. `outPath` is where the file is
|
|
105
|
+
* written in the content folder; the statement names it relative to the page, as includes
|
|
106
|
+
* resolve. `trim` writes `-%}`, so the file's last newline is not a blank line in a code block.
|
|
107
|
+
*/
|
|
108
|
+
export function includeRef(intent, outPath, source, kind, baked, trim = false) {
|
|
109
|
+
if (!intent.store.shared.has(outPath))
|
|
110
|
+
intent.store.shared.set(outPath, { source, kind });
|
|
111
|
+
let rel = posix.relative(posix.dirname(intent.pagePath), outPath);
|
|
112
|
+
if (!rel.startsWith("."))
|
|
113
|
+
rel = `./${rel}`;
|
|
114
|
+
// a path is site data: it must not close the string or the tag it is written in
|
|
115
|
+
if (/["%{}\n\\]/.test(rel))
|
|
116
|
+
return baked;
|
|
117
|
+
return mark(intent.store, "I", `{% include "${rel}" ${trim ? "-" : ""}%}`, baked);
|
|
118
|
+
}
|
|
119
|
+
/** Text with every marker replaced by its value: for content that is itself copied into a page. */
|
|
120
|
+
export const bakeIntent = (text, store) => text.replace(markerOf(store), (_m, _k, _intent, baked) => baked);
|
|
121
|
+
/** Template syntax a page's own text shows: it would run if the text were read as a template. */
|
|
122
|
+
const TEMPLATE_TEXT = /\{\{|\{%|\{#[\s\S]*?#\}/;
|
|
123
|
+
/**
|
|
124
|
+
* Whether markdown shows template syntax outside code. A shared file that does cannot be
|
|
125
|
+
* included: on a template page its text would run, and a page that includes it could not be
|
|
126
|
+
* told apart from one that does not.
|
|
127
|
+
*/
|
|
128
|
+
export function showsTemplateText(markdown) {
|
|
129
|
+
let prose = "";
|
|
130
|
+
let cursor = 0;
|
|
131
|
+
for (const [start, end] of literalRanges(markdown).sort((a, b) => a[0] - b[0])) {
|
|
132
|
+
prose += markdown.slice(cursor, start);
|
|
133
|
+
cursor = Math.max(cursor, end);
|
|
134
|
+
}
|
|
135
|
+
return TEMPLATE_TEXT.test(prose + markdown.slice(cursor));
|
|
136
|
+
}
|
|
137
|
+
/** Pick, for every marker of a page, the intent form or the value. */
|
|
138
|
+
export function resolveIntent(marked, allowTemplate, store, whyNotTemplate) {
|
|
139
|
+
const markers = [];
|
|
140
|
+
let skeleton = "";
|
|
141
|
+
let last = 0;
|
|
142
|
+
// Text between the import's own marks is the site's: a mark character in it is not a mark.
|
|
143
|
+
for (const m of marked.matchAll(markerOf(store))) {
|
|
144
|
+
skeleton += withoutMarkerChars(marked.slice(last, m.index));
|
|
145
|
+
markers.push({ kind: m[1], intent: m[2], baked: m[3], at: skeleton.length, raw: m[0] });
|
|
146
|
+
skeleton += SLOT;
|
|
147
|
+
last = m.index + m[0].length;
|
|
148
|
+
}
|
|
149
|
+
skeleton += withoutMarkerChars(marked.slice(last));
|
|
150
|
+
if (markers.length === 0)
|
|
151
|
+
return { markdown: skeleton, template: false, computed: [], notes: [], blocks: [] };
|
|
152
|
+
// Code: a block or a span is read as a template only when marked, and then all of it is.
|
|
153
|
+
const kinds = codeRanges(skeleton).sort((a, b) => a.start - b.start);
|
|
154
|
+
const ranges = kinds.map((r) => [r.start, r.end]);
|
|
155
|
+
const rangeOf = (at) => {
|
|
156
|
+
let lo = 0;
|
|
157
|
+
let hi = ranges.length - 1;
|
|
158
|
+
while (lo <= hi) {
|
|
159
|
+
const mid = (lo + hi) >> 1;
|
|
160
|
+
if (at < ranges[mid][0])
|
|
161
|
+
hi = mid - 1;
|
|
162
|
+
else if (at >= ranges[mid][1])
|
|
163
|
+
lo = mid + 1;
|
|
164
|
+
else
|
|
165
|
+
return mid;
|
|
166
|
+
}
|
|
167
|
+
return -1;
|
|
168
|
+
};
|
|
169
|
+
const inRange = markers.map((m) => rangeOf(m.at));
|
|
170
|
+
const codeHasOther = new Map();
|
|
171
|
+
for (const r of new Set(inRange)) {
|
|
172
|
+
if (r >= 0)
|
|
173
|
+
codeHasOther.set(r, TEMPLATE_TEXT.test(skeleton.slice(ranges[r][0], ranges[r][1])));
|
|
174
|
+
}
|
|
175
|
+
// Prose: outside every code range.
|
|
176
|
+
let prose = "";
|
|
177
|
+
let cursor = 0;
|
|
178
|
+
for (const [start, end] of ranges) {
|
|
179
|
+
prose += skeleton.slice(cursor, start);
|
|
180
|
+
cursor = Math.max(cursor, end);
|
|
181
|
+
}
|
|
182
|
+
prose += skeleton.slice(cursor);
|
|
183
|
+
const proseHasOther = TEMPLATE_TEXT.test(prose);
|
|
184
|
+
const isFence = (r) => r >= 0 && kinds[r].fence;
|
|
185
|
+
// Where each line starts, found once: looking back for the line break from each marker read a
|
|
186
|
+
// long line again for every marker on it (20,000 on one line took seconds).
|
|
187
|
+
const lineStarts = [0];
|
|
188
|
+
for (let i = skeleton.indexOf("\n"); i !== -1; i = skeleton.indexOf("\n", i + 1))
|
|
189
|
+
lineStarts.push(i + 1);
|
|
190
|
+
const lineIndexOf = (at) => {
|
|
191
|
+
let lo = 0;
|
|
192
|
+
let hi = lineStarts.length - 1;
|
|
193
|
+
while (lo < hi) {
|
|
194
|
+
const mid = (lo + hi + 1) >> 1;
|
|
195
|
+
if (lineStarts[mid] <= at)
|
|
196
|
+
lo = mid;
|
|
197
|
+
else
|
|
198
|
+
hi = mid - 1;
|
|
199
|
+
}
|
|
200
|
+
return lo;
|
|
201
|
+
};
|
|
202
|
+
const unevenLines = new Map();
|
|
203
|
+
const unevenBackticks = (at) => {
|
|
204
|
+
const line = lineIndexOf(at);
|
|
205
|
+
const known = unevenLines.get(line);
|
|
206
|
+
if (known !== undefined)
|
|
207
|
+
return known;
|
|
208
|
+
const end = line + 1 < lineStarts.length ? lineStarts[line + 1] - 1 : skeleton.length;
|
|
209
|
+
const runs = skeleton.slice(lineStarts[line], end).match(/`+/g) ?? [];
|
|
210
|
+
const uneven = new Set(runs.map((run) => run.length)).size > 1;
|
|
211
|
+
unevenLines.set(line, uneven);
|
|
212
|
+
return uneven;
|
|
213
|
+
};
|
|
214
|
+
// (a choice value marks its code `{choices}`, which runs nothing: template text beside it is no matter)
|
|
215
|
+
const usable = (i) => markers[i].kind === "C" || inRange[i] < 0 || !codeHasOther.get(inRange[i]);
|
|
216
|
+
const template = allowTemplate && !proseHasOther &&
|
|
217
|
+
markers.some((m, i) => (m.kind === "T" || m.kind === "B") && usable(i));
|
|
218
|
+
const notes = [];
|
|
219
|
+
const lineOf = (at) => {
|
|
220
|
+
const start = skeleton.lastIndexOf("\n", at - 1) + 1;
|
|
221
|
+
const endAt = skeleton.indexOf("\n", at);
|
|
222
|
+
return { before: skeleton.slice(start, at), after: skeleton.slice(at + 1, endAt === -1 ? skeleton.length : endAt) };
|
|
223
|
+
};
|
|
224
|
+
const chosen = markers.map((m, i) => {
|
|
225
|
+
const bake = (why) => {
|
|
226
|
+
notes.push(why);
|
|
227
|
+
return { text: m.baked, kept: false };
|
|
228
|
+
};
|
|
229
|
+
// A line whose backtick runs differ in length holds a code span this reading of code may
|
|
230
|
+
// cut differently from the build's (`` a ` b ``): whether the marker is in code is not known.
|
|
231
|
+
if (!isFence(inRange[i]) && unevenBackticks(m.at))
|
|
232
|
+
return bake("a value is written on a line whose code spans use backtick runs of different lengths: where the code starts and ends is read differently by the build");
|
|
233
|
+
if (!usable(i))
|
|
234
|
+
return bake("a value is written into code that also shows template text ({{ … }}): marking the code as a template would run that text");
|
|
235
|
+
if ((m.kind === "T" || m.kind === "B") && !template) {
|
|
236
|
+
return bake(allowTemplate
|
|
237
|
+
? "a computed value is written as today's value: the page's text shows template syntax, so it cannot be a template page"
|
|
238
|
+
: whyNotTemplate ?? "a computed value is written as today's value in a shared file: the pages that include it are not all template pages");
|
|
239
|
+
}
|
|
240
|
+
if (m.kind === "B") {
|
|
241
|
+
const { before, after } = lineOf(m.at);
|
|
242
|
+
if (before !== "" || after.trim() !== "" || inRange[i] >= 0)
|
|
243
|
+
return bake("a block computed from data is written as it is today: it does not stand on a line of its own");
|
|
244
|
+
}
|
|
245
|
+
if (m.kind === "I") {
|
|
246
|
+
const { before, after } = lineOf(m.at);
|
|
247
|
+
if (before.trim() !== "" || after.trim() !== "")
|
|
248
|
+
return bake("an include inside a line of text is written as its text: an include stands on a line of its own");
|
|
249
|
+
// On an indented line (a list item, a code block in one) the build indents the included
|
|
250
|
+
// lines with it. After a quote's `>` or a list marker the line is not the include's alone,
|
|
251
|
+
// which the check above has already turned into text.
|
|
252
|
+
}
|
|
253
|
+
return { text: m.intent, kept: true };
|
|
254
|
+
});
|
|
255
|
+
// A value written out is the site's text from then on, and is weighed as the page's own
|
|
256
|
+
// text is: on a template page, template syntax in it would run (a gate's description that
|
|
257
|
+
// says `{{ … }}`), and so would syntax it forms WITH the text beside it (a value ending `{`
|
|
258
|
+
// before a page's `{ 7 * 7 }}`; reviews 2026-11-23). So when the page keeps anything, it is
|
|
259
|
+
// read again with every written-out value as text, and all the checks above see the page
|
|
260
|
+
// as it will be written.
|
|
261
|
+
if (chosen.some((c) => c.kept) && chosen.some((c) => !c.kept)) {
|
|
262
|
+
let n = 0;
|
|
263
|
+
const again = marked.replace(markerOf(store), (raw, _k, _intent, baked) => (chosen[n++].kept ? raw : baked));
|
|
264
|
+
const result = resolveIntent(again, allowTemplate, store, whyNotTemplate);
|
|
265
|
+
return { ...result, notes: [...new Set([...notes, ...result.notes])] };
|
|
266
|
+
}
|
|
267
|
+
// Mark each code range that keeps an intent form: `minja` for template text, `choices` for a
|
|
268
|
+
// reader choice's value (`:choice-value[]{…}`), in ONE group (a second group would be lost).
|
|
269
|
+
const inserts = new Map();
|
|
270
|
+
const names = new Map();
|
|
271
|
+
markers.forEach((m, i) => {
|
|
272
|
+
const r = inRange[i];
|
|
273
|
+
if (r < 0 || !chosen[i].kept)
|
|
274
|
+
return;
|
|
275
|
+
const set = names.get(r) ?? new Set();
|
|
276
|
+
set.add(m.kind === "C" ? "choices" : "minja");
|
|
277
|
+
names.set(r, set);
|
|
278
|
+
});
|
|
279
|
+
for (const [r, set] of names) {
|
|
280
|
+
const [start, end] = ranges[r];
|
|
281
|
+
const wanted = ["minja", "choices"].filter((n) => set.has(n));
|
|
282
|
+
// An attribute group already there (a fence's `{title="…"}`, a span's `{.cls}`) takes the
|
|
283
|
+
// names: a second group is lost (`{minja} {choices}` read as `choices` alone)
|
|
284
|
+
const into = (group, at) => {
|
|
285
|
+
const have = new Set(group.slice(1, -1).split(/\s+/));
|
|
286
|
+
const missing = wanted.filter((n) => !have.has(n));
|
|
287
|
+
if (missing.length)
|
|
288
|
+
inserts.set(at + group.length - 1, `${group.length > 2 ? " " : ""}${missing.join(" ")}`);
|
|
289
|
+
};
|
|
290
|
+
// A fence's first line is its opener, whatever quote or list marker comes before the run.
|
|
291
|
+
if (kinds[r].fence) {
|
|
292
|
+
const lineEnd = skeleton.indexOf("\n", start);
|
|
293
|
+
if (lineEnd === -1)
|
|
294
|
+
continue;
|
|
295
|
+
const info = skeleton.slice(start, lineEnd);
|
|
296
|
+
const last = /\{[^{}\n]*\}\s*$/.exec(info);
|
|
297
|
+
if (last)
|
|
298
|
+
into(last[0].trimEnd(), start + last.index);
|
|
299
|
+
else
|
|
300
|
+
inserts.set(lineEnd, `${/[`~]$/.test(info.trimEnd()) ? "" : " "}{${wanted.join(" ")}}`);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
const after = /^\{[^{}\n]*\}/.exec(skeleton.slice(end, end + 400));
|
|
304
|
+
if (after)
|
|
305
|
+
into(after[0], end);
|
|
306
|
+
else
|
|
307
|
+
inserts.set(end, `{${wanted.join(" ")}}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// In code, a value is the plain form (`code` and `link` are prose forms; a label may hold `\]`)
|
|
311
|
+
const inCodeForm = (text) => text.replace(/^:choice-value\[(?:\\.|[^\]\\\n])*\]\{([a-z][a-z0-9-]*="[^"\n]*")[^}\n]*\}$/, ":choice-value[]{$1}");
|
|
312
|
+
markers.forEach((m, i) => {
|
|
313
|
+
if (m.kind === "C" && chosen[i].kept && inRange[i] >= 0)
|
|
314
|
+
chosen[i] = { text: inCodeForm(chosen[i].text), kept: true };
|
|
315
|
+
});
|
|
316
|
+
let out = "";
|
|
317
|
+
let next = 0;
|
|
318
|
+
for (let i = 0; i <= skeleton.length; i++) {
|
|
319
|
+
const insert = inserts.get(i);
|
|
320
|
+
if (insert)
|
|
321
|
+
out += insert;
|
|
322
|
+
if (i === skeleton.length)
|
|
323
|
+
break;
|
|
324
|
+
const ch = skeleton[i];
|
|
325
|
+
if (ch === SLOT && next < markers.length && markers[next].at === i)
|
|
326
|
+
out += chosen[next++].text;
|
|
327
|
+
else
|
|
328
|
+
out += ch;
|
|
329
|
+
}
|
|
330
|
+
const computed = markers
|
|
331
|
+
.filter((m, i) => (m.kind === "T" || m.kind === "B") && chosen[i].kept)
|
|
332
|
+
// every word of what the page prints, outside its string literals: the names among them
|
|
333
|
+
// are the ones `definitionLines` knows
|
|
334
|
+
.flatMap((m) => m.intent.replace(/"(?:[^"\\]|\\.)*"/g, "").match(/[A-Za-z_]\w*/g) ?? []);
|
|
335
|
+
const blocks = markers.filter((m, i) => m.kind === "B" && chosen[i].kept).map((m) => m.intent);
|
|
336
|
+
return { markdown: out, template, computed, notes: [...new Set(notes)], blocks };
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* A page's blocks of template text, each replaced by a word on a line of its own, so the page
|
|
340
|
+
* can be read as Markdown: `restore` gives a word's block back. The words carry `key` (the
|
|
341
|
+
* import's own), which no text of the site's can hold.
|
|
342
|
+
*/
|
|
343
|
+
export function liftTemplateBlocks(markdown, blocks, key) {
|
|
344
|
+
const restore = new Map();
|
|
345
|
+
let out = markdown;
|
|
346
|
+
let from = 0;
|
|
347
|
+
for (const block of blocks) {
|
|
348
|
+
const at = out.indexOf(block, from);
|
|
349
|
+
if (at === -1)
|
|
350
|
+
continue;
|
|
351
|
+
const word = `templateblock${key}n${restore.size}`;
|
|
352
|
+
restore.set(word, block);
|
|
353
|
+
out = `${out.slice(0, at)}\n\n${word}\n\n${out.slice(at + block.length)}`;
|
|
354
|
+
from = at + word.length + 4;
|
|
355
|
+
}
|
|
356
|
+
return { markdown: out, restore };
|
|
357
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown link references in a text, read loosely and in one pass.
|
|
3
|
+
*
|
|
4
|
+
* Hugo puts a `{{% name %}}` shortcode's output into the page BEFORE the page's Markdown is
|
|
5
|
+
* read, so a reference in the shortcode's text and a definition in the page (or the other way
|
|
6
|
+
* round) meet in one document. A text that is to be a shared file, read alone, must not depend
|
|
7
|
+
* on that. This finds what a text defines and uses so the two can be compared.
|
|
8
|
+
*
|
|
9
|
+
* Loose on purpose: a bracket in a code block counts as a use. A wrong "uses" only means a text
|
|
10
|
+
* is copied where it could have been included; a missed one loses a link.
|
|
11
|
+
*/
|
|
12
|
+
export interface LinkReferences {
|
|
13
|
+
/** Label → what it is defined as (the rest of its line, trimmed). The first definition wins. */
|
|
14
|
+
defined: Map<string, string>;
|
|
15
|
+
/** Labels used as `[text][label]` or `[label][]`. */
|
|
16
|
+
used: Set<string>;
|
|
17
|
+
/** Text in brackets on its own, `[label]`: a link only when something defines the label. */
|
|
18
|
+
shortcut: Set<string>;
|
|
19
|
+
}
|
|
20
|
+
/** Each line is read once, left to right: the cost is the length of the text. */
|
|
21
|
+
export declare function linkReferences(text: string): LinkReferences;
|
|
22
|
+
/**
|
|
23
|
+
* Why a text cannot be read alone, or undefined when it can. `others` are the texts it shares
|
|
24
|
+
* a document with in Hugo: the page, and the other shortcode files the page puts in. It cannot
|
|
25
|
+
* when it uses a reference it does not define, when another text defines one of its labels
|
|
26
|
+
* differently (Hugo then uses the first definition for all of them), or when another text uses
|
|
27
|
+
* a label only it defines.
|
|
28
|
+
*/
|
|
29
|
+
export declare function dependsOnPage(text: LinkReferences, others: readonly LinkReferences[]): string | undefined;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown link references in a text, read loosely and in one pass.
|
|
3
|
+
*
|
|
4
|
+
* Hugo puts a `{{% name %}}` shortcode's output into the page BEFORE the page's Markdown is
|
|
5
|
+
* read, so a reference in the shortcode's text and a definition in the page (or the other way
|
|
6
|
+
* round) meet in one document. A text that is to be a shared file, read alone, must not depend
|
|
7
|
+
* on that. This finds what a text defines and uses so the two can be compared.
|
|
8
|
+
*
|
|
9
|
+
* Loose on purpose: a bracket in a code block counts as a use. A wrong "uses" only means a text
|
|
10
|
+
* is copied where it could have been included; a missed one loses a link.
|
|
11
|
+
*/
|
|
12
|
+
/** A label as Markdown matches it: case and runs of space do not count. */
|
|
13
|
+
const normal = (label) => label.replace(/\s+/g, " ").trim().toLowerCase();
|
|
14
|
+
/** Each line is read once, left to right: the cost is the length of the text. */
|
|
15
|
+
export function linkReferences(text) {
|
|
16
|
+
const defined = new Map();
|
|
17
|
+
const used = new Set();
|
|
18
|
+
const shortcut = new Set();
|
|
19
|
+
for (const line of text.split("\n")) {
|
|
20
|
+
if (!line.includes("["))
|
|
21
|
+
continue;
|
|
22
|
+
// bracket groups of the line, innermost only: a label holds no `[`
|
|
23
|
+
const groups = [];
|
|
24
|
+
let open = -1;
|
|
25
|
+
for (let i = 0; i < line.length; i++) {
|
|
26
|
+
const c = line.charCodeAt(i);
|
|
27
|
+
if (c === 92 /* \ */)
|
|
28
|
+
i++;
|
|
29
|
+
else if (c === 91 /* [ */)
|
|
30
|
+
open = i;
|
|
31
|
+
else if (c === 93 /* ] */ && open >= 0) {
|
|
32
|
+
groups.push({ start: open, end: i });
|
|
33
|
+
open = -1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (let g = 0; g < groups.length; g++) {
|
|
37
|
+
const { start, end } = groups[g];
|
|
38
|
+
const label = line.slice(start + 1, end);
|
|
39
|
+
const after = line[end + 1];
|
|
40
|
+
// `[label]: target` at the start of a line (up to three spaces in)
|
|
41
|
+
if (g === 0 && after === ":" && start <= 3 && line.slice(0, start).trim() === "") {
|
|
42
|
+
const key = normal(label);
|
|
43
|
+
if (key && !defined.has(key))
|
|
44
|
+
defined.set(key, line.slice(end + 2).trim());
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const next = groups[g + 1];
|
|
48
|
+
if (next && next.start === end + 1) {
|
|
49
|
+
// `[text][label]`, or `[label][]`
|
|
50
|
+
used.add(normal(line.slice(next.start + 1, next.end) || label));
|
|
51
|
+
g++;
|
|
52
|
+
}
|
|
53
|
+
else if (after !== "(" && label.trim() !== "") {
|
|
54
|
+
// `[label]` alone is a link only when something defines it
|
|
55
|
+
shortcut.add(normal(label));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
used.delete("");
|
|
60
|
+
shortcut.delete("");
|
|
61
|
+
return { defined, used, shortcut };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Why a text cannot be read alone, or undefined when it can. `others` are the texts it shares
|
|
65
|
+
* a document with in Hugo: the page, and the other shortcode files the page puts in. It cannot
|
|
66
|
+
* when it uses a reference it does not define, when another text defines one of its labels
|
|
67
|
+
* differently (Hugo then uses the first definition for all of them), or when another text uses
|
|
68
|
+
* a label only it defines.
|
|
69
|
+
*/
|
|
70
|
+
export function dependsOnPage(text, others) {
|
|
71
|
+
for (const label of text.used) {
|
|
72
|
+
if (!text.defined.has(label))
|
|
73
|
+
return "it uses link references the page defines";
|
|
74
|
+
}
|
|
75
|
+
for (const label of text.shortcut) {
|
|
76
|
+
if (!text.defined.has(label) && others.some((o) => o !== text && o.defined.has(label)))
|
|
77
|
+
return "it uses link references the page defines";
|
|
78
|
+
}
|
|
79
|
+
for (const [label, target] of text.defined) {
|
|
80
|
+
for (const other of others) {
|
|
81
|
+
if (other === text)
|
|
82
|
+
continue;
|
|
83
|
+
const theirs = other.defined.get(label);
|
|
84
|
+
if (theirs !== undefined && theirs !== target)
|
|
85
|
+
return "it and the page, or another shortcode on the page, define the same link reference differently";
|
|
86
|
+
if (theirs === undefined && (other.used.has(label) || other.shortcut.has(label)))
|
|
87
|
+
return "the page uses a link reference it defines";
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|