@seed-hypermedia/client 0.0.104 → 0.0.105

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.
@@ -562,7 +562,13 @@ var METADATA_STRING_KEYS = [
562
562
  "childrenType"
563
563
  ];
564
564
  var METADATA_BOOLEAN_KEYS = ["showOutline", "showActivity"];
565
- function parseFrontmatter(markdown) {
565
+ var MarkdownFrontmatterError = class extends Error {
566
+ constructor(message) {
567
+ super(message);
568
+ this.name = "MarkdownFrontmatterError";
569
+ }
570
+ };
571
+ function parseFrontmatter(markdown, options = {}) {
566
572
  const trimmed = markdown.replace(/^/, "");
567
573
  if (!trimmed.startsWith("---")) {
568
574
  return { content: markdown, metadata: {} };
@@ -599,7 +605,13 @@ function parseFrontmatter(markdown) {
599
605
  for (const key of METADATA_BOOLEAN_KEYS) {
600
606
  if (metadata[key] !== void 0 && typeof metadata[key] !== "boolean") metadata[key] = Boolean(metadata[key]);
601
607
  }
602
- } catch {
608
+ } catch (error) {
609
+ if (options.strictFrontmatter !== false) {
610
+ const detail = (error instanceof Error ? error.message.split("\n")[0] : String(error)).replace(/:\s*$/, "");
611
+ throw new MarkdownFrontmatterError(
612
+ `Invalid YAML in frontmatter: ${detail}. Quote values that contain a colon, e.g. name: "A: B".`
613
+ );
614
+ }
603
615
  return { content: markdown, metadata: {} };
604
616
  }
605
617
  return { content: rest, metadata };
@@ -611,9 +623,9 @@ var IMAGE_START_RE = /^!\[/;
611
623
  function startsSpecialBlock(rest) {
612
624
  return HEADING_RE.test(rest) || FENCE_RE.test(rest) || rest.startsWith("$$") || IMAGE_START_RE.test(rest) || rest.startsWith("|") || STANDALONE_COMMENT_RE.test(rest) || END_RE.test(rest);
613
625
  }
614
- function parseMarkdown(markdown) {
626
+ function parseMarkdown(markdown, options = {}) {
615
627
  var _a;
616
- const { content, metadata } = parseFrontmatter(markdown);
628
+ const { content, metadata } = parseFrontmatter(markdown, options);
617
629
  const lines = content.split("\n");
618
630
  const infos = lines.map(analyzeLine);
619
631
  const root = { list: [], childIndent: 0 };
@@ -894,6 +906,7 @@ export {
894
906
  parseInlineFormatting,
895
907
  hmAccountMentionUid,
896
908
  mergeAdjacentAnnotations,
909
+ MarkdownFrontmatterError,
897
910
  parseFrontmatter,
898
911
  parseMarkdown,
899
912
  markdownBlockNodesToHMBlockNodes,
@@ -0,0 +1,58 @@
1
+ // src/mermaid-validation.ts
2
+ var InvalidMermaidError = class extends Error {
3
+ errors;
4
+ constructor(errors) {
5
+ super(
6
+ `${errors.length === 1 ? "Invalid mermaid diagram" : `${errors.length} invalid mermaid diagrams`}: ` + errors.map((e) => `block ${e.blockId}: ${e.message}`).join("; ")
7
+ );
8
+ this.name = "InvalidMermaidError";
9
+ this.errors = errors;
10
+ }
11
+ };
12
+ function blockLanguage(block) {
13
+ if (typeof block.language === "string") return block.language;
14
+ const attr = block.attributes && block.attributes.language;
15
+ return typeof attr === "string" ? attr : void 0;
16
+ }
17
+ function collectMermaidBlocks(nodes) {
18
+ const out = [];
19
+ const walk = (list) => {
20
+ var _a, _b;
21
+ for (const node of list) {
22
+ const { block } = node;
23
+ if (block.type === "Code" && ((_a = blockLanguage(block)) == null ? void 0 : _a.trim().toLowerCase()) === "mermaid") {
24
+ out.push({ blockId: block.id ?? "", source: block.text ?? "" });
25
+ }
26
+ if ((_b = node.children) == null ? void 0 : _b.length) walk(node.children);
27
+ }
28
+ };
29
+ walk(nodes);
30
+ return out;
31
+ }
32
+ async function validateMermaidBlocks(nodes, parse) {
33
+ const errors = [];
34
+ for (const { blockId, source } of collectMermaidBlocks(nodes)) {
35
+ if (!source.trim()) {
36
+ errors.push({ blockId, message: "empty diagram" });
37
+ continue;
38
+ }
39
+ try {
40
+ await parse(source);
41
+ } catch (error) {
42
+ const message = error instanceof Error ? error.message : String(error);
43
+ errors.push({ blockId, message: message.split("\n").slice(0, 3).join(" ").trim() });
44
+ }
45
+ }
46
+ return errors;
47
+ }
48
+ async function assertValidMermaid(nodes, parse) {
49
+ const errors = await validateMermaidBlocks(nodes, parse);
50
+ if (errors.length) throw new InvalidMermaidError(errors);
51
+ }
52
+
53
+ export {
54
+ InvalidMermaidError,
55
+ collectMermaidBlocks,
56
+ validateMermaidBlocks,
57
+ assertValidMermaid
58
+ };
package/dist/index.d.ts CHANGED
@@ -31,8 +31,10 @@ export { resolveIdWithClient } from './resource-read';
31
31
  export type { ResolveIdWithClientOptions, ResolvedIdWithClient } from './resource-read';
32
32
  export { fileToIpfsBlobs, filesToIpfsBlobs, resolveFileLinksInBlocks, hasFileLinks } from './file-to-ipfs';
33
33
  export type { CollectedBlob } from './file-to-ipfs';
34
- export { parseMarkdown, flattenToOperations, parseInlineFormatting, parseFrontmatter, markdownBlockNodesToHMBlockNodes, hmAccountMentionUid, } from './markdown-to-blocks';
35
- export type { BlockNode, SeedBlock, Annotation } from './markdown-to-blocks';
34
+ export { parseMarkdown, flattenToOperations, parseInlineFormatting, parseFrontmatter, markdownBlockNodesToHMBlockNodes, hmAccountMentionUid, MarkdownFrontmatterError, } from './markdown-to-blocks';
35
+ export type { BlockNode, SeedBlock, Annotation, ParseMarkdownOptions } from './markdown-to-blocks';
36
+ export { collectMermaidBlocks, validateMermaidBlocks, assertValidMermaid, InvalidMermaidError, } from './mermaid-validation';
37
+ export type { MermaidParser, MermaidDiagramError, MermaidBlockLike } from './mermaid-validation';
36
38
  export { blocksToMarkdown, emitFrontmatter, slugify, draftFilename, parseDraftFilename, documentToResolvedMarkdown, commentToResolvedMarkdown, contentToResolvedMarkdown, } from './blocks-to-markdown';
37
39
  export type { ResolvedMarkdownOptions } from './blocks-to-markdown';
38
40
  export type { BlocksToMarkdownOptions } from './blocks-to-markdown';
package/dist/index.mjs CHANGED
@@ -1,3 +1,12 @@
1
+ import {
2
+ pdfToBlocks
3
+ } from "./chunk-NSNLRZ3X.mjs";
4
+ import {
5
+ teiToBlocks
6
+ } from "./chunk-GIKXPGFZ.mjs";
7
+ import {
8
+ embeddedPdfToBlocks
9
+ } from "./chunk-2FJBS3V7.mjs";
1
10
  import {
2
11
  resolveIdWithClient
3
12
  } from "./chunk-MHICVQJI.mjs";
@@ -10,22 +19,20 @@ import {
10
19
  keyfile_exports
11
20
  } from "./chunk-EY6RQVAP.mjs";
12
21
  import {
22
+ MarkdownFrontmatterError,
13
23
  flattenToOperations,
14
24
  hmAccountMentionUid,
15
25
  markdownBlockNodesToHMBlockNodes,
16
26
  parseFrontmatter,
17
27
  parseInlineFormatting,
18
28
  parseMarkdown
19
- } from "./chunk-K5NYTWBJ.mjs";
29
+ } from "./chunk-KKKXGVKJ.mjs";
20
30
  import {
21
- pdfToBlocks
22
- } from "./chunk-NSNLRZ3X.mjs";
23
- import {
24
- teiToBlocks
25
- } from "./chunk-GIKXPGFZ.mjs";
26
- import {
27
- embeddedPdfToBlocks
28
- } from "./chunk-2FJBS3V7.mjs";
31
+ InvalidMermaidError,
32
+ assertValidMermaid,
33
+ collectMermaidBlocks,
34
+ validateMermaidBlocks
35
+ } from "./chunk-TGCGGVEU.mjs";
29
36
  import {
30
37
  editorBlockToHMBlock,
31
38
  editorBlocksToHMBlockNodes
@@ -155,14 +162,18 @@ export {
155
162
  AnnotationSet,
156
163
  DEFAULT_GROBID_URL,
157
164
  HYPERMEDIA_SCHEME,
165
+ InvalidMermaidError,
166
+ MarkdownFrontmatterError,
158
167
  SeedClientError,
159
168
  SeedNetworkError,
160
169
  SeedValidationError,
161
170
  addSpanToAnnotation,
162
171
  annotationContains,
172
+ assertValidMermaid,
163
173
  autoLinkChildToParent,
164
174
  blocksToMarkdown,
165
175
  codePointLength,
176
+ collectMermaidBlocks,
166
177
  commentRecordIdFromBlob,
167
178
  commentToResolvedMarkdown,
168
179
  computeReplaceOps,
@@ -245,5 +256,6 @@ export {
245
256
  trimTrailingEmptyBlocks,
246
257
  unpackHmId,
247
258
  updateComment,
248
- updateContact
259
+ updateContact,
260
+ validateMermaidBlocks
249
261
  };
@@ -71,7 +71,23 @@ export type BlockComment = {
71
71
  * and keys this client does not know (schema-typed documents carry their
72
72
  * own). `title:` is accepted as a backward-compatible alias for `name:`.
73
73
  */
74
- export declare function parseFrontmatter(markdown: string): {
74
+ /**
75
+ * Thrown when a document starts with a `---` frontmatter block whose YAML does not parse.
76
+ * A silent fallthrough used to leave the whole block in the body as a paragraph, so the
77
+ * failure only surfaced to readers of the published document.
78
+ */
79
+ export declare class MarkdownFrontmatterError extends Error {
80
+ constructor(message: string);
81
+ }
82
+ export type ParseMarkdownOptions = {
83
+ /**
84
+ * `true` (default) throws {@link MarkdownFrontmatterError} on invalid frontmatter YAML.
85
+ * `false` keeps the legacy behaviour of treating the block as body text, for display
86
+ * paths that must render whatever they are given.
87
+ */
88
+ strictFrontmatter?: boolean;
89
+ };
90
+ export declare function parseFrontmatter(markdown: string, options?: ParseMarkdownOptions): {
75
91
  content: string;
76
92
  metadata: HMMetadata;
77
93
  };
@@ -82,7 +98,7 @@ export declare function parseFrontmatter(markdown: string): {
82
98
  * metadata and stripped from the content before tokenizing. Block IDs from
83
99
  * `<!-- id:… -->` comments are preserved; absent ids are generated.
84
100
  */
85
- export declare function parseMarkdown(markdown: string): {
101
+ export declare function parseMarkdown(markdown: string, options?: ParseMarkdownOptions): {
86
102
  tree: BlockNode[];
87
103
  metadata: HMMetadata;
88
104
  };
@@ -1,4 +1,5 @@
1
1
  import {
2
+ MarkdownFrontmatterError,
2
3
  flattenToOperations,
3
4
  hmAccountMentionUid,
4
5
  markdownBlockNodesToHMBlockNodes,
@@ -6,9 +7,10 @@ import {
6
7
  parseFrontmatter,
7
8
  parseInlineFormatting,
8
9
  parseMarkdown
9
- } from "./chunk-K5NYTWBJ.mjs";
10
+ } from "./chunk-KKKXGVKJ.mjs";
10
11
  import "./chunk-7P6ASYW6.mjs";
11
12
  export {
13
+ MarkdownFrontmatterError,
12
14
  flattenToOperations,
13
15
  hmAccountMentionUid,
14
16
  markdownBlockNodesToHMBlockNodes,
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Loads mermaid's parser for {@link assertValidMermaid} in whatever runtime this runs in.
3
+ *
4
+ * Kept apart from `mermaid-validation.ts` so that importing the validator never pulls mermaid
5
+ * into a bundle. `mermaid` is a peer the caller installs; outside a browser a DOM shim is needed
6
+ * too (mermaid's sanitizer initialises against `window.document`), and `linkedom` is used when
7
+ * present. Both are imported dynamically with computed specifiers so bundlers leave them alone.
8
+ */
9
+ import type { MermaidParser } from './mermaid-validation';
10
+ /**
11
+ * Returns a parser that rejects with mermaid's syntax error for invalid diagrams. The import is
12
+ * done once and shared; call sites pass the result to `assertValidMermaid`.
13
+ */
14
+ export declare function loadMermaidParser(): Promise<MermaidParser>;
@@ -0,0 +1,38 @@
1
+ import "./chunk-7P6ASYW6.mjs";
2
+
3
+ // src/mermaid-parser.ts
4
+ var loader;
5
+ async function installDomShim() {
6
+ const g = globalThis;
7
+ if (typeof g.document !== "undefined") return;
8
+ const specifier = "linkedom";
9
+ const { parseHTML } = await import(
10
+ /* @vite-ignore */
11
+ specifier
12
+ );
13
+ const dom = parseHTML("<!doctype html><html><body></body></html>");
14
+ g.window = dom.window;
15
+ g.document = dom.document;
16
+ g.DOMParser = dom.DOMParser;
17
+ if (typeof g.navigator === "undefined") g.navigator = { userAgent: "node" };
18
+ }
19
+ function loadMermaidParser() {
20
+ if (!loader) {
21
+ loader = (async () => {
22
+ await installDomShim();
23
+ const specifier = "mermaid";
24
+ const mod = await import(
25
+ /* @vite-ignore */
26
+ specifier
27
+ );
28
+ return (source) => mod.default.parse(source);
29
+ })();
30
+ loader.catch(() => {
31
+ loader = void 0;
32
+ });
33
+ }
34
+ return loader;
35
+ }
36
+ export {
37
+ loadMermaidParser
38
+ };
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Mermaid diagram validation for Code blocks.
3
+ *
4
+ * A ```mermaid fence publishes as a Code block with `language: "mermaid"`. The viewer renders it
5
+ * with mermaid at read time, so a syntax error written today is only discovered by whoever opens
6
+ * the document. Writers (CLI, agents runtime, SDK users) call {@link assertValidMermaid} before
7
+ * publishing so the error lands on the author instead.
8
+ *
9
+ * This module has no dependency on mermaid itself: the parser is injected, because loading it
10
+ * differs per runtime (a browser has a DOM; Node and Bun need a shim). See `mermaid-parser.ts`
11
+ * for the loader used by the CLI and the agents runtime.
12
+ */
13
+ /** Resolves when the source parses; rejects with mermaid's syntax error otherwise. */
14
+ export type MermaidParser = (source: string) => Promise<unknown>;
15
+ export type MermaidDiagramError = {
16
+ blockId: string;
17
+ message: string;
18
+ };
19
+ /** The subset of a block node this module reads; both the parser tree and the API shape satisfy it. */
20
+ export type MermaidBlockLike = {
21
+ block: {
22
+ id?: string;
23
+ type: string;
24
+ text?: string;
25
+ language?: string;
26
+ attributes?: {
27
+ language?: string;
28
+ } | Record<string, unknown>;
29
+ };
30
+ children?: MermaidBlockLike[];
31
+ };
32
+ export declare class InvalidMermaidError extends Error {
33
+ readonly errors: MermaidDiagramError[];
34
+ constructor(errors: MermaidDiagramError[]);
35
+ }
36
+ /** Every Code block whose language is `mermaid`, in document order, with its source text. */
37
+ export declare function collectMermaidBlocks(nodes: MermaidBlockLike[]): Array<{
38
+ blockId: string;
39
+ source: string;
40
+ }>;
41
+ /** Parses every mermaid block with `parse` and returns the failures (empty when all are valid). */
42
+ export declare function validateMermaidBlocks(nodes: MermaidBlockLike[], parse: MermaidParser): Promise<MermaidDiagramError[]>;
43
+ /** Throws {@link InvalidMermaidError} when any mermaid block in `nodes` fails to parse. */
44
+ export declare function assertValidMermaid(nodes: MermaidBlockLike[], parse: MermaidParser): Promise<void>;
@@ -0,0 +1,13 @@
1
+ import {
2
+ InvalidMermaidError,
3
+ assertValidMermaid,
4
+ collectMermaidBlocks,
5
+ validateMermaidBlocks
6
+ } from "./chunk-TGCGGVEU.mjs";
7
+ import "./chunk-7P6ASYW6.mjs";
8
+ export {
9
+ InvalidMermaidError,
10
+ assertValidMermaid,
11
+ collectMermaidBlocks,
12
+ validateMermaidBlocks
13
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seed-hypermedia/client",
3
- "version": "0.0.104",
3
+ "version": "0.0.105",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/seed-hypermedia/seed",