@blogmcp/mdx-schema 0.2.2 → 0.3.0

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.
@@ -18,6 +18,38 @@ export declare const HAS_MDX_RE: RegExp;
18
18
  * - Self-closing: match[4]=name, match[5]=props
19
19
  *
20
20
  * NOTE: Create a new RegExp instance per call (g-flag + exec requires fresh lastIndex).
21
+ *
22
+ * @deprecated MATCHING ONLY — never build a document WALK on this. It cannot express either
23
+ * half of correct extraction, and both gaps caused measured production damage:
24
+ *
25
+ * - NO PROP BOUNDARY. The self-closing branch's props group `(?:[^/]|\/(?!>))*` matches '>'
26
+ * and newlines, so ANY `<Name>` — including a prose mention — starts a match that runs to
27
+ * the next '/>' anywhere in the document. Measured on `docs/public/tools/upload-media.md`:
28
+ * ONE match spanning lines 25→166 (142 lines, 8916 chars, 1362 whitespace-separated "prop"
29
+ * tokens), which SWALLOWS everything it spans — 81.7% of that page's body never reached the
30
+ * renderer.
31
+ * - NO CODE AWARENESS. A component inside a fence (all 4 flavours: bare ```, ```lang,
32
+ * 1-3-space-indented, and ~~~) or inside an inline `code` span is documentation ABOUT the
33
+ * component, and was extracted as a LIVE one. Both the inline SELF-CLOSING form
34
+ * (`` `<Gallery images={[1]} />` ``) and the inline PAIRED form (`` `<Callout>x</Callout>` ``)
35
+ * extracted. A bare `` `<Gallery>` `` mention was ignored only because it has no closer — so
36
+ * "inline spans are already respected" was never true.
37
+ * - The paired branch is not a safe reference to mirror either: `([^>]*)` stops at the first '>'
38
+ * even inside a quoted value (`<Callout title="a > b">Hi</Callout>` → children `b">\nHi`) and
39
+ * eats the trailing '/' of a self-closing tag, absorbing it as paired whenever a later
40
+ * `</Name>` exists (`docs/public/concepts/mdx-storage-format.md:138` swallowed 4 real
41
+ * components that way, across 57 lines).
42
+ *
43
+ * Neither gap is expressible as a regex (nested braces need recursion; code masking needs a
44
+ * block-structure pre-pass), and a post-filter over these matches cannot recover a component a
45
+ * runaway match already consumed — `exec` has advanced past it. The tempting "just mirror the
46
+ * paired branch" tighten to `([^>]*)` is WORSE THAN THE BUG: it passes the entire existing test
47
+ * suite while silently deleting real committed content — `upload-media.md:166-167`
48
+ * (`<Gallery images={[{ src: "<url>", … }]} />`, `<AudioPlayer slug="<slug>" />`) match ZERO
49
+ * times under it.
50
+ *
51
+ * Use {@link scanMdxNodes} (offsets) or {@link extractMdxComponents} (no offsets). Still exported
52
+ * only so already-published consumers keep compiling.
21
53
  */
22
54
  export declare const MDX_COMPONENT_PATTERN: RegExp;
23
55
  /**
@@ -61,10 +93,133 @@ export declare function stripFrontmatter(source: string): string;
61
93
  * Returns the frontmatter block (without delimiters) or null if none.
62
94
  */
63
95
  export declare function extractFrontmatter(source: string): string | null;
96
+ /**
97
+ * Exact start offset of the `stripFrontmatter` result within `content`.
98
+ *
99
+ * Mirrors `stripFrontmatter` exactly (substring after the closing `---`, then `.trim()`), so an
100
+ * offset produced by scanning the STRIPPED body can be mapped back onto full-article coordinates.
101
+ *
102
+ * ⚠ Do NOT derive this as `content.length - stripped.length` — `stripFrontmatter` trims BOTH
103
+ * ends, so that delta is not the prefix length. That confusion is half of the line-number defect
104
+ * this exists to fix: the validator searched the ORIGINAL content while extraction ran on the
105
+ * STRIPPED copy, so the two offset bases silently differed and a component name merely MENTIONED
106
+ * in frontmatter hijacked every reported line.
107
+ *
108
+ * KNOWN, PRE-EXISTING: a document opening with a `---` THEMATIC BREAK is still misread as
109
+ * frontmatter. This function faithfully mirrors that bug so offsets stay CONSISTENT with
110
+ * `stripFrontmatter`; fixing it is a separate change to `stripFrontmatter` itself.
111
+ */
112
+ export declare function computeBodyOffset(content: string): number;
113
+ /** 1-based line number containing `offset` within `content`. */
114
+ export declare function lineAtOffset(content: string, offset: number): number;
115
+ /** `computeCodeMask` flag: this offset is ordinary markup/prose. */
116
+ export declare const CODE_NONE = 0;
117
+ /**
118
+ * `computeCodeMask` flag: this offset is inside a FENCED code block — block-level structure,
119
+ * therefore HIGH confidence. A tag whose own boundary lands in one is malformed and is rejected.
120
+ */
121
+ export declare const CODE_FENCE = 1;
122
+ /**
123
+ * `computeCodeMask` flag: this offset is inside an INLINE code span — AMBIGUOUS, therefore lower
124
+ * confidence. A lone backtick inside a prop value (`title="the \` char"`) can pair with a genuine
125
+ * span later on the same line and mask the tag's own '>' or its closing tag. Inline masking may
126
+ * therefore hide a tag START, but must never invalidate a tag BOUNDARY (see `scanOpenTag`'s caller)
127
+ * and must never be the sole reason a paired tag loses its closer (see `findClosingTag`, which
128
+ * falls back to a masked closer rather than dropping the component).
129
+ */
130
+ export declare const CODE_INLINE = 2;
131
+ /**
132
+ * Flag every offset in `source` that sits inside a code region. `mask[i] !== CODE_NONE` means
133
+ * "offset i is code, not markup". Always exactly `source.length` long.
134
+ *
135
+ * ⚠ A PARALLEL FLAG ARRAY, NOT A MASKED COPY OF THE STRING. This is the one decision in this file
136
+ * that must never be "simplified" later. The obvious implementation — build a masked string and
137
+ * scan that — destroys content, because every slice the scanner emits (props, children, html
138
+ * chunks) would then come from the masked copy: a `<CodeBlock language="md">` wrapping a fenced
139
+ * sample would emit `children` of pure filler, and `title="Use \`code\` inline"` would become
140
+ * `title="Use ______ inline"`. With a flag array there is no second string to slice from by
141
+ * accident, index alignment is free, and the validator's offsets are exact by construction.
142
+ *
143
+ * WHY it exists at all (measured): the scanner had no notion of code, so 100% of the component
144
+ * instances in the entire shipped docs corpus — 49 fenced + 21 inline, ZERO in prose — were
145
+ * extracted as live components. All 4 fence flavours are affected (bare ```, ```lang,
146
+ * 1-3-space-indented, ~~~), and BOTH inline forms extract. It also made a component's own
147
+ * documentation unpublishable: `docs/public/features/editor-mcp.md` was rejected with HTTP 400
148
+ * `Gallery (line 194): Missing required prop "images"` purely from a fenced teaching example, and
149
+ * `features/article-feedback.md` — which contains ZERO fences — was blocked by an inline one.
150
+ *
151
+ * Three deliberate divergences from CommonMark, each chosen because the alternative loses
152
+ * legitimate content. Every one fails OPEN (toward extraction): a false negative re-renders a code
153
+ * sample as a live component — visible and reportable; a false positive DELETES a working
154
+ * component from a published page.
155
+ *
156
+ * 1. An UNTERMINATED fence masks NOTHING, and the scan RESUMES on the next line so a LATER
157
+ * well-formed fence in the same document is still masked. CommonMark extends an unclosed fence
158
+ * to end-of-document; honouring that drops every component after a stray fence, and this is not
159
+ * hypothetical — `docs/public/features/media-picker.md:204` opens a ```json fence it never
160
+ * closes. It would also break the extract → serialize → re-extract roundtrip for
161
+ * `<CodeBlock language="md">\n```\n</CodeBlock>`.
162
+ * 2. Inline spans are SINGLE-LINE and run-length matched (a run of N closes only on exactly N).
163
+ * CommonMark would pair a stray backtick with one on a later line and swallow a real component
164
+ * in between.
165
+ * 3. Four-space INDENTED code blocks are NOT masked. They are the only construct where masking is
166
+ * actively harmful: the continuation lines of a legitimate multi-line component are themselves
167
+ * indented >=4, so masking them would leave the tag matching with its props EMPTIED, turning
168
+ * valid content into a save-blocking `Missing required prop "images"`. A false negative beats a
169
+ * false error. (Blockquoted fences and fences indented >=4 inside list items are likewise not
170
+ * detected — same direction, same reason.)
171
+ */
172
+ export declare function computeCodeMask(source: string): Uint8Array;
173
+ /** An html span between components, with exact offsets into the scanned string. */
174
+ export interface MdxScanHtmlNode {
175
+ type: 'html';
176
+ /** Verbatim slice of the scanned string (never trimmed). */
177
+ content: string;
178
+ start: number;
179
+ end: number;
180
+ }
181
+ /** One component instance, with exact offsets into the scanned string. */
182
+ export interface MdxScanComponentNode {
183
+ type: 'component';
184
+ name: string;
185
+ /** Raw props source, verbatim — already parsed into `props`, kept for splices and debugging. */
186
+ propsSource: string;
187
+ props: Record<string, unknown>;
188
+ /** Trimmed inner source; `undefined` for a self-closing tag, `''` for `<X></X>`. */
189
+ children?: string;
190
+ /** Offset of '<'. */
191
+ start: number;
192
+ /** Offset just past '/>' or '</Name>'. */
193
+ end: number;
194
+ }
195
+ export type MdxScanNode = MdxScanHtmlNode | MdxScanComponentNode;
196
+ /**
197
+ * THE canonical MDX split — single-pass, code-aware, brace/quote-aware, offset-bearing.
198
+ * `extractMdxComponents` is a thin adapter over it; every other surface should be expressed in
199
+ * terms of it rather than re-walking `MDX_COMPONENT_PATTERN` (several sites did, and therefore
200
+ * carried both bugs independently).
201
+ *
202
+ * Structure is LOCATED on the code mask; every string is SLICED from the ORIGINAL. That split is
203
+ * the whole safety property: masking decides what IS a component, never what a component CONTAINS.
204
+ *
205
+ * Offsets are relative to the string PASSED IN. A caller that scanned a frontmatter-stripped body
206
+ * and needs file coordinates must add `computeBodyOffset(originalContent)`.
207
+ *
208
+ * Nested components are handled by consumers re-scanning `children` (see `checkPin`), so the mask
209
+ * is re-derived per level rather than inherited — a fence inside a component's children must still
210
+ * be a fence when that child text is scanned on its own.
211
+ */
212
+ export declare function scanMdxNodes(content: string): MdxScanNode[];
64
213
  /**
65
214
  * Extract MDX components from content, splitting into html chunks and component definitions.
66
215
  * Does NOT process markdown → HTML (caller handles that).
67
216
  *
217
+ * Thin adapter over `scanMdxNodes`; the offsets are dropped here ON PURPOSE. `ParsedContent` is a
218
+ * PUBLISHED type (headless tenants render off it) and existing specs pin its exact shape with a
219
+ * strict `toEqual`, encoding the html / `children: undefined` / `children: ''` tri-state. Adding
220
+ * offsets would break those specs and force every renderer to handle a field it does not use.
221
+ * Callers that need offsets (validation, render-embed, the review app) call `scanMdxNodes`.
222
+ *
68
223
  * @param content - MDX content (frontmatter should already be stripped)
69
224
  * @returns Array of parsed content items
70
225
  */