@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.
@@ -4,19 +4,29 @@
4
4
  * Hugo has no fixed shortcode vocabulary: a site defines its own in
5
5
  * `layouts/shortcodes/`, and a theme ships more. Kubernetes uses 52 distinct
6
6
  * names, of which only a handful are Hugo builtins. So the registry is OPEN,
7
- * in three layers — builtins < theme (docsy) < site (kubernetes) < user —
7
+ * in layers — Hugo's own < adapters (conventions < a theme < a site) < user —
8
8
  * exactly like the MDX adapters, because a site-specific shortcode must never
9
- * become a core concern.
9
+ * become a core concern. An adapter (`HugoSiteAdapter`, `adapters/`) is what a
10
+ * theme or one site defines; it is used when the site is found to match it.
10
11
  *
11
12
  * A shortcode with no mapping is NEVER dropped: it is preserved verbatim and
12
13
  * recorded in the fidelity ledger, so an unmapped construct is a known loss
13
14
  * rather than a silent one.
14
15
  */
16
+ import type { HugoIntent, HugoIntentStore } from "./intent.js";
17
+ import type { LocatedContent } from "./resolve.js";
15
18
  import type { ShortcodeArgs, ShortcodeNode } from "./shortcodes.js";
16
19
  import type { HugoConfig } from "./config.js";
20
+ import type { LinkReferences } from "./references.js";
21
+ import type { HtmlBlockMapping } from "./html-blocks.js";
17
22
  export interface ShortcodeContext {
18
23
  /** Translate a Hugo source fragment (a shortcode body) to Dogsbay-MD. */
19
24
  translate: (source: string) => string;
25
+ /**
26
+ * As `translate`, for text a mapping reads as DATA for the whole import (a glossary term's
27
+ * page): translated as a text of its own, whatever depth the asking shortcode is at.
28
+ */
29
+ translateData?: (source: string) => string;
20
30
  /** Site config — params, baseURL, languages. */
21
31
  config: HugoConfig;
22
32
  /** Record a fidelity note. */
@@ -32,7 +42,59 @@ export interface ShortcodeContext {
32
42
  readContentFile?: (path: string, fromFile?: string) => string | undefined;
33
43
  /** Source file currently being translated, relative to the content root. */
34
44
  file?: string;
45
+ /** The site's translated strings for the language being imported (`loadHugoI18n`). */
46
+ i18n?: Record<string, string>;
47
+ /**
48
+ * Set when the import keeps the source's intent (a migration to a Dogsbay site): a mapping
49
+ * then writes an attribute, an include or an expression with `attributeRef` / `includeRef` /
50
+ * `expressionRef` (intent.ts), not the value. Unset: the value, as Hugo prints it.
51
+ */
52
+ intent?: HugoIntent;
53
+ /** As `readContentFile`, and it says which file was read. */
54
+ locateContentFile?: (path: string, fromFile?: string) => LocatedContent | undefined;
55
+ /**
56
+ * The Markdown files of one folder of the site's DEFAULT language (a shortcode that reads a
57
+ * folder of pages as data, as kubernetes' `feature-state` reads the feature gates' pages:
58
+ * Hugo reads them from the English site whatever language is built). Not recursive.
59
+ */
60
+ listContentDir?: (dir: string) => Array<{
61
+ name: string;
62
+ text: string;
63
+ }>;
64
+ /**
65
+ * A file of the site's `data/` folder, parsed (Hugo's `.Site.Data`): `"releases/schedule"` is
66
+ * `data/releases/schedule.yaml` (or `.yml`, `.json`, `.toml`). Undefined when there is none
67
+ * or it cannot be read.
68
+ */
69
+ readSiteData?: (name: string) => unknown;
70
+ /**
71
+ * Where a shared file is written in the content folder (`_includes/…`, `_examples/…`;
72
+ * a shortcode's own file, `_includes/shortcodes/…`).
73
+ */
74
+ sharedPath?: (located: LocatedContent, kind: SharedKind) => string | undefined;
75
+ /**
76
+ * The file of a shortcode the site defines as MARKDOWN (`layouts/shortcodes/intro.md`, or
77
+ * `intro.en.md` for one language): the language being imported, then no language, then the
78
+ * default language. Undefined for any other shortcode, and for a file that cannot be read.
79
+ */
80
+ shortcodeFile?: (name: string) => LocatedContent | undefined;
81
+ /**
82
+ * The link references of the whole text being translated (the page, or a shared file), for
83
+ * a mapping that puts Markdown into it which must not depend on them. Worked out once.
84
+ */
85
+ pageReferences?: () => LinkReferences;
86
+ /** The whole text being translated (the page's body, or a shared file's), as the site wrote it. */
87
+ pageSource?: string;
88
+ /** The front matter of the page being translated (Hugo's `.Page.Params`); unset in a shared file. */
89
+ pageParams?: Record<string, unknown>;
90
+ /**
91
+ * Set for each call: whether it stands on a line of its own (only spaces before it on its line
92
+ * and after it on its last), so a mapping can give a block, or a span inside a line.
93
+ */
94
+ alone?: () => boolean;
35
95
  }
96
+ /** What a shared file is: a content file pages include, an example, or a shortcode's own file. */
97
+ export type SharedKind = "include" | "example" | "shortcode";
36
98
  export interface HugoLedgerEntry {
37
99
  /** Shortcode name. */
38
100
  shortcode: string;
@@ -58,6 +120,58 @@ export interface HugoLedgerEntry {
58
120
  */
59
121
  export type ShortcodeMapping = (node: ShortcodeNode, ctx: ShortcodeContext) => string | null | undefined;
60
122
  export type ShortcodeRegistry = Record<string, ShortcodeMapping>;
123
+ /**
124
+ * What is known of a Hugo site before its pages are read: names only, never file contents.
125
+ * Enough to tell which theme it uses and which shortcodes it defines itself.
126
+ */
127
+ export interface HugoSiteFacts {
128
+ /** `theme` in the config, in order. */
129
+ themes: string[];
130
+ /** Hugo module imports (`module.imports[].path`), e.g. `github.com/google/docsy`. */
131
+ modules: string[];
132
+ /**
133
+ * Shortcodes the site defines in its own `layouts/shortcodes/` (and `layouts/_shortcodes/`):
134
+ * the file's name without its extension and language (`skew`, `blocks/cover`).
135
+ */
136
+ shortcodes: ReadonlySet<string>;
137
+ params: Record<string, unknown>;
138
+ }
139
+ /**
140
+ * The shortcodes of one theme or one site, and how to tell that a site uses them.
141
+ *
142
+ * Adapters are layered in the order given (a later one wins a name), above Hugo's own
143
+ * shortcodes and below the caller's.
144
+ */
145
+ export interface HugoSiteAdapter {
146
+ /** Short and stable: what `--adapters` names (`docsy`, `kubernetes`). */
147
+ name: string;
148
+ /** One line, for the report and `--help`. */
149
+ description: string;
150
+ /** Why the site matches, in a few words for the report; undefined when it does not. */
151
+ detect: (site: HugoSiteFacts) => string | undefined;
152
+ shortcodes: ShortcodeRegistry;
153
+ /**
154
+ * Shortcodes that print a VALUE (a version), filled in inside code too. Hugo runs
155
+ * shortcodes before Markdown, so `kubectl v{{< skew currentVersion >}}` in a code block is
156
+ * a call; every other shortcode found in code is left as written.
157
+ */
158
+ valueShortcodes?: string[];
159
+ /**
160
+ * For a shortcode NO adapter maps by name: a rule that reads what the shortcode is (its
161
+ * file), as a site with hundreds of shortcodes that are Markdown files needs. A later
162
+ * adapter's is tried first; `undefined` passes to the next, and then the call is kept as
163
+ * written and reported, as any call with no mapping is.
164
+ */
165
+ fallback?: ShortcodeMapping;
166
+ /** Once per translated text, when the import keeps intent: before any shortcode is read. */
167
+ prepare?: (store: HugoIntentStore, params: Record<string, unknown>) => void;
168
+ /**
169
+ * The site's styled HTML boxes as directives (`<div class="alert alert-info">` → `:::info`),
170
+ * so their bodies are Markdown (html-blocks.ts). A box whose body is not plain inline HTML
171
+ * stays as written and is reported.
172
+ */
173
+ htmlBlocks?: HtmlBlockMapping[];
174
+ }
61
175
  /** First positional argument, or a named one, whichever the call used. */
62
176
  export declare function arg(args: ShortcodeArgs, ...names: string[]): string | undefined;
63
177
  /** `{key="value"}` attribute block for a Dogsbay-MD directive, or "". */
package/dist/resolve.d.ts CHANGED
@@ -7,6 +7,23 @@ export interface ContentResolverOptions {
7
7
  * `code_sample` reads from.
8
8
  */
9
9
  searchDirs?: string[];
10
+ /**
11
+ * Absolute directory the calling page's path is relative to, when it is not
12
+ * the content root: an import narrowed to one section (`content/en/docs`)
13
+ * still reads `includes/` and `examples/` from the language root beside it.
14
+ * Reading them from the section root lost every include and code sample of
15
+ * a `--section docs` import (363 and 136 on kubernetes/website).
16
+ */
17
+ pageRoot?: string;
18
+ /**
19
+ * Absolute content root of the site's default language. A path under one of
20
+ * `fallbackDirs` that the language does not have is read from here, as the
21
+ * kubernetes `code_sample` shortcode does for a translation with no copy of
22
+ * an example. Includes do not fall back: Hugo fails the build instead.
23
+ */
24
+ fallbackRoot?: string;
25
+ /** First path segments that may be read from `fallbackRoot`. Default: `examples`. */
26
+ fallbackDirs?: string[];
10
27
  }
11
28
  /**
12
29
  * Build a reader that takes a shortcode's file argument and the page it was
@@ -14,6 +31,16 @@ export interface ContentResolverOptions {
14
31
  * records an unresolved reference rather than emitting nothing silently.
15
32
  */
16
33
  export declare function makeContentResolver(opts: ContentResolverOptions): (path: string, fromFile?: string) => string | undefined;
34
+ /** A file a shortcode names: where it is, its text, and whether it came from the default language. */
35
+ export interface LocatedContent {
36
+ /** Absolute path of the file. */
37
+ path: string;
38
+ text: string;
39
+ /** Read from `fallbackRoot`: the language has no copy. */
40
+ fallback: boolean;
41
+ }
42
+ /** As `makeContentResolver`, and it says WHICH file was read (a shared file is written once). */
43
+ export declare function makeContentLocator(opts: ContentResolverOptions): (path: string, fromFile?: string) => LocatedContent | undefined;
17
44
  /**
18
45
  * Whether a shortcode's `include=` argument names a FILE.
19
46
  *
package/dist/resolve.js CHANGED
@@ -21,7 +21,16 @@ import { dirname, isAbsolute, resolve, sep } from "node:path";
21
21
  * records an unresolved reference rather than emitting nothing silently.
22
22
  */
23
23
  export function makeContentResolver(opts) {
24
+ const locate = makeContentLocator(opts);
25
+ return (path, fromFile) => locate(path, fromFile)?.text;
26
+ }
27
+ /** As `makeContentResolver`, and it says WHICH file was read (a shared file is written once). */
28
+ export function makeContentLocator(opts) {
24
29
  const { contentRoot } = opts;
30
+ const pageRoot = opts.pageRoot ?? contentRoot;
31
+ const fallbackRoot = opts.fallbackRoot && opts.fallbackRoot !== contentRoot ? opts.fallbackRoot : undefined;
32
+ const fallbackDirs = opts.fallbackDirs ?? ["examples"];
33
+ const inside = (candidate, root) => candidate !== root && candidate.startsWith(root + sep);
25
34
  const searchDirs = opts.searchDirs ?? ["includes", "examples", "static", "."];
26
35
  return (path, fromFile) => {
27
36
  if (!path)
@@ -39,15 +48,18 @@ export function makeContentResolver(opts) {
39
48
  if (fromFile) {
40
49
  const pageDir = isAbsolute(fromFile)
41
50
  ? dirname(fromFile)
42
- : resolve(contentRoot, dirname(fromFile));
51
+ : resolve(pageRoot, dirname(fromFile));
43
52
  candidates.push(resolve(pageDir, clean));
44
53
  }
45
- // Then the site conventions.
46
- for (const dir of searchDirs) {
47
- candidates.push(resolve(contentRoot, dir, clean.replace(/^\//, "")));
54
+ // Then the site conventions: the imported section's own first, then the language's.
55
+ const roots = pageRoot === contentRoot ? [contentRoot] : [pageRoot, contentRoot];
56
+ for (const root of roots) {
57
+ for (const dir of searchDirs)
58
+ candidates.push(resolve(root, dir, clean.replace(/^\//, "")));
48
59
  }
49
60
  // Finally, root-relative.
50
- candidates.push(resolve(contentRoot, clean.replace(/^\//, "")));
61
+ for (const root of roots)
62
+ candidates.push(resolve(root, clean.replace(/^\//, "")));
51
63
  for (const candidate of candidates) {
52
64
  // Never escape the content root: a shortcode argument is site data, and
53
65
  // `../../etc/passwd` must not be readable through it.
@@ -57,13 +69,27 @@ export function makeContentResolver(opts) {
57
69
  continue;
58
70
  try {
59
71
  if (existsSync(candidate) && statSync(candidate).isFile()) {
60
- return readFileSync(candidate, "utf-8");
72
+ return { path: candidate, text: readFileSync(candidate, "utf-8"), fallback: false };
61
73
  }
62
74
  }
63
75
  catch {
64
76
  // Unreadable — keep trying the remaining candidates.
65
77
  }
66
78
  }
79
+ // The default language's copy, for the directories that fall back to it.
80
+ if (fallbackRoot && fallbackDirs.includes(clean.replace(/^\//, "").split(/[\\/]/)[0])) {
81
+ const candidate = resolve(fallbackRoot, clean.replace(/^\//, ""));
82
+ if (inside(candidate, fallbackRoot)) {
83
+ try {
84
+ if (existsSync(candidate) && statSync(candidate).isFile()) {
85
+ return { path: candidate, text: readFileSync(candidate, "utf-8"), fallback: true };
86
+ }
87
+ }
88
+ catch {
89
+ // unreadable: not found
90
+ }
91
+ }
92
+ }
67
93
  return undefined;
68
94
  };
69
95
  }
@@ -1,23 +1,3 @@
1
- /**
2
- * Hugo shortcode scanner.
3
- *
4
- * Hugo content is markdown PLUS a template language, and the template has two
5
- * delimiter forms with different evaluation semantics:
6
- *
7
- * {{< sc >}} output inserted RAW — not markdown-processed
8
- * {{% sc %}} inner content IS markdown-processed
9
- * {{< sc >}}…{{< /sc >}} paired (has a body)
10
- * {{< sc "pos" k="v" >}} positional AND named arguments, in one call
11
- *
12
- * A regex sweep cannot read this. Shortcodes nest (`tabs` contains `tab`
13
- * contains `note`), the two delimiter forms mix freely INSIDE one another
14
- * (k8s writes `{{< tabs >}}{{% tab %}}`), and a shortcode inside a fenced code
15
- * block is literal text that must survive byte-exact — the Hugo docs
16
- * themselves are full of those.
17
- *
18
- * So this is a scanner: it walks the source once, tracks fences, and returns
19
- * spans with their nesting resolved.
20
- */
21
1
  export type ShortcodeDelimiter = "angle" | "percent";
22
2
  export interface ShortcodeArgs {
23
3
  /** Positional arguments, in order: `{{< youtube dQw4w9WgXcQ >}}`. */
@@ -38,12 +18,32 @@ export interface ShortcodeNode {
38
18
  selfClosing: boolean;
39
19
  /** 1-based line of the opening tag. */
40
20
  line: number;
21
+ /** A value shortcode found inside code: filled in only when it has a value. */
22
+ inCode?: boolean;
41
23
  }
42
24
  export interface TextNode {
43
25
  kind: "text";
44
26
  text: string;
45
27
  }
46
28
  export type ScanNode = ShortcodeNode | TextNode;
29
+ /** Text as Hugo prints it: the comment markers of a shown shortcode removed. */
30
+ export declare function unescapeShownShortcodes(text: string): string;
31
+ /**
32
+ * Byte ranges that are LITERAL: fenced code blocks and inline code spans.
33
+ * A shortcode inside them is documentation about shortcodes, not a call.
34
+ *
35
+ * Fences are found with the build's own grammar (`createFenceTracker`), so a code block here is a
36
+ * code block there: one inside a quote or on a list-marker line included. With a grammar of its
37
+ * own, the import did not mark a quoted code block that the build then read as code, and the
38
+ * attribute in it was shown as written (2026-11-22).
39
+ */
40
+ export declare function literalRanges(source: string): Array<[number, number]>;
41
+ /** The same ranges, each with what it is: a fenced block (opener line first) or an inline span. */
42
+ export declare function codeRanges(source: string): Array<{
43
+ start: number;
44
+ end: number;
45
+ fence: boolean;
46
+ }>;
47
47
  /** Parse the argument string of a shortcode call. */
48
48
  export declare function parseArgs(text: string): ShortcodeArgs;
49
49
  /**
@@ -51,7 +51,7 @@ export declare function parseArgs(text: string): ShortcodeArgs;
51
51
  * A paired shortcode's body stays in `inner` for the caller to scan
52
52
  * recursively — that is what makes nesting work without a second parser.
53
53
  */
54
- export declare function scanShortcodes(source: string): ScanNode[];
54
+ export declare function scanShortcodes(source: string, inCode?: ReadonlySet<string>): ScanNode[];
55
55
  /**
56
56
  * Hugo's content-type body markers.
57
57
  *
@@ -18,44 +18,63 @@
18
18
  * So this is a scanner: it walks the source once, tracks fences, and returns
19
19
  * spans with their nesting resolved.
20
20
  */
21
+ import { createFenceTracker } from "@dogsbay/format-dogsbay-md/browser";
22
+ // Hugo's way to SHOW a shortcode: a tag written with comment markers inside its delimiters
23
+ // (open delimiter, slash, star … star, slash, close delimiter) prints the tag without them.
24
+ // The comment may run over several tags, to the next closing marker (kubernetes escapes a
25
+ // whole example with one pair). Nothing inside it is a call.
26
+ const SHOWN_SHORTCODE = /\{\{<\/\*([\s\S]*?)\*\/>\}\}|\{\{%\/\*([\s\S]*?)\*\/%\}\}/g;
27
+ /** Text as Hugo prints it: the comment markers of a shown shortcode removed. */
28
+ export function unescapeShownShortcodes(text) {
29
+ return text.replace(SHOWN_SHORTCODE, (m, angle, percent) => angle !== undefined ? `{{<${angle}>}}` : `{{%${percent}%}}`);
30
+ }
21
31
  /**
22
32
  * Byte ranges that are LITERAL: fenced code blocks and inline code spans.
23
33
  * A shortcode inside them is documentation about shortcodes, not a call.
34
+ *
35
+ * Fences are found with the build's own grammar (`createFenceTracker`), so a code block here is a
36
+ * code block there: one inside a quote or on a list-marker line included. With a grammar of its
37
+ * own, the import did not mark a quoted code block that the build then read as code, and the
38
+ * attribute in it was shown as written (2026-11-22).
24
39
  */
25
- function literalRanges(source) {
40
+ export function literalRanges(source) {
41
+ return codeRanges(source).map((r) => [r.start, r.end]);
42
+ }
43
+ /** The same ranges, each with what it is: a fenced block (opener line first) or an inline span. */
44
+ export function codeRanges(source) {
26
45
  const ranges = [];
27
- const lines = source.split("\n");
46
+ const tracker = createFenceTracker();
28
47
  let offset = 0;
29
- let fence = null;
30
- for (const line of lines) {
48
+ let fenceStart = -1;
49
+ for (const line of source.split("\n")) {
31
50
  const lineStart = offset;
32
51
  offset += line.length + 1;
33
- const trimmed = line.trim();
34
- if (!fence) {
35
- const open = /^(`{3,}|~{3,})/.exec(trimmed);
36
- if (open) {
37
- fence = { marker: open[1], start: lineStart };
38
- continue;
52
+ const wasOpen = tracker.open();
53
+ const kind = tracker.step(line);
54
+ // A fence that ended without a closer (its quote ended): it stops before this line.
55
+ if (wasOpen && kind !== "code" && !(kind === "fence" && !tracker.open())) {
56
+ ranges.push({ start: fenceStart, end: lineStart, fence: true });
57
+ fenceStart = -1;
58
+ }
59
+ if (kind === "fence") {
60
+ if (tracker.open())
61
+ fenceStart = lineStart;
62
+ else {
63
+ ranges.push({ start: fenceStart, end: Math.min(offset, source.length), fence: true });
64
+ fenceStart = -1;
39
65
  }
66
+ }
67
+ else if (kind === "prose") {
40
68
  // Inline code spans on a non-fence line.
41
69
  for (const m of line.matchAll(/`+[^`]*`+/g)) {
42
- ranges.push([lineStart + m.index, lineStart + m.index + m[0].length]);
43
- }
44
- }
45
- else {
46
- // CommonMark: the closer is the same character, at least as long, and
47
- // carries no info string.
48
- const close = new RegExp(`^${fence.marker[0]}{${fence.marker.length},}[ \\t]*$`);
49
- if (close.test(trimmed)) {
50
- ranges.push([fence.start, offset]);
51
- fence = null;
70
+ ranges.push({ start: lineStart + m.index, end: lineStart + m.index + m[0].length, fence: false });
52
71
  }
53
72
  }
54
73
  }
55
74
  // An unterminated fence still protects everything to end of file: its
56
75
  // content is code either way, and editing it is the worse failure.
57
- if (fence)
58
- ranges.push([fence.start, source.length]);
76
+ if (fenceStart >= 0)
77
+ ranges.push({ start: fenceStart, end: source.length, fence: true });
59
78
  return ranges;
60
79
  }
61
80
  /** Parse the argument string of a shortcode call. */
@@ -84,9 +103,11 @@ export function parseArgs(text) {
84
103
  return { positional, named };
85
104
  }
86
105
  /** Every shortcode tag in the source, skipping literal ranges. */
87
- function findTags(source) {
106
+ function findTags(source, inCode) {
88
107
  const literals = literalRanges(source);
89
108
  const inLiteral = (i) => literals.some(([s, e]) => i >= s && i < e);
109
+ const shown = [...source.matchAll(SHOWN_SHORTCODE)].map((m) => [m.index, m.index + m[0].length]);
110
+ const inShown = (i) => shown.some(([s, e]) => i >= s && i < e);
90
111
  const tags = [];
91
112
  // `{{<` or `{{%`, optional `/`, name, args, matching close delimiter.
92
113
  // The args run is lazy and skips quoted regions so a `>` or `%` inside a
@@ -103,7 +124,11 @@ function findTags(source) {
103
124
  // `{{< … >}}` and `{{% … %}}` — the delimiters must agree.
104
125
  if ((open === "<" && close !== ">") || (open === "%" && close !== "%"))
105
126
  continue;
106
- if (inLiteral(m.index))
127
+ // In code, only a value shortcode's own tag is read (never a closer: it has no body).
128
+ if (inShown(m.index))
129
+ continue;
130
+ const literal = inLiteral(m.index);
131
+ if (literal && !(inCode?.has(m[4]) && m[3] !== "/"))
107
132
  continue;
108
133
  // `{{< tab name="x" include="y" />}}` — Hugo's explicit self-close. The
109
134
  // kubeadm reference pages use it 118 times. Without recognising it, the
@@ -119,6 +144,7 @@ function findTags(source) {
119
144
  delimiter: open === "<" ? "angle" : "percent",
120
145
  args: parseArgs(rawArgs),
121
146
  line: source.slice(0, m.index).split("\n").length,
147
+ ...(literal ? { inCode: true } : {}),
122
148
  });
123
149
  }
124
150
  return tags;
@@ -128,10 +154,16 @@ function findTags(source) {
128
154
  * A paired shortcode's body stays in `inner` for the caller to scan
129
155
  * recursively — that is what makes nesting work without a second parser.
130
156
  */
131
- export function scanShortcodes(source) {
132
- const tags = findTags(source);
157
+ export function scanShortcodes(source, inCode) {
158
+ const tags = findTags(source, inCode);
133
159
  const out = [];
134
160
  let cursor = 0;
161
+ // Where each name's last closing tag is: an opener after it has no closer, and is not searched
162
+ // for one. Without this, a page of many self-closing calls (`{{< skew … >}}` and no `{{< /skew >}}`)
163
+ // looked through every later tag for each one (measured 2026-11-22: 20,000 calls, 3.5 s).
164
+ const lastCloser = new Map();
165
+ tags.forEach((t, index) => { if (t.closing)
166
+ lastCloser.set(t.name, index); });
135
167
  for (let i = 0; i < tags.length; i++) {
136
168
  const tag = tags[i];
137
169
  if (tag.start < cursor)
@@ -141,7 +173,7 @@ export function scanShortcodes(source) {
141
173
  // Find the matching closer, counting nested opens of the SAME name.
142
174
  // An explicitly self-closed tag has no body and must not consume one.
143
175
  let closer;
144
- if (!tag.explicitSelfClose) {
176
+ if (!tag.explicitSelfClose && !tag.inCode && (lastCloser.get(tag.name) ?? -1) > i) {
145
177
  let depth = 1;
146
178
  for (let j = i + 1; j < tags.length; j++) {
147
179
  if (tags[j].name !== tag.name)
@@ -180,6 +212,7 @@ export function scanShortcodes(source) {
180
212
  raw: source.slice(tag.start, tag.end),
181
213
  selfClosing: true,
182
214
  line: tag.line,
215
+ ...(tag.inCode ? { inCode: true } : {}),
183
216
  });
184
217
  cursor = tag.end;
185
218
  }
@@ -0,0 +1,25 @@
1
+ export interface StaticFile {
2
+ /** Where it is written in the content folder: `_assets/images/a.png`. */
3
+ path: string;
4
+ /** Absolute path of the file in the Hugo site. */
5
+ source: string;
6
+ }
7
+ export interface StaticAssets {
8
+ /** Rewrite the references of one tree, in place. Returns the shown files that were not found. */
9
+ rewrite: (tree: unknown) => string[];
10
+ /** The files the rewritten trees refer to. */
11
+ files: () => StaticFile[];
12
+ /** More files were referred to than are taken (100,000): the rest keep the paths the site wrote. */
13
+ full: () => boolean;
14
+ }
15
+ export interface StaticOptions {
16
+ /**
17
+ * Content folders whose files are served at the same site paths (a page bundle's image,
18
+ * which the caller copies beside its page): a reference to one of those is not missing.
19
+ */
20
+ contentRoots?: string[];
21
+ /** The path part of the site's `baseURL` (`/docs`): references written with it name the same files. */
22
+ basePath?: string;
23
+ }
24
+ /** `staticDir`: absolute path of the site's static folder. Nothing is taken when it is not inside `siteDir`. */
25
+ export declare function staticAssets(siteDir: string, staticDir: string, options?: StaticOptions): StaticAssets;