blume 1.4.1 → 1.4.3

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.
Files changed (93) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/dist/cli/index.js +700 -579
  3. package/dist/cli/index.js.map +57 -56
  4. package/dist/types/core/base-path.d.ts +8 -0
  5. package/dist/types/core/config-input.d.ts +8 -0
  6. package/dist/types/core/data.d.ts +10 -0
  7. package/dist/types/core/schema.d.ts +4 -0
  8. package/dist/types/core/sources/types.d.ts +9 -1
  9. package/dist/types/openapi/references.d.ts +8 -2
  10. package/docs/configuration/ai.mdx +41 -9
  11. package/docs/content/sources.mdx +1 -1
  12. package/package.json +17 -7
  13. package/src/ai/agent-readability.ts +3 -2
  14. package/src/ai/api-catalog.ts +2 -2
  15. package/src/ai/ask-context.ts +45 -12
  16. package/src/ai/link-headers.ts +7 -2
  17. package/src/ai/llms.ts +2 -1
  18. package/src/ai/mcp/discovery.ts +25 -6
  19. package/src/ai/mcp/server.ts +108 -98
  20. package/src/ai/tar.ts +29 -70
  21. package/src/astro/examples.ts +7 -3
  22. package/src/astro/generate.ts +63 -34
  23. package/src/astro/islands.ts +7 -3
  24. package/src/astro/templates.ts +36 -9
  25. package/src/audit/agent.ts +14 -29
  26. package/src/audit/crawl.ts +41 -16
  27. package/src/audit/run.ts +10 -3
  28. package/src/audit/snapshot.ts +27 -2
  29. package/src/cli/commands/audit.ts +12 -17
  30. package/src/cli/commands/build.ts +15 -7
  31. package/src/cli/commands/dev.ts +13 -15
  32. package/src/cli/commands/eject.ts +4 -4
  33. package/src/cli/commands/eval.ts +17 -27
  34. package/src/cli/env.ts +13 -30
  35. package/src/cli/init/scaffold.ts +21 -0
  36. package/src/cli/report-format.ts +22 -0
  37. package/src/components/content/AccordionItem.astro +2 -9
  38. package/src/components/content/ColorItem.astro +5 -13
  39. package/src/components/content/Component.astro +12 -8
  40. package/src/components/content/Frame.astro +2 -12
  41. package/src/components/content/Prompt.astro +12 -31
  42. package/src/components/content/Tab.astro +2 -9
  43. package/src/components/content/Tooltip.astro +1 -9
  44. package/src/components/content/Update.astro +2 -9
  45. package/src/components/content/inline-markdown.ts +28 -0
  46. package/src/components/copy-feedback.ts +96 -0
  47. package/src/components/islands/ask-ai.tsx +78 -9
  48. package/src/components/layout/PageActions.astro +20 -32
  49. package/src/components/layout/PageLayout.astro +8 -28
  50. package/src/components/layout/RootLayout.astro +47 -48
  51. package/src/components/layout/Search.astro +56 -9
  52. package/src/components/layout/drawer-inert.ts +31 -0
  53. package/src/components/layout/search/pagefind.ts +6 -5
  54. package/src/components/layout/search/types.ts +32 -0
  55. package/src/components/openapi/panel.ts +11 -8
  56. package/src/components/raf-throttle.ts +21 -0
  57. package/src/components/slug.ts +14 -0
  58. package/src/core/base-path.ts +18 -1
  59. package/src/core/config-input.ts +8 -0
  60. package/src/core/data.ts +7 -0
  61. package/src/core/frontmatter.ts +45 -1
  62. package/src/core/probe.ts +7 -19
  63. package/src/core/project-graph.ts +12 -1
  64. package/src/core/schema.ts +6 -0
  65. package/src/core/site-url.ts +27 -0
  66. package/src/core/sources/cache.ts +10 -8
  67. package/src/core/sources/github-releases.ts +21 -1
  68. package/src/core/sources/normalize.ts +26 -2
  69. package/src/core/sources/notion.ts +27 -5
  70. package/src/core/sources/portable-text.ts +16 -1
  71. package/src/core/sources/resolve.ts +1 -0
  72. package/src/core/sources/types.ts +13 -1
  73. package/src/deploy/cloudflare-negotiation.ts +15 -1
  74. package/src/deploy/robots.ts +2 -1
  75. package/src/deploy/rss.ts +2 -1
  76. package/src/deploy/sitemap.ts +56 -7
  77. package/src/eval/agents.ts +13 -10
  78. package/src/eval/report.ts +1 -14
  79. package/src/markdown/package-commands.ts +61 -54
  80. package/src/og/card.ts +24 -26
  81. package/src/openapi/model.ts +9 -9
  82. package/src/openapi/parse.ts +69 -28
  83. package/src/openapi/references.ts +35 -12
  84. package/src/openapi/render-mdx.ts +64 -25
  85. package/src/openapi/scalar.ts +2 -2
  86. package/src/openapi/source.ts +28 -1
  87. package/src/search/documents.ts +78 -34
  88. package/src/search/orama-index.ts +51 -12
  89. package/src/theme/palette.ts +6 -2
  90. package/src/translate/ledger.ts +4 -2
  91. package/src/translate/report.ts +1 -14
  92. package/src/translate/run.ts +20 -35
  93. package/src/cli/coalesce.ts +0 -43
@@ -3,11 +3,7 @@ import { existsSync } from "node:fs";
3
3
  import { defineCommand } from "citty";
4
4
  import { join } from "pathe";
5
5
 
6
- import {
7
- AGENTS,
8
- launchAgent,
9
- WINDOWS_COMMAND_NOT_FOUND,
10
- } from "../../audit/agent.ts";
6
+ import { AGENTS, launchAgent } from "../../audit/agent.ts";
11
7
  import type { AgentKind } from "../../audit/agent.ts";
12
8
  import { BlumeError } from "../../core/diagnostics.ts";
13
9
  import { scanProject } from "../../core/project-graph.ts";
@@ -37,32 +33,32 @@ const DEFAULT_TIMEOUT_S = 180;
37
33
  const isAgentKind = (value: string): value is AgentKind => value in AGENTS;
38
34
 
39
35
  /**
40
- * Launch the interactive agent CLI, translating a missing executable into the
41
- * Windows not-found sentinel. Only `ENOENT` means "not installed" — any other
42
- * spawn failure (`EACCES`, `EMFILE`, …) must surface as itself.
36
+ * Launch the interactive agent CLI, turning a missing executable into the
37
+ * install hint. Only `ENOENT` means "not installed" — any other spawn failure
38
+ * (`EACCES`, `EMFILE`, …) must surface as itself.
43
39
  */
40
+ const notInstalled = (agent: AgentKind): never => {
41
+ const cli = AGENTS[agent];
42
+ logger.error(
43
+ `${cli.name} (\`${cli.bin}\`) was not found on PATH. Install it with \`${cli.install}\`.`
44
+ );
45
+ return process.exit(1);
46
+ };
47
+
44
48
  const launchAgentCode = async (
45
- bin: string,
49
+ agent: AgentKind,
46
50
  prompt: string
47
51
  ): Promise<number> => {
48
52
  try {
49
- return await launchAgent(bin, prompt);
53
+ return await launchAgent(AGENTS[agent].bin, prompt);
50
54
  } catch (error) {
51
55
  if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") {
52
56
  throw error;
53
57
  }
54
- return WINDOWS_COMMAND_NOT_FOUND;
58
+ return notInstalled(agent);
55
59
  }
56
60
  };
57
61
 
58
- const notInstalled = (agent: AgentKind): never => {
59
- const cli = AGENTS[agent];
60
- logger.error(
61
- `${cli.name} (\`${cli.bin}\`) was not found on PATH. Install it with \`${cli.install}\`.`
62
- );
63
- return process.exit(1);
64
- };
65
-
66
62
  /** The fraction of run (non-skipped) questions that passed. */
67
63
  export const passFraction = (result: EvalResult): number => {
68
64
  const ran = result.results.length - result.counts.skip;
@@ -125,10 +121,7 @@ const runFixHandoff = async (
125
121
  process.stderr.write(
126
122
  ` Handing ${count} failed question${count === 1 ? "" : "s"} to ${cli.name}…\n\n`
127
123
  );
128
- const code = await launchAgentCode(cli.bin, evalFixPrompt(report));
129
- if (code === WINDOWS_COMMAND_NOT_FOUND) {
130
- notInstalled(agent);
131
- }
124
+ const code = await launchAgentCode(agent, evalFixPrompt(report));
132
125
  if (code !== 0) {
133
126
  process.exit(code);
134
127
  }
@@ -143,10 +136,7 @@ const runInit = async (agent: AgentKind, file: string): Promise<void> => {
143
136
  );
144
137
  process.exit(1);
145
138
  }
146
- const code = await launchAgentCode(AGENTS[agent].bin, initPrompt(file));
147
- if (code === WINDOWS_COMMAND_NOT_FOUND) {
148
- notInstalled(agent);
149
- }
139
+ const code = await launchAgentCode(agent, initPrompt(file));
150
140
  if (code !== 0) {
151
141
  process.exit(code);
152
142
  }
package/src/cli/env.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { existsSync, readFileSync } from "node:fs";
1
+ import { existsSync } from "node:fs";
2
2
 
3
- import { parse } from "dotenv";
3
+ import { config } from "dotenv";
4
4
  import { dirname, join, resolve } from "pathe";
5
5
 
6
6
  // Blume's remote sources (GitHub Releases, mdx-remote, Sanity, Notion…) read
@@ -9,44 +9,27 @@ import { dirname, join, resolve } from "pathe";
9
9
  // that gap: it cascades `.env`/`.env.local` from the working dir up to the repo
10
10
  // root, so a monorepo can keep one `.env` at the root and every app picks it up.
11
11
 
12
- /** Apply parsed vars without clobbering anything already in `process.env`. */
13
- const applyEnv = (parsed: Record<string, string>): void => {
14
- for (const [key, value] of Object.entries(parsed)) {
15
- if (!(key in process.env)) {
16
- process.env[key] = value;
17
- }
18
- }
19
- };
20
-
21
- const loadFile = (path: string): void => {
22
- try {
23
- if (existsSync(path)) {
24
- // dotenv is the same parser Vite runs over these files at build time,
25
- // so a value means the same thing to the pre-boot content scan and the
26
- // built site — including multi-line double-quoted values (PEM keys),
27
- // which a line-based parser silently truncates.
28
- applyEnv(parse(readFileSync(path, "utf-8")));
29
- }
30
- } catch {
31
- // Env files are best-effort; a read/parse failure must not abort a build.
32
- }
33
- };
34
-
35
12
  /**
36
13
  * Load `.env`/`.env.local`, cascading from `startDir` up to the repository root
37
- * (the first ancestor containing a `.git`) or the filesystem root. Nearer files
38
- * and existing `process.env` values win, so shell/CI overrides are never lost
39
- * and `.env.local` layers over `.env`.
14
+ * (the first ancestor containing a `.git`) or the filesystem root. The ordered
15
+ * path list is handed to dotenv, whose `config` is first-wins, never clobbers
16
+ * existing `process.env` values, and treats unreadable files as best-effort —
17
+ * so shell/CI overrides are never lost, `.env.local` layers over `.env`, and a
18
+ * bad file never aborts a build. dotenv is also the parser Vite runs over
19
+ * these files at build time, so a value means the same thing to the pre-boot
20
+ * content scan and the built site — including multi-line double-quoted values
21
+ * (PEM keys), which a line-based parser silently truncates.
40
22
  */
41
23
  export const loadEnvFiles = (startDir: string): void => {
24
+ const paths: string[] = [];
42
25
  let dir = resolve(startDir);
43
26
  let done = false;
44
27
  while (!done) {
45
- loadFile(join(dir, ".env.local"));
46
- loadFile(join(dir, ".env"));
28
+ paths.push(join(dir, ".env.local"), join(dir, ".env"));
47
29
  const parent = dirname(dir);
48
30
  // Stop at the repo root (nearest `.git`) or the filesystem root.
49
31
  done = existsSync(join(dir, ".git")) || parent === dir;
50
32
  dir = parent;
51
33
  }
34
+ config({ path: paths, quiet: true });
52
35
  };
@@ -1,6 +1,7 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { mkdir, writeFile } from "node:fs/promises";
3
3
 
4
+ import { detect } from "package-manager-detector/detect";
4
5
  import { basename, dirname, isAbsolute, join, relative } from "pathe";
5
6
 
6
7
  import { blumePackageJson, toPackageName } from "../../core/package-json.ts";
@@ -155,12 +156,32 @@ export const commandsFor = (
155
156
  /**
156
157
  * Derive the package manager from an npm user-agent string (the first
157
158
  * `name/version` token of `npm_config_user_agent`), falling back to npm.
159
+ * Right for `init`, where the project doesn't exist yet and the invoking
160
+ * runner is the only signal.
158
161
  */
159
162
  export const detectPackageManager = (userAgent?: string): PackageManager => {
160
163
  const name = userAgent?.split("/")[0] as PackageManager | undefined;
161
164
  return name !== undefined && PACKAGE_MANAGERS.includes(name) ? name : "npm";
162
165
  };
163
166
 
167
+ /**
168
+ * Detect an existing project's package manager from its lockfile /
169
+ * `packageManager` field (package-manager-detector), falling back to the
170
+ * user agent. Right for `eject`: the CLI is often run directly (`npx blume
171
+ * eject`, a bare `blume eject`), where `npm_config_user_agent` is absent or
172
+ * names the runner rather than the project's manager, and the old
173
+ * user-agent-only detection silently printed npm hints for a pnpm project.
174
+ */
175
+ export const detectProjectPackageManager = async (
176
+ root: string
177
+ ): Promise<PackageManager> => {
178
+ const detected = await detect({ cwd: root });
179
+ const name = detected?.name as PackageManager | undefined;
180
+ return name !== undefined && PACKAGE_MANAGERS.includes(name)
181
+ ? name
182
+ : detectPackageManager(process.env.npm_config_user_agent);
183
+ };
184
+
164
185
  /**
165
186
  * The content dir is joined into every scaffolded file path, so an absolute or
166
187
  * `../`-escaping value would write outside the project. Returns an error
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Number formatting shared by the translate and eval report renderers. The
3
+ * `1.5s` / `4m 12s` shapes are asserted in both suites — change them in both
4
+ * minds at once.
5
+ */
6
+
7
+ /** Milliseconds as `1.5s`. */
8
+ export const seconds = (ms: number): string => `${(ms / 1000).toFixed(1)}s`;
9
+
10
+ /** A dollar cost as `$1.23`, or nothing when the run reported none. */
11
+ export const money = (cost: number | undefined): string =>
12
+ cost === undefined ? "" : `$${cost.toFixed(2)}`;
13
+
14
+ /** Milliseconds as `12.3s` under a minute, `4m 12s` from there up. */
15
+ export const duration = (ms: number): string => {
16
+ if (ms < 60_000) {
17
+ return seconds(ms);
18
+ }
19
+ const minutes = Math.floor(ms / 60_000);
20
+ const rest = Math.round((ms % 60_000) / 1000);
21
+ return `${minutes}m ${rest}s`;
22
+ };
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  import Icon from "../Icon.astro";
3
+ import { componentSlug } from "../slug.ts";
3
4
 
4
5
  interface Props {
5
6
  title: string;
@@ -10,15 +11,7 @@ interface Props {
10
11
  }
11
12
 
12
13
  const { defaultOpen = false, description, icon, id, title } = Astro.props;
13
- const slugify = (value: string): string =>
14
- value
15
- .toLowerCase()
16
- .trim()
17
- .replaceAll(/[^\w\s-]/gu, "")
18
- .replaceAll(/[\s_]+/gu, "-")
19
- .replaceAll(/-+/gu, "-")
20
- .replaceAll(/^-|-$/gu, "");
21
- const accordionId = id ?? slugify(title);
14
+ const accordionId = id ?? componentSlug(title);
22
15
  ---
23
16
 
24
17
  <details
@@ -53,6 +53,8 @@ const displayValue =
53
53
  </style>
54
54
 
55
55
  <script>
56
+ import { copyText, flashLabel } from "../copy-feedback.ts";
57
+
56
58
  const state = window as Window & { __blumeColorCopy?: boolean };
57
59
  if (!state.__blumeColorCopy) {
58
60
  state.__blumeColorCopy = true;
@@ -67,24 +69,14 @@ const displayValue =
67
69
  return;
68
70
  }
69
71
 
70
- try {
71
- await navigator.clipboard.writeText(button.dataset.blumeColorCopy ?? "");
72
- } catch {
72
+ if (!(await copyText(button.dataset.blumeColorCopy ?? ""))) {
73
73
  return;
74
74
  }
75
75
 
76
76
  const status = button.querySelector<HTMLElement>("[data-blume-color-status]");
77
- if (!status) {
78
- return;
77
+ if (status) {
78
+ flashLabel(status, "Copied");
79
79
  }
80
-
81
- // Remember the real value once — capturing at click time would capture
82
- // "Copied" on a double-click and stick until reload.
83
- status.dataset.blumeLabel ??= status.textContent ?? "";
84
- status.textContent = "Copied";
85
- window.setTimeout(() => {
86
- status.textContent = status.dataset.blumeLabel ?? "";
87
- }, 1500);
88
80
  });
89
81
  }
90
82
  </script>
@@ -165,12 +165,16 @@ const paneClass = "motion-safe:transition-[height] motion-safe:duration-200";
165
165
 
166
166
  // A pane capped by a small viewport would otherwise stay small after the
167
167
  // window grows: the frame's content stopped changing size, so its observer
168
- // has nothing new to report.
169
- window.addEventListener("resize", () => {
170
- for (const frame of document.querySelectorAll<HTMLIFrameElement>(
171
- "iframe[data-blume-example-frame][data-blume-reported-height]"
172
- )) {
173
- applyMeasuredHeight(frame);
174
- }
175
- });
168
+ // has nothing new to report. rAF-coalesced so a live resize drag runs the
169
+ // read-then-write pass once per frame, not once per event.
170
+ window.addEventListener(
171
+ "resize",
172
+ rafThrottle(() => {
173
+ for (const frame of document.querySelectorAll<HTMLIFrameElement>(
174
+ "iframe[data-blume-example-frame][data-blume-reported-height]"
175
+ )) {
176
+ applyMeasuredHeight(frame);
177
+ }
178
+ })
179
+ );
176
180
  </script>
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  import { createSatteriMarkdownProcessor } from "@astrojs/markdown-satteri";
3
3
 
4
+ import { escapeRawHtml, unwrapParagraph } from "./inline-markdown.ts";
5
+
4
6
  interface Props {
5
7
  caption?: string;
6
8
  hint?: string;
@@ -8,18 +10,6 @@ interface Props {
8
10
 
9
11
  const { caption, hint } = Astro.props;
10
12
 
11
- const escapeRawHtml = (value: string): string =>
12
- value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
13
-
14
- const unwrapParagraph = (html: string): string => {
15
- const trimmed = html.trim();
16
- // Only a *single* paragraph is unwrapped: the content must not contain its
17
- // own `</p>`, or `<p>a</p>\n<p>b</p>` would "unwrap" to `a</p>\n<p>b` —
18
- // unbalanced HTML injected via set:html.
19
- const match = trimmed.match(/^<p>(?<content>(?:(?!<\/p>)[\s\S])*)<\/p>$/u);
20
- return match?.groups?.content ?? trimmed;
21
- };
22
-
23
13
  const markdownProcessor = await createSatteriMarkdownProcessor();
24
14
  const captionHtml = caption
25
15
  ? unwrapParagraph((await markdownProcessor.render(escapeRawHtml(caption))).code)
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  import { createSatteriMarkdownProcessor } from "@astrojs/markdown-satteri";
3
3
 
4
+ import { escapeRawHtml, unwrapParagraph } from "./inline-markdown.ts";
5
+
4
6
  interface Props {
5
7
  actions?: boolean | string | string[];
6
8
  description: string;
@@ -8,18 +10,6 @@ interface Props {
8
10
 
9
11
  const { actions = ["copy"], description } = Astro.props;
10
12
 
11
- const escapeRawHtml = (value: string): string =>
12
- value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
13
-
14
- const unwrapParagraph = (html: string): string => {
15
- const trimmed = html.trim();
16
- // Only a *single* paragraph is unwrapped: the content must not contain its
17
- // own `</p>`, or `<p>a</p>\n<p>b</p>` would "unwrap" to `a</p>\n<p>b` —
18
- // unbalanced HTML injected via set:html.
19
- const match = trimmed.match(/^<p>(?<content>(?:(?!<\/p>)[\s\S])*)<\/p>$/u);
20
- return match?.groups?.content ?? trimmed;
21
- };
22
-
23
13
  const normalizeActions = (value: Props["actions"]): string[] => {
24
14
  if (Array.isArray(value)) {
25
15
  return value;
@@ -93,6 +83,8 @@ const secondaryButton =
93
83
  </blume-prompt>
94
84
 
95
85
  <script>
86
+ import { copyText, createCopyFlash } from "../copy-feedback.ts";
87
+
96
88
  class BlumePrompt extends HTMLElement {
97
89
  connectedCallback() {
98
90
  const content = this.querySelector<HTMLElement>(
@@ -116,28 +108,17 @@ const secondaryButton =
116
108
  const done = copy?.querySelector<HTMLElement>(
117
109
  "[data-blume-prompt-copy-done]"
118
110
  );
119
- let resetTimer: ReturnType<typeof setTimeout> | undefined;
111
+ // Toggle visibility rather than swapping text: both labels occupy the
112
+ // same grid cell, so the button width never changes.
113
+ const flash = createCopyFlash((copied) => {
114
+ idle?.classList.toggle("invisible", copied);
115
+ done?.classList.toggle("invisible", !copied);
116
+ }, "Copied");
120
117
  copy?.addEventListener("click", async () => {
121
118
  const text = promptText();
122
- if (!text) {
123
- return;
124
- }
125
- try {
126
- await navigator.clipboard.writeText(text);
127
- } catch {
128
- return;
119
+ if (text && (await copyText(text))) {
120
+ flash();
129
121
  }
130
-
131
- // Toggle visibility rather than swapping text: both labels occupy the
132
- // same grid cell, so the button width never changes. Clearing the timer
133
- // keeps the "Copied" state 1.5s past the last of several rapid clicks.
134
- clearTimeout(resetTimer);
135
- idle?.classList.add("invisible");
136
- done?.classList.remove("invisible");
137
- resetTimer = setTimeout(() => {
138
- idle?.classList.remove("invisible");
139
- done?.classList.add("invisible");
140
- }, 1500);
141
122
  });
142
123
  }
143
124
  }
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  import Icon from "../Icon.astro";
3
+ import { componentSlug } from "../slug.ts";
3
4
 
4
5
  interface Props {
5
6
  class?: string;
@@ -10,15 +11,7 @@ interface Props {
10
11
  }
11
12
 
12
13
  const { class: className, icon, id, style, title } = Astro.props;
13
- const slugify = (value: string): string =>
14
- value
15
- .toLowerCase()
16
- .trim()
17
- .replaceAll(/[^\w\s-]/gu, "")
18
- .replaceAll(/[\s_]+/gu, "-")
19
- .replaceAll(/-+/gu, "-")
20
- .replaceAll(/^-|-$/gu, "");
21
- const tabId = id ?? slugify(title);
14
+ const tabId = id ?? componentSlug(title);
22
15
  ---
23
16
 
24
17
  {/*
@@ -2,6 +2,7 @@
2
2
  import { randomUUID } from "node:crypto";
3
3
 
4
4
  import { contentHref } from "./base-href.ts";
5
+ import { unwrapParagraph } from "./inline-markdown.ts";
5
6
 
6
7
  interface Props {
7
8
  cta?: string;
@@ -14,15 +15,6 @@ const { cta, headline, href, tip } = Astro.props;
14
15
  const tooltipId = `blume-tooltip-${randomUUID()}`;
15
16
  const external = href?.startsWith("http");
16
17
 
17
- const unwrapParagraph = (html: string): string => {
18
- const trimmed = html.trim();
19
- // Only a *single* paragraph is unwrapped: the content must not contain its
20
- // own `</p>`, or `<p>a</p>\n<p>b</p>` would "unwrap" to `a</p>\n<p>b` —
21
- // unbalanced HTML injected via set:html.
22
- const match = trimmed.match(/^<p>(?<content>(?:(?!<\/p>)[\s\S])*)<\/p>$/u);
23
- return match?.groups?.content ?? trimmed;
24
- };
25
-
26
18
  const labelHtml = Astro.slots.has("default")
27
19
  ? unwrapParagraph(await Astro.slots.render("default"))
28
20
  : "";
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { componentSlug } from "../slug.ts";
2
3
  import { contentHref } from "./base-href.ts";
3
4
 
4
5
  interface RssMetadata {
@@ -31,16 +32,8 @@ const {
31
32
  tags,
32
33
  title,
33
34
  } = Astro.props;
34
- const slugify = (text: string): string =>
35
- text
36
- .toLowerCase()
37
- .trim()
38
- .replaceAll(/[^\w\s-]/gu, "")
39
- .replaceAll(/[\s_]+/gu, "-")
40
- .replaceAll(/-+/gu, "-")
41
- .replaceAll(/^-|-$/gu, "");
42
35
  const updateLabel = label ?? title ?? "Update";
43
- const id = providedId ?? (slugify(updateLabel) || "update");
36
+ const id = providedId ?? (componentSlug(updateLabel) || "update");
44
37
  const tagList = Array.isArray(tags) ? tags : tags ? [tags] : [];
45
38
  ---
46
39
 
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Shared helpers for components that render a short Markdown prop (a caption,
3
+ * a description, a tooltip label) into inline HTML injected via `set:html`.
4
+ * Previously copied verbatim into Prompt, Frame, and Tooltip.
5
+ */
6
+
7
+ /**
8
+ * Neutralize raw HTML in a Markdown-rendered text prop by escaping only
9
+ * `<`/`>`. `&` deliberately stays: CommonMark already renders a bare `&` as
10
+ * `&amp;` and resolves real entity references, so leaving it alone keeps
11
+ * `&copy;`-style authoring working — a full HTML escape would render it as
12
+ * the literal text `&copy;`.
13
+ */
14
+ export const escapeRawHtml = (value: string): string =>
15
+ value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
16
+
17
+ /**
18
+ * Unwrap the `<p>` a block-level Markdown render wraps around single-line
19
+ * content, so it can sit inside inline markup. Only a *single* paragraph is
20
+ * unwrapped: the content must not contain its own `</p>`, or
21
+ * `<p>a</p>\n<p>b</p>` would "unwrap" to `a</p>\n<p>b` — unbalanced HTML
22
+ * injected via `set:html`.
23
+ */
24
+ export const unwrapParagraph = (html: string): string => {
25
+ const trimmed = html.trim();
26
+ const match = trimmed.match(/^<p>(?<content>(?:(?!<\/p>)[\s\S])*)<\/p>$/u);
27
+ return match?.groups?.content ?? trimmed;
28
+ };
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Shared clipboard + "Copied" feedback used by every copy affordance (code
3
+ * blocks, page actions, color swatches, prompts, API panels, Ask AI). One
4
+ * implementation owns the invariants each site used to hand-roll:
5
+ *
6
+ * - the clipboard write is guarded, and nothing flashes on failure — a
7
+ * confirmation must never lie;
8
+ * - repeat copies restart the hold instead of stacking timers, so the copied
9
+ * state never reverts early after a double-click;
10
+ * - every successful copy is announced to a shared polite live region, so the
11
+ * confirmation is audible, not just visual (previously only the code-block
12
+ * button announced).
13
+ */
14
+
15
+ /** How long the copied confirmation holds before reverting. */
16
+ const HOLD_MS = 1500;
17
+
18
+ /** The shared visually-hidden live region, created on first announcement. */
19
+ let region: HTMLElement | null = null;
20
+
21
+ /**
22
+ * Announce `message` to screen readers. The region is re-created if a swap
23
+ * (view transition, client router) disconnected it.
24
+ */
25
+ export const announceCopied = (message: string): void => {
26
+ if (!region?.isConnected) {
27
+ region = document.createElement("div");
28
+ region.setAttribute("role", "status");
29
+ region.className = "sr-only";
30
+ document.body.append(region);
31
+ }
32
+ // Clear first so repeating the same message is re-announced.
33
+ region.textContent = "";
34
+ region.textContent = message;
35
+ };
36
+
37
+ /**
38
+ * Copy `text` to the clipboard. Returns whether the write succeeded; failures
39
+ * (insecure context, permissions) are swallowed so callers can simply skip
40
+ * their confirmation.
41
+ */
42
+ export const copyText = async (text: string): Promise<boolean> => {
43
+ try {
44
+ await navigator.clipboard.writeText(text);
45
+ return true;
46
+ } catch {
47
+ return false;
48
+ }
49
+ };
50
+
51
+ /**
52
+ * A per-affordance flash: `apply(true)` paints the copied state, `apply(false)`
53
+ * reverts it after the hold. Calling the returned function again restarts the
54
+ * hold. `announce` (the localized "Copied" label) is spoken on each flash.
55
+ */
56
+ export const createCopyFlash = (
57
+ apply: (copied: boolean) => void,
58
+ announce?: string,
59
+ holdMs: number = HOLD_MS
60
+ ): (() => void) => {
61
+ let timer: ReturnType<typeof setTimeout> | undefined;
62
+ return () => {
63
+ clearTimeout(timer);
64
+ apply(true);
65
+ if (announce) {
66
+ announceCopied(announce);
67
+ }
68
+ timer = setTimeout(() => apply(false), holdMs);
69
+ };
70
+ };
71
+
72
+ /** Flash timers for {@link flashLabel}, keyed per element. */
73
+ const labelTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
74
+
75
+ /**
76
+ * The text-swap flash: show `message` in `el`, then restore its own (possibly
77
+ * localized) label after the hold. The original label is captured once, on the
78
+ * first flash — capturing at click time would capture the flash message itself
79
+ * on a double-click and stick until reload.
80
+ */
81
+ export const flashLabel = (
82
+ el: HTMLElement,
83
+ message: string,
84
+ holdMs: number = HOLD_MS
85
+ ): void => {
86
+ el.dataset.blumeLabel ??= el.textContent ?? "";
87
+ el.textContent = message;
88
+ announceCopied(message);
89
+ clearTimeout(labelTimers.get(el));
90
+ labelTimers.set(
91
+ el,
92
+ setTimeout(() => {
93
+ el.textContent = el.dataset.blumeLabel ?? "";
94
+ }, holdMs)
95
+ );
96
+ };