@blogmcp/render-embed 0.7.1 → 0.8.1

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/parse.d.ts CHANGED
@@ -38,11 +38,10 @@ export declare function createDefaultMarked(): Marked;
38
38
  */
39
39
  export { computeBodyOffset };
40
40
  /**
41
- * Split a markdown span into top-level BLOCK units, each with its source range within `src`. Uses
42
- * marked's block lexer (whose token `raw` values concatenate back to the input); GUARDS on that
43
- * exact reconstruction (e.g. CRLF normalization breaks it) → returns `[]` so the caller falls back
44
- * to one whole-span block and a splice can never write the wrong byte range. Each range is trimmed
45
- * of trailing whitespace (editable slice = block text; the full token length still advances offset).
41
+ * Split a markdown span into top-level BLOCK units, each with its source range within `src`. The
42
+ * split itself is mdx-schema's `splitMarkdownTokens` (refuses with `[]` when the tokens do not
43
+ * reconstruct the input exactly, e.g. CRLF), so the caller falls back to one whole-span block and a
44
+ * splice can never write the wrong byte range. Each range is trimmed of trailing whitespace.
46
45
  */
47
46
  export declare function splitMarkdownBlocks(src: string, marked: Marked): Array<{
48
47
  html: string;
@@ -51,5 +50,9 @@ export declare function splitMarkdownBlocks(src: string, marked: Marked): Array<
51
50
  /**
52
51
  * Parse article source into a `RenderUnit[]` (block-level html units + MDX component nodes), plus
53
52
  * the post-frontmatter `body` and its `bodyOffset` within `content`.
53
+ *
54
+ * The split and the `mdx-N` ids come from mdx-schema's `planArticleUnits`, which the page-review
55
+ * layer uses too; this function only renders the markdown units. With no units (a blank body, or
56
+ * nothing splittable) the whole body is rendered as one `html` string instead.
54
57
  */
55
58
  export declare function parseArticle(content: string, marked?: Marked): ParsedArticle;
package/dist/parse.js CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import { Marked } from 'marked';
11
11
  import { sanitizeHtml, sanitizeUrl } from '@blogmcp/sanitize';
12
- import { stripFrontmatter, HAS_MDX_RE, scanMdxNodes, computeBodyOffset, } from '@blogmcp/mdx-schema/composables/parsing';
12
+ import { computeBodyOffset, planArticleUnits, splitMarkdownTokens, } from '@blogmcp/mdx-schema/composables/parsing';
13
13
  /**
14
14
  * Detects raw HTML in a markdown SOURCE — the fast-path-skip guard in `renderMarkdownSafe`.
15
15
  * Matches `<`+letter (tag), `<!` (comment/doctype), `</` (closing); does NOT match bare `<` in
@@ -66,113 +66,38 @@ function renderMarkdownSafe(src, marked) {
66
66
  * produces the offsets it is added to.
67
67
  */
68
68
  export { computeBodyOffset };
69
+ /** marked as a `PlanLexer`: its block lexer, whose token `raw` values concatenate back to the input. */
70
+ function lexWith(marked) {
71
+ return (src) => marked.lexer(src);
72
+ }
69
73
  /**
70
- * Split a markdown span into top-level BLOCK units, each with its source range within `src`. Uses
71
- * marked's block lexer (whose token `raw` values concatenate back to the input); GUARDS on that
72
- * exact reconstruction (e.g. CRLF normalization breaks it) → returns `[]` so the caller falls back
73
- * to one whole-span block and a splice can never write the wrong byte range. Each range is trimmed
74
- * of trailing whitespace (editable slice = block text; the full token length still advances offset).
74
+ * Split a markdown span into top-level BLOCK units, each with its source range within `src`. The
75
+ * split itself is mdx-schema's `splitMarkdownTokens` (refuses with `[]` when the tokens do not
76
+ * reconstruct the input exactly, e.g. CRLF), so the caller falls back to one whole-span block and a
77
+ * splice can never write the wrong byte range. Each range is trimmed of trailing whitespace.
75
78
  */
76
79
  export function splitMarkdownBlocks(src, marked) {
77
- let tokens;
78
- try {
79
- tokens = marked.lexer(src);
80
- }
81
- catch {
82
- return [];
83
- }
84
- if (tokens.map((t) => t.raw).join('') !== src)
85
- return [];
86
- const blocks = [];
87
- let offset = 0;
88
- for (const token of tokens) {
89
- const start = offset;
90
- offset += token.raw.length;
91
- if (token.type === 'space' || !token.raw.trim())
92
- continue;
93
- const trimmedLen = token.raw.replace(/\s+$/, '').length;
94
- blocks.push({
95
- html: renderMarkdownSafe(token.raw, marked),
96
- sourceRange: { start, end: start + trimmedLen },
97
- });
98
- }
99
- return blocks;
80
+ return splitMarkdownTokens(src, lexWith(marked)).map((b) => ({
81
+ html: renderMarkdownSafe(b.token.raw, marked),
82
+ sourceRange: b.sourceRange,
83
+ }));
100
84
  }
101
85
  /**
102
86
  * Parse article source into a `RenderUnit[]` (block-level html units + MDX component nodes), plus
103
87
  * the post-frontmatter `body` and its `bodyOffset` within `content`.
88
+ *
89
+ * The split and the `mdx-N` ids come from mdx-schema's `planArticleUnits`, which the page-review
90
+ * layer uses too; this function only renders the markdown units. With no units (a blank body, or
91
+ * nothing splittable) the whole body is rendered as one `html` string instead.
104
92
  */
105
93
  export function parseArticle(content, marked = createDefaultMarked()) {
106
94
  if (!content)
107
95
  return { mode: 'html', html: '', items: [], body: '', bodyOffset: 0 };
108
- const body = stripFrontmatter(content);
109
- const bodyOffset = computeBodyOffset(content);
110
- const items = [];
111
- let nodeIndex = 0;
112
- const nextId = () => `mdx-${nodeIndex++}`;
113
- const pushMarkdownBlocks = (md, base) => {
114
- if (!md.trim())
115
- return;
116
- const blocks = splitMarkdownBlocks(md, marked);
117
- if (blocks.length > 0) {
118
- for (const b of blocks) {
119
- items.push({
120
- type: 'html',
121
- html: b.html,
122
- nodeId: nextId(),
123
- sourceRange: { start: base + b.sourceRange.start, end: base + b.sourceRange.end },
124
- });
125
- }
126
- }
127
- else {
128
- items.push({
129
- type: 'html',
130
- html: renderMarkdownSafe(md, marked),
131
- nodeId: nextId(),
132
- sourceRange: { start: base, end: base + md.length },
133
- });
134
- }
135
- };
136
- // Fast path: pure markdown.
137
- if (!HAS_MDX_RE.test(body)) {
138
- pushMarkdownBlocks(body, 0);
139
- if (items.length > 0)
140
- return { mode: 'html', html: '', items, body, bodyOffset };
141
- return { mode: 'html', html: renderMarkdownSafe(body, marked), items: [], body, bodyOffset };
142
- }
143
- // MDX path: markdown chunks (themselves block-split) interleaved with component nodes.
144
- //
145
- // `scanMdxNodes` replaces a local MDX_COMPONENT_PATTERN walk that carried both scanner bugs
146
- // independently — a prose `<Gallery>` mention ran to the next '/>' anywhere in the document, and a
147
- // component shown inside a ``` fence became a live component node with a `sourceRange` an editor
148
- // could then splice. See the @deprecated block on MDX_COMPONENT_PATTERN for the measurements.
149
- //
150
- // ⚠ Only the COMPONENT nodes are consumed. The gaps keep going through this file's own
151
- // `pushMarkdownBlocks`, deliberately: it block-splits with a different whitespace policy, and
152
- // consuming the walk's `html` nodes instead would renumber `mdx-N` and shift `sourceRange` for
153
- // EVERY article, not just the fence-bearing ones — silently invalidating anchors that already
154
- // exist in stored review comments.
155
- let lastIndex = 0;
156
- for (const node of scanMdxNodes(body)) {
157
- if (node.type !== 'component')
158
- continue;
159
- if (node.start > lastIndex) {
160
- pushMarkdownBlocks(body.slice(lastIndex, node.start), lastIndex);
161
- }
162
- items.push({
163
- type: 'component',
164
- name: node.name,
165
- props: node.props,
166
- children: node.children,
167
- nodeId: nextId(),
168
- sourceRange: { start: node.start, end: node.end },
169
- });
170
- lastIndex = node.end;
171
- }
172
- if (lastIndex < body.length) {
173
- pushMarkdownBlocks(body.slice(lastIndex), lastIndex);
174
- }
175
- if (items.length === 0 && body.trim()) {
96
+ const { body, bodyOffset, units } = planArticleUnits(content, lexWith(marked));
97
+ const items = units.map((u) => u.kind === 'component'
98
+ ? { type: 'component', name: u.name, props: u.props, children: u.children, nodeId: u.nodeId, sourceRange: u.sourceRange }
99
+ : { type: 'html', html: renderMarkdownSafe(u.source, marked), nodeId: u.nodeId, sourceRange: u.sourceRange });
100
+ if (items.length === 0) {
176
101
  return { mode: 'html', html: renderMarkdownSafe(body, marked), items: [], body, bodyOffset };
177
102
  }
178
103
  const hasComponent = items.some((it) => it.type === 'component');
package/dist/parse.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EACL,gBAA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AM,EAAE,CAAC,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE;QACzH,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB,CAAC,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC,WAAW,EAAE,CAC/G,CAAA;IAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,CAAA;IAC9F,CAAC;IACD,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,KAAK,WAAW,CAAC,CAAA;IAChE,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,CAAA;AACnF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blogmcp/render-embed",
3
- "version": "0.7.1",
3
+ "version": "0.8.1",
4
4
  "description": "Framework-agnostic render-embed protocol + parse for the headless-renderer review overlay (host + renderer postMessage bridges).",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -57,8 +57,8 @@
57
57
  "postpack": "node ../../scripts/pack-dist-types.mjs postpack"
58
58
  },
59
59
  "dependencies": {
60
- "@blogmcp/mdx-schema": "^0.7.1",
61
- "@blogmcp/sanitize": "^0.7.1",
60
+ "@blogmcp/mdx-schema": "^0.8.1",
61
+ "@blogmcp/sanitize": "^0.8.1",
62
62
  "marked": "^15.0.7",
63
63
  "zod": "^3.23.8"
64
64
  },
package/src/parse.ts CHANGED
@@ -11,10 +11,11 @@
11
11
  import { Marked } from 'marked'
12
12
  import { sanitizeHtml, sanitizeUrl } from '@blogmcp/sanitize'
13
13
  import {
14
- stripFrontmatter,
15
- HAS_MDX_RE,
16
- scanMdxNodes,
17
14
  computeBodyOffset,
15
+ planArticleUnits,
16
+ splitMarkdownTokens,
17
+ type PlanLexer,
18
+ type PlanLexToken,
18
19
  } from '@blogmcp/mdx-schema/composables/parsing'
19
20
  import type { ParsedArticle, RenderUnit, SourceRange } from './types.js'
20
21
 
@@ -77,120 +78,48 @@ function renderMarkdownSafe(src: string, marked: Marked): string {
77
78
  */
78
79
  export { computeBodyOffset }
79
80
 
81
+ /** marked as a `PlanLexer`: its block lexer, whose token `raw` values concatenate back to the input. */
82
+ function lexWith(marked: Marked): PlanLexer {
83
+ return (src) => marked.lexer(src) as unknown as PlanLexToken[]
84
+ }
85
+
80
86
  /**
81
- * Split a markdown span into top-level BLOCK units, each with its source range within `src`. Uses
82
- * marked's block lexer (whose token `raw` values concatenate back to the input); GUARDS on that
83
- * exact reconstruction (e.g. CRLF normalization breaks it) → returns `[]` so the caller falls back
84
- * to one whole-span block and a splice can never write the wrong byte range. Each range is trimmed
85
- * of trailing whitespace (editable slice = block text; the full token length still advances offset).
87
+ * Split a markdown span into top-level BLOCK units, each with its source range within `src`. The
88
+ * split itself is mdx-schema's `splitMarkdownTokens` (refuses with `[]` when the tokens do not
89
+ * reconstruct the input exactly, e.g. CRLF), so the caller falls back to one whole-span block and a
90
+ * splice can never write the wrong byte range. Each range is trimmed of trailing whitespace.
86
91
  */
87
92
  export function splitMarkdownBlocks(
88
93
  src: string,
89
94
  marked: Marked,
90
95
  ): Array<{ html: string; sourceRange: SourceRange }> {
91
- let tokens: Array<{ type: string; raw: string }>
92
- try {
93
- tokens = marked.lexer(src) as unknown as Array<{ type: string; raw: string }>
94
- } catch {
95
- return []
96
- }
97
- if (tokens.map((t) => t.raw).join('') !== src) return []
98
- const blocks: Array<{ html: string; sourceRange: SourceRange }> = []
99
- let offset = 0
100
- for (const token of tokens) {
101
- const start = offset
102
- offset += token.raw.length
103
- if (token.type === 'space' || !token.raw.trim()) continue
104
- const trimmedLen = token.raw.replace(/\s+$/, '').length
105
- blocks.push({
106
- html: renderMarkdownSafe(token.raw, marked),
107
- sourceRange: { start, end: start + trimmedLen },
108
- })
109
- }
110
- return blocks
96
+ return splitMarkdownTokens(src, lexWith(marked)).map((b) => ({
97
+ html: renderMarkdownSafe(b.token.raw, marked),
98
+ sourceRange: b.sourceRange,
99
+ }))
111
100
  }
112
101
 
113
102
  /**
114
103
  * Parse article source into a `RenderUnit[]` (block-level html units + MDX component nodes), plus
115
104
  * the post-frontmatter `body` and its `bodyOffset` within `content`.
105
+ *
106
+ * The split and the `mdx-N` ids come from mdx-schema's `planArticleUnits`, which the page-review
107
+ * layer uses too; this function only renders the markdown units. With no units (a blank body, or
108
+ * nothing splittable) the whole body is rendered as one `html` string instead.
116
109
  */
117
110
  export function parseArticle(content: string, marked: Marked = createDefaultMarked()): ParsedArticle {
118
111
  if (!content) return { mode: 'html', html: '', items: [], body: '', bodyOffset: 0 }
119
112
 
120
- const body = stripFrontmatter(content)
121
- const bodyOffset = computeBodyOffset(content)
122
-
123
- const items: RenderUnit[] = []
124
- let nodeIndex = 0
125
- const nextId = (): string => `mdx-${nodeIndex++}`
126
-
127
- const pushMarkdownBlocks = (md: string, base: number): void => {
128
- if (!md.trim()) return
129
- const blocks = splitMarkdownBlocks(md, marked)
130
- if (blocks.length > 0) {
131
- for (const b of blocks) {
132
- items.push({
133
- type: 'html',
134
- html: b.html,
135
- nodeId: nextId(),
136
- sourceRange: { start: base + b.sourceRange.start, end: base + b.sourceRange.end },
137
- })
138
- }
139
- } else {
140
- items.push({
141
- type: 'html',
142
- html: renderMarkdownSafe(md, marked),
143
- nodeId: nextId(),
144
- sourceRange: { start: base, end: base + md.length },
145
- })
146
- }
147
- }
148
-
149
- // Fast path: pure markdown.
150
- if (!HAS_MDX_RE.test(body)) {
151
- pushMarkdownBlocks(body, 0)
152
- if (items.length > 0) return { mode: 'html', html: '', items, body, bodyOffset }
153
- return { mode: 'html', html: renderMarkdownSafe(body, marked), items: [], body, bodyOffset }
154
- }
113
+ const { body, bodyOffset, units } = planArticleUnits(content, lexWith(marked))
114
+ const items: RenderUnit[] = units.map((u) =>
115
+ u.kind === 'component'
116
+ ? { type: 'component', name: u.name, props: u.props, children: u.children, nodeId: u.nodeId, sourceRange: u.sourceRange }
117
+ : { type: 'html', html: renderMarkdownSafe(u.source, marked), nodeId: u.nodeId, sourceRange: u.sourceRange },
118
+ )
155
119
 
156
- // MDX path: markdown chunks (themselves block-split) interleaved with component nodes.
157
- //
158
- // `scanMdxNodes` replaces a local MDX_COMPONENT_PATTERN walk that carried both scanner bugs
159
- // independently — a prose `<Gallery>` mention ran to the next '/>' anywhere in the document, and a
160
- // component shown inside a ``` fence became a live component node with a `sourceRange` an editor
161
- // could then splice. See the @deprecated block on MDX_COMPONENT_PATTERN for the measurements.
162
- //
163
- // ⚠ Only the COMPONENT nodes are consumed. The gaps keep going through this file's own
164
- // `pushMarkdownBlocks`, deliberately: it block-splits with a different whitespace policy, and
165
- // consuming the walk's `html` nodes instead would renumber `mdx-N` and shift `sourceRange` for
166
- // EVERY article, not just the fence-bearing ones — silently invalidating anchors that already
167
- // exist in stored review comments.
168
- let lastIndex = 0
169
-
170
- for (const node of scanMdxNodes(body)) {
171
- if (node.type !== 'component') continue
172
- if (node.start > lastIndex) {
173
- pushMarkdownBlocks(body.slice(lastIndex, node.start), lastIndex)
174
- }
175
- items.push({
176
- type: 'component',
177
- name: node.name,
178
- props: node.props,
179
- children: node.children,
180
- nodeId: nextId(),
181
- sourceRange: { start: node.start, end: node.end },
182
- })
183
- lastIndex = node.end
184
- }
185
-
186
- if (lastIndex < body.length) {
187
- pushMarkdownBlocks(body.slice(lastIndex), lastIndex)
188
- }
189
-
190
- if (items.length === 0 && body.trim()) {
120
+ if (items.length === 0) {
191
121
  return { mode: 'html', html: renderMarkdownSafe(body, marked), items: [], body, bodyOffset }
192
122
  }
193
-
194
123
  const hasComponent = items.some((it) => it.type === 'component')
195
124
  return { mode: hasComponent ? 'mdx' : 'html', html: '', items, body, bodyOffset }
196
125
  }