@blogmcp/mdx-schema 0.2.1 → 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.
@@ -6,7 +6,13 @@
6
6
  */
7
7
 
8
8
  import type { ComponentMetadata } from '../types/index.js'
9
- import { extractMdxComponents, stripFrontmatter, HAS_MDX_RE } from '../composables/useMdxParsing.js'
9
+ import {
10
+ scanMdxNodes,
11
+ stripFrontmatter,
12
+ computeBodyOffset,
13
+ lineAtOffset,
14
+ HAS_MDX_RE,
15
+ } from '../composables/useMdxParsing.js'
10
16
  import { getComponentMetadata, getComponentNames } from './index.js'
11
17
 
12
18
  export { HAS_MDX_RE }
@@ -115,26 +121,56 @@ export function validateComponent(
115
121
 
116
122
  /**
117
123
  * Validate all MDX components in markdown content.
124
+ *
125
+ * The reported `line` is a FILE coordinate (frontmatter included), derived from the OFFENDING
126
+ * instance's own scan offset. It replaces a `content.indexOf('<' + name)` lookup that had two
127
+ * independent defects, both measured on the shipped docs corpus:
128
+ *
129
+ * 1. It reported the FIRST occurrence of that component name for EVERY instance — 29 of 69
130
+ * extracted components got a wrong line (42%), worst delta +308 lines. `features/media-audio.md`
131
+ * reported "line 13" for seven different AudioPlayer instances, and `features/editor-mcp.md`
132
+ * blamed line 194 for an error that belongs to the `<Gallery .../>` on line 251.
133
+ * 2. It searched the ORIGINAL content while extraction ran on the frontmatter-STRIPPED copy, so a
134
+ * component name merely MENTIONED in frontmatter hijacked every reported line.
135
+ *
136
+ * `computeBodyOffset` (not `content.length - stripped.length`) is the correct base — the delta is
137
+ * wrong because `stripFrontmatter` trims BOTH ends. This line is what
138
+ * `ArticleComponentValidationError` interpolates, so it is the message an author must act on: a
139
+ * wrong one turns a true error into a half-hour hunt for a false positive.
140
+ *
141
+ * `line` is now always >= 1; the old `return 0` "not found" sentinel is gone — its consumers all
142
+ * interpolate the value into human-readable messages and none test for 0.
118
143
  */
119
144
  export function validateMarkdownComponents(
120
145
  content: string,
121
146
  options?: ValidateOptions,
122
147
  ): ContentValidationResult {
123
148
  const stripped = stripFrontmatter(content)
124
- const parsed = extractMdxComponents(stripped)
149
+ const bodyOffset = computeBodyOffset(content)
150
+ const nodes = scanMdxNodes(stripped)
125
151
  const results: ComponentValidationResult[] = []
126
152
 
127
- for (const item of parsed) {
128
- if (item.type !== 'component') continue
153
+ // ⚠ ONE monotonic cursor, not `lineAtOffset` per node. `scanMdxNodes` emits nodes in ascending
154
+ // `start`, so the newlines before node N are a prefix of those before node N+1 — counting from 0
155
+ // for every node makes this O(components × offset). Measured on legitimate content: 500 → 8000
156
+ // components (16×) took 20ms → 4397ms (220×), i.e. seconds of blocking CPU on the authenticated
157
+ // save path, while the scan itself stayed at 12ms. That was also strictly WORSE than the
158
+ // `indexOf`-based lookup this replaced, which would have made the correctness fix a performance
159
+ // regression.
160
+ let cursor = 0
161
+ let cursorLine = 1
129
162
 
130
- // Find the line number of this component in the original content
131
- const componentTag = `<${item.name}`
132
- const line = findLineNumber(content, componentTag)
163
+ for (const node of nodes) {
164
+ if (node.type !== 'component') continue
133
165
 
134
- const validation = validateComponent(item.name, item.props, item.children, options)
166
+ const target = Math.min(bodyOffset + node.start, content.length)
167
+ for (; cursor < target; cursor++) if (content[cursor] === '\n') cursorLine++
168
+ const line = cursorLine
169
+
170
+ const validation = validateComponent(node.name, node.props, node.children, options)
135
171
  if (!validation.valid || validation.warnings.length > 0) {
136
172
  results.push({
137
- name: item.name,
173
+ name: node.name,
138
174
  line,
139
175
  errors: validation.errors,
140
176
  warnings: validation.warnings,
@@ -145,9 +181,3 @@ export function validateMarkdownComponents(
145
181
  const valid = results.every(r => r.errors.length === 0)
146
182
  return { valid, components: results }
147
183
  }
148
-
149
- function findLineNumber(content: string, search: string): number {
150
- const index = content.indexOf(search)
151
- if (index === -1) return 0
152
- return content.substring(0, index).split('\n').length
153
- }