@blogmcp/mdx-schema 0.8.1 → 0.9.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/index.d.ts CHANGED
@@ -19,3 +19,4 @@ export * from './styles/index.js';
19
19
  export * from './version.js';
20
20
  export * from './prepareProps.js';
21
21
  export * from './checkPin.js';
22
+ export * from './title.js';
package/dist/index.js CHANGED
@@ -19,4 +19,5 @@ export * from './styles/index.js';
19
19
  export * from './version.js';
20
20
  export * from './prepareProps.js';
21
21
  export * from './checkPin.js';
22
+ export * from './title.js';
22
23
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,wBAAwB,CAAA;AACtC,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,wBAAwB,CAAA;AACtC,cAAc,mBAAmB,CAAA;AACjC,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The article title as plain text, and the title an article body declares.
3
+ *
4
+ * Contract (owner decision 2026-10-10): the article BODY is the single source of truth for the
5
+ * title. When the body has a level-1 heading, kb-api stores that heading's plain text as the
6
+ * translation's `title` on every content write; the title column then serves only places that do
7
+ * not render Markdown (`<title>`, meta/og/twitter, JSON-LD, cards, search). Those places print the
8
+ * title as text, so it has to be plain text — `Outkovka pestrá (*Trametes versicolor*)` showed its
9
+ * asterisks in the browser tab before this existed.
10
+ *
11
+ * Dependency-free on purpose: kb-api (write path), `@blogmcp/sdk` (SEO/cards) and md-editor all
12
+ * call these two functions, so they agree by construction instead of by three regexes.
13
+ */
14
+ /**
15
+ * Inline Markdown/MDX → plain text, for every place that prints a title as text.
16
+ *
17
+ * Emphasis, strong, strikethrough, code spans (their text is kept), links and images (their text /
18
+ * alt is kept), inline HTML and JSX tags (removed, text between them kept), backslash escapes and
19
+ * the common entities. Whitespace is collapsed and trimmed. Text that is not Markdown is returned
20
+ * unchanged apart from whitespace, so the function is safe to apply to any stored title.
21
+ */
22
+ export declare function inlineMarkdownToPlainText(input: string | null | undefined): string;
23
+ /**
24
+ * The plain text of the FIRST level-1 heading of an article body, or null when it has none.
25
+ *
26
+ * ATX (`# Title`, optional closing `#`s) and setext (`Title` underlined with `===`). The heading
27
+ * need not be the first line — a draft may open with a note. Skipped: YAML frontmatter, fenced
28
+ * code, indented code, blockquotes (a quoted heading is not the page's heading), HTML comments and
29
+ * multi-line MDX expressions (`{ … }`). `#hashtag` (no space) is not a heading. A heading that is
30
+ * empty after stripping its Markdown does not count.
31
+ */
32
+ export declare function extractTitleHeading(content: string | null | undefined): string | null;
package/dist/title.js ADDED
@@ -0,0 +1,445 @@
1
+ /**
2
+ * The article title as plain text, and the title an article body declares.
3
+ *
4
+ * Contract (owner decision 2026-10-10): the article BODY is the single source of truth for the
5
+ * title. When the body has a level-1 heading, kb-api stores that heading's plain text as the
6
+ * translation's `title` on every content write; the title column then serves only places that do
7
+ * not render Markdown (`<title>`, meta/og/twitter, JSON-LD, cards, search). Those places print the
8
+ * title as text, so it has to be plain text — `Outkovka pestrá (*Trametes versicolor*)` showed its
9
+ * asterisks in the browser tab before this existed.
10
+ *
11
+ * Dependency-free on purpose: kb-api (write path), `@blogmcp/sdk` (SEO/cards) and md-editor all
12
+ * call these two functions, so they agree by construction instead of by three regexes.
13
+ */
14
+ /** Private-use placeholders protect code spans and escapes from the later passes. */
15
+ const PH_BASE = 0xe000;
16
+ const PH_RE = /[-]/g;
17
+ /** ASCII punctuation, as CommonMark defines it for backslash escapes and flanking. */
18
+ const ASCII_PUNCT = new Set('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.split(''));
19
+ const NAMED_ENTITIES = {
20
+ amp: '&',
21
+ lt: '<',
22
+ gt: '>',
23
+ quot: '"',
24
+ apos: "'",
25
+ nbsp: ' ',
26
+ ndash: '–',
27
+ mdash: '—',
28
+ hellip: '…',
29
+ laquo: '«',
30
+ raquo: '»',
31
+ bdquo: '„',
32
+ ldquo: '“',
33
+ rdquo: '”',
34
+ lsquo: '‘',
35
+ rsquo: '’',
36
+ sbquo: '‚',
37
+ copy: '©',
38
+ reg: '®',
39
+ trade: '™',
40
+ times: '×',
41
+ deg: '°',
42
+ middot: '·',
43
+ };
44
+ function decodeEntities(s) {
45
+ return s.replace(/&(#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6}|[A-Za-z][A-Za-z0-9]{1,31});/g, (whole, body) => {
46
+ if (body[0] === '#') {
47
+ const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
48
+ if (!Number.isFinite(code) || code <= 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff))
49
+ return '�';
50
+ return String.fromCodePoint(code);
51
+ }
52
+ return NAMED_ENTITIES[body] ?? whole;
53
+ });
54
+ }
55
+ function isWhitespace(ch) {
56
+ return ch === undefined || /\s/u.test(ch);
57
+ }
58
+ function isPunctuation(ch) {
59
+ if (ch === undefined)
60
+ return false;
61
+ if (ASCII_PUNCT.has(ch))
62
+ return true;
63
+ return /[\p{P}\p{S}]/u.test(ch);
64
+ }
65
+ /**
66
+ * Replace code spans and backslash escapes with placeholders whose literal text is kept in
67
+ * `store`. Run before every other pass, so nothing inside a code span is ever read as markup.
68
+ */
69
+ function protect(s, store) {
70
+ const hold = (text) => {
71
+ store.push(text);
72
+ return String.fromCharCode(PH_BASE + store.length - 1);
73
+ };
74
+ let out = '';
75
+ let i = 0;
76
+ while (i < s.length) {
77
+ const ch = s[i];
78
+ if (ch === '\\' && i + 1 < s.length && ASCII_PUNCT.has(s[i + 1])) {
79
+ out += hold(s[i + 1]);
80
+ i += 2;
81
+ continue;
82
+ }
83
+ if (ch === '`') {
84
+ let run = 0;
85
+ while (s[i + run] === '`')
86
+ run++;
87
+ // Find a closing run of exactly the same length.
88
+ let j = i + run;
89
+ let close = -1;
90
+ while (j < s.length) {
91
+ if (s[j] !== '`') {
92
+ j++;
93
+ continue;
94
+ }
95
+ let r = 0;
96
+ while (s[j + r] === '`')
97
+ r++;
98
+ if (r === run) {
99
+ close = j;
100
+ break;
101
+ }
102
+ j += r;
103
+ }
104
+ if (close === -1) {
105
+ // No closer: the backticks are literal text.
106
+ out += s.slice(i, i + run);
107
+ i += run;
108
+ continue;
109
+ }
110
+ let body = s.slice(i + run, close).replace(/\r?\n/g, ' ');
111
+ // CommonMark: strip ONE leading and trailing space when both exist and the body is not all spaces.
112
+ if (body.length >= 2 && body.startsWith(' ') && body.endsWith(' ') && body.trim() !== '')
113
+ body = body.slice(1, -1);
114
+ out += hold(body);
115
+ i = close + run;
116
+ continue;
117
+ }
118
+ out += ch;
119
+ i++;
120
+ }
121
+ return out;
122
+ }
123
+ function restore(s, store) {
124
+ return s.replace(PH_RE, (c) => store[c.charCodeAt(0) - PH_BASE] ?? '');
125
+ }
126
+ /** Index of the `]` that closes the `[` at `open`, honouring nesting; -1 when none. */
127
+ function closingBracket(s, open) {
128
+ let depth = 0;
129
+ for (let i = open; i < s.length; i++) {
130
+ if (s[i] === '[')
131
+ depth++;
132
+ else if (s[i] === ']') {
133
+ depth--;
134
+ if (depth === 0)
135
+ return i;
136
+ }
137
+ }
138
+ return -1;
139
+ }
140
+ /** Index just past the `)` closing an inline link destination starting at `open` (the `(`); -1 when none. */
141
+ function closingParen(s, open) {
142
+ let depth = 0;
143
+ let i = open;
144
+ // `<…>` destination: anything up to `>`.
145
+ for (; i < s.length; i++) {
146
+ const ch = s[i];
147
+ if (ch === '(')
148
+ depth++;
149
+ else if (ch === ')') {
150
+ depth--;
151
+ if (depth === 0)
152
+ return i + 1;
153
+ }
154
+ else if (ch === '\n' && s[i + 1] === '\n')
155
+ return -1;
156
+ }
157
+ return -1;
158
+ }
159
+ /** `![alt](…)` → alt, `[text](…)` / `[text][ref]` / `[text][]` → text. Shortcut `[text]` stays. */
160
+ function stripLinks(s) {
161
+ let out = '';
162
+ let i = 0;
163
+ while (i < s.length) {
164
+ const isImage = s[i] === '!' && s[i + 1] === '[';
165
+ const start = isImage ? i + 1 : i;
166
+ if (s[start] !== '[') {
167
+ out += s[i];
168
+ i++;
169
+ continue;
170
+ }
171
+ const close = closingBracket(s, start);
172
+ if (close === -1) {
173
+ out += s[i];
174
+ i++;
175
+ continue;
176
+ }
177
+ const text = s.slice(start + 1, close);
178
+ const next = s[close + 1];
179
+ if (next === '(') {
180
+ const end = closingParen(s, close + 1);
181
+ if (end !== -1) {
182
+ out += stripLinks(text);
183
+ i = end;
184
+ continue;
185
+ }
186
+ }
187
+ else if (next === '[') {
188
+ const refClose = s.indexOf(']', close + 2);
189
+ if (refClose !== -1 && !s.slice(close + 2, refClose).includes('[')) {
190
+ out += stripLinks(text);
191
+ i = refClose + 1;
192
+ continue;
193
+ }
194
+ }
195
+ // Not a link we can resolve (shortcut reference or literal brackets): keep as written.
196
+ out += s[i];
197
+ i++;
198
+ }
199
+ return out;
200
+ }
201
+ /** Inline HTML / JSX tags, HTML comments, MDX comments and autolinks. Text between tags is kept. */
202
+ function stripTags(s) {
203
+ return s
204
+ .replace(/<!--[\s\S]*?-->/g, '')
205
+ .replace(/\{\/\*[\s\S]*?\*\/\}/g, '')
206
+ .replace(/<((?:https?|mailto|ftp):[^\s<>]*)>/gi, '$1')
207
+ .replace(/<([^\s<>@]+@[^\s<>@]+\.[^\s<>@]+)>/g, '$1')
208
+ .replace(/<\/?[A-Za-z][A-Za-z0-9.:-]*(?:\s+(?:[^<>"'{}]|"[^"]*"|'[^']*'|\{[^{}]*\})*)?\/?>/g, '');
209
+ }
210
+ /**
211
+ * CommonMark emphasis (`*`, `_`) and GFM strikethrough (`~~`, `~`): matched delimiters are
212
+ * removed, unmatched ones stay as literal text. Flanking rules decide who may open or close, so
213
+ * an intraword underscore (`kb_upsert_tag_translation`) is never emphasis while an intraword
214
+ * asterisk (`un*frigging*believable`) is.
215
+ */
216
+ function stripEmphasis(s) {
217
+ const parts = [];
218
+ const delims = [];
219
+ let buf = '';
220
+ let i = 0;
221
+ while (i < s.length) {
222
+ const ch = s[i];
223
+ if (ch !== '*' && ch !== '_' && ch !== '~') {
224
+ buf += ch;
225
+ i++;
226
+ continue;
227
+ }
228
+ let run = 0;
229
+ while (s[i + run] === ch)
230
+ run++;
231
+ const before = i === 0 ? undefined : s[i - 1];
232
+ const after = s[i + run];
233
+ const leftFlanking = !isWhitespace(after) && (!isPunctuation(after) || isWhitespace(before) || isPunctuation(before));
234
+ const rightFlanking = !isWhitespace(before) && (!isPunctuation(before) || isWhitespace(after) || isPunctuation(after));
235
+ let canOpen = leftFlanking;
236
+ let canClose = rightFlanking;
237
+ if (ch === '_') {
238
+ canOpen = leftFlanking && (!rightFlanking || isPunctuation(before));
239
+ canClose = rightFlanking && (!leftFlanking || isPunctuation(after));
240
+ }
241
+ if (ch === '~' && run > 2) {
242
+ // GFM: three or more tildes never delimit strikethrough.
243
+ canOpen = false;
244
+ canClose = false;
245
+ }
246
+ if (buf)
247
+ parts.push(buf);
248
+ buf = '';
249
+ parts.push(s.slice(i, i + run));
250
+ delims.push({ part: parts.length - 1, ch, count: run, origCount: run, canOpen, canClose });
251
+ i += run;
252
+ }
253
+ if (buf)
254
+ parts.push(buf);
255
+ // Keep left/right halves per delimiter run so partial matches leave the right characters.
256
+ const removeLeft = new Map();
257
+ const removeRight = new Map();
258
+ const active = delims.map(() => true);
259
+ for (let c = 0; c < delims.length; c++) {
260
+ const closer = delims[c];
261
+ if (!closer.canClose)
262
+ continue;
263
+ while (closer.count > 0) {
264
+ let found = -1;
265
+ for (let o = c - 1; o >= 0; o--) {
266
+ const opener = delims[o];
267
+ if (!active[o] || opener.ch !== closer.ch || !opener.canOpen || opener.count === 0)
268
+ continue;
269
+ if (closer.ch === '~') {
270
+ if (opener.count !== closer.count)
271
+ continue;
272
+ }
273
+ else if ((opener.canClose || closer.canOpen) && (opener.origCount + closer.origCount) % 3 === 0 &&
274
+ !(opener.origCount % 3 === 0 && closer.origCount % 3 === 0)) {
275
+ // CommonMark "rule of 3".
276
+ continue;
277
+ }
278
+ found = o;
279
+ break;
280
+ }
281
+ if (found === -1)
282
+ break;
283
+ const opener = delims[found];
284
+ const use = closer.ch === '~' ? closer.count : opener.count >= 2 && closer.count >= 2 ? 2 : 1;
285
+ opener.count -= use;
286
+ closer.count -= use;
287
+ removeRight.set(opener.part, (removeRight.get(opener.part) ?? 0) + use);
288
+ removeLeft.set(closer.part, (removeLeft.get(closer.part) ?? 0) + use);
289
+ // Delimiters between the pair can no longer match anything.
290
+ for (let k = found + 1; k < c; k++)
291
+ active[k] = false;
292
+ if (opener.count === 0)
293
+ active[found] = false;
294
+ }
295
+ if (closer.count === 0)
296
+ active[c] = false;
297
+ }
298
+ const delimByPart = new Map(delims.map((d) => [d.part, d]));
299
+ return parts
300
+ .map((p, idx) => {
301
+ const d = delimByPart.get(idx);
302
+ if (!d)
303
+ return p;
304
+ const left = removeLeft.get(idx) ?? 0;
305
+ const right = removeRight.get(idx) ?? 0;
306
+ return p.slice(left, Math.max(left, p.length - right));
307
+ })
308
+ .join('');
309
+ }
310
+ /**
311
+ * Inline Markdown/MDX → plain text, for every place that prints a title as text.
312
+ *
313
+ * Emphasis, strong, strikethrough, code spans (their text is kept), links and images (their text /
314
+ * alt is kept), inline HTML and JSX tags (removed, text between them kept), backslash escapes and
315
+ * the common entities. Whitespace is collapsed and trimmed. Text that is not Markdown is returned
316
+ * unchanged apart from whitespace, so the function is safe to apply to any stored title.
317
+ */
318
+ export function inlineMarkdownToPlainText(input) {
319
+ if (!input)
320
+ return '';
321
+ const store = [];
322
+ let s = protect(String(input), store);
323
+ s = stripTags(s);
324
+ s = stripLinks(s);
325
+ s = stripEmphasis(s);
326
+ s = restore(s, store);
327
+ s = decodeEntities(s);
328
+ return s.replace(/\s+/g, ' ').trim();
329
+ }
330
+ const ATX_H1_RE = /^ {0,3}#(?:[ \t]+(.*?))?[ \t]*$/;
331
+ const SETEXT_H1_RE = /^ {0,3}=+[ \t]*$/;
332
+ const FENCE_RE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
333
+ /** Frontmatter only when the document opens with a `---` line closed by a later `---`/`...` line. */
334
+ function bodyStartLine(lines) {
335
+ if (lines[0]?.trim() !== '---')
336
+ return 0;
337
+ for (let k = 1; k < lines.length; k++) {
338
+ const t = lines[k].trim();
339
+ if (t === '---' || t === '...')
340
+ return k + 1;
341
+ }
342
+ return 0;
343
+ }
344
+ /**
345
+ * The plain text of the FIRST level-1 heading of an article body, or null when it has none.
346
+ *
347
+ * ATX (`# Title`, optional closing `#`s) and setext (`Title` underlined with `===`). The heading
348
+ * need not be the first line — a draft may open with a note. Skipped: YAML frontmatter, fenced
349
+ * code, indented code, blockquotes (a quoted heading is not the page's heading), HTML comments and
350
+ * multi-line MDX expressions (`{ … }`). `#hashtag` (no space) is not a heading. A heading that is
351
+ * empty after stripping its Markdown does not count.
352
+ */
353
+ export function extractTitleHeading(content) {
354
+ if (!content)
355
+ return null;
356
+ const lines = String(content).split(/\r?\n/);
357
+ let fence = null;
358
+ let inComment = false;
359
+ let braceDepth = 0;
360
+ /** Lines of the paragraph that a setext underline would turn into a heading. */
361
+ let para = [];
362
+ for (let ln = bodyStartLine(lines); ln < lines.length; ln++) {
363
+ const line = lines[ln];
364
+ if (fence) {
365
+ const m = FENCE_RE.exec(line);
366
+ if (m && m[1][0] === fence.ch && m[1].length >= fence.len && m[2].trim() === '')
367
+ fence = null;
368
+ continue;
369
+ }
370
+ if (inComment) {
371
+ if (line.includes('-->'))
372
+ inComment = false;
373
+ continue;
374
+ }
375
+ if (braceDepth > 0) {
376
+ for (const ch of line) {
377
+ if (ch === '{')
378
+ braceDepth++;
379
+ else if (ch === '}')
380
+ braceDepth--;
381
+ }
382
+ if (braceDepth < 0)
383
+ braceDepth = 0;
384
+ continue;
385
+ }
386
+ const fenceOpen = FENCE_RE.exec(line);
387
+ if (fenceOpen && !(fenceOpen[1][0] === '`' && fenceOpen[2].includes('`'))) {
388
+ fence = { ch: fenceOpen[1][0], len: fenceOpen[1].length };
389
+ para = [];
390
+ continue;
391
+ }
392
+ if (/^ {0,3}<!--/.test(line) && !line.includes('-->')) {
393
+ inComment = true;
394
+ para = [];
395
+ continue;
396
+ }
397
+ if (/^ {0,3}\{/.test(line)) {
398
+ let depth = 0;
399
+ for (const ch of line) {
400
+ if (ch === '{')
401
+ depth++;
402
+ else if (ch === '}')
403
+ depth--;
404
+ }
405
+ if (depth > 0)
406
+ braceDepth = depth;
407
+ para = [];
408
+ continue;
409
+ }
410
+ if (line.trim() === '') {
411
+ para = [];
412
+ continue;
413
+ }
414
+ // Indented code (only when it cannot be a paragraph continuation) and blockquotes.
415
+ if ((/^( {4}|\t)/.test(line) && para.length === 0) || /^ {0,3}>/.test(line)) {
416
+ para = [];
417
+ continue;
418
+ }
419
+ const atx = ATX_H1_RE.exec(line);
420
+ if (atx) {
421
+ // Optional closing sequence: a run of `#` preceded by a space (or the whole content).
422
+ const raw = (atx[1] ?? '').replace(/(^|[ \t]+)#+[ \t]*$/, '');
423
+ const text = inlineMarkdownToPlainText(raw);
424
+ if (text)
425
+ return text;
426
+ para = [];
427
+ continue;
428
+ }
429
+ if (SETEXT_H1_RE.test(line) && para.length > 0) {
430
+ const text = inlineMarkdownToPlainText(para.join(' '));
431
+ if (text)
432
+ return text;
433
+ para = [];
434
+ continue;
435
+ }
436
+ // Lines that start another block end the paragraph instead of joining it.
437
+ if (/^ {0,3}(#{1,6}([ \t]|$)|[-*+][ \t]|\d{1,9}[.)][ \t]|(?:[-*_][ \t]*){3,}$|\|)/.test(line) || /^ {0,3}<[A-Za-z/]/.test(line)) {
438
+ para = [];
439
+ continue;
440
+ }
441
+ para.push(line.trim());
442
+ }
443
+ return null;
444
+ }
445
+ //# sourceMappingURL=title.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"title.js","sourceRoot":"","sources":["../src/title.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,qFAAqF;AACrF,MAAM,OAAO,GAAG,MAAM,CAAA;AACtB,MAAM,KAAK,GAAG,QAAQ,CAAA;AAEtB,sFAAsF;AACtF,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,oCAAoC,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAA;AAE3E,MAAM,cAAc,GAA2B;IAC7C,GAAG,EAAE,GAAG;IACR,EAAE,EAAE,GAAG;IACP,EAAE,EAAE,GAAG;IACP,IAAI,EAAE,GAAG;IACT,IAAI,EAAE,GAAG;IACT,IAAI,EAAE,GAAG;IACT,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,MAAM,EAAE,GAAG;IACX,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,IAAI,EAAE,GAAG;IACT,GAAG,EAAE,GAAG;IACR,KAAK,EAAE,GAAG;IACV,KAAK,EAAE,GAAG;IACV,GAAG,EAAE,GAAG;IACR,MAAM,EAAE,GAAG;CACZ,CAAA;AAED,SAAS,cAAc,CAAC,CAAS;IAC/B,OAAO,CAAC,CAAC,OAAO,CAAC,kEAAkE,EAAE,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IAAI,GAAG,EAAE,CAAA;YACT,SAAQ;QACV,CAAC;QACD,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,GAAG,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;YACtD,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAA;YACrB,IAAI,GAAG,EAAE,CAAA;YACT,SAAQ;QACV,CAAC;QACD,0EAA0E;QAC1E,IAAI,8EAA8E,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAChI,IAAI,GAAG,EAAE,CAAA;YACT,SAAQ;QACV,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;IACxB,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blogmcp/mdx-schema",
3
- "version": "0.8.1",
3
+ "version": "0.9.1",
4
4
  "description": "Framework-agnostic schema + metadata + parsing for MDX components. Zero framework dependencies — consumed by Vue and React renderer packages.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -49,6 +49,10 @@
49
49
  "./tripmap": {
50
50
  "types": "./dist/tripmap.d.ts",
51
51
  "import": "./dist/tripmap.js"
52
+ },
53
+ "./title": {
54
+ "types": "./dist/title.d.ts",
55
+ "import": "./dist/title.js"
52
56
  }
53
57
  },
54
58
  "files": [
package/src/index.ts CHANGED
@@ -20,3 +20,4 @@ export * from './styles/index.js'
20
20
  export * from './version.js'
21
21
  export * from './prepareProps.js'
22
22
  export * from './checkPin.js'
23
+ export * from './title.js'
package/src/title.ts ADDED
@@ -0,0 +1,435 @@
1
+ /**
2
+ * The article title as plain text, and the title an article body declares.
3
+ *
4
+ * Contract (owner decision 2026-10-10): the article BODY is the single source of truth for the
5
+ * title. When the body has a level-1 heading, kb-api stores that heading's plain text as the
6
+ * translation's `title` on every content write; the title column then serves only places that do
7
+ * not render Markdown (`<title>`, meta/og/twitter, JSON-LD, cards, search). Those places print the
8
+ * title as text, so it has to be plain text — `Outkovka pestrá (*Trametes versicolor*)` showed its
9
+ * asterisks in the browser tab before this existed.
10
+ *
11
+ * Dependency-free on purpose: kb-api (write path), `@blogmcp/sdk` (SEO/cards) and md-editor all
12
+ * call these two functions, so they agree by construction instead of by three regexes.
13
+ */
14
+
15
+ /** Private-use placeholders protect code spans and escapes from the later passes. */
16
+ const PH_BASE = 0xe000
17
+ const PH_RE = /[-]/g
18
+
19
+ /** ASCII punctuation, as CommonMark defines it for backslash escapes and flanking. */
20
+ const ASCII_PUNCT = new Set('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.split(''))
21
+
22
+ const NAMED_ENTITIES: Record<string, string> = {
23
+ amp: '&',
24
+ lt: '<',
25
+ gt: '>',
26
+ quot: '"',
27
+ apos: "'",
28
+ nbsp: ' ',
29
+ ndash: '–',
30
+ mdash: '—',
31
+ hellip: '…',
32
+ laquo: '«',
33
+ raquo: '»',
34
+ bdquo: '„',
35
+ ldquo: '“',
36
+ rdquo: '”',
37
+ lsquo: '‘',
38
+ rsquo: '’',
39
+ sbquo: '‚',
40
+ copy: '©',
41
+ reg: '®',
42
+ trade: '™',
43
+ times: '×',
44
+ deg: '°',
45
+ middot: '·',
46
+ }
47
+
48
+ function decodeEntities(s: string): string {
49
+ return s.replace(/&(#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6}|[A-Za-z][A-Za-z0-9]{1,31});/g, (whole, body: string) => {
50
+ if (body[0] === '#') {
51
+ const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10)
52
+ if (!Number.isFinite(code) || code <= 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return '�'
53
+ return String.fromCodePoint(code)
54
+ }
55
+ return NAMED_ENTITIES[body] ?? whole
56
+ })
57
+ }
58
+
59
+ function isWhitespace(ch: string | undefined): boolean {
60
+ return ch === undefined || /\s/u.test(ch)
61
+ }
62
+
63
+ function isPunctuation(ch: string | undefined): boolean {
64
+ if (ch === undefined) return false
65
+ if (ASCII_PUNCT.has(ch)) return true
66
+ return /[\p{P}\p{S}]/u.test(ch)
67
+ }
68
+
69
+ /**
70
+ * Replace code spans and backslash escapes with placeholders whose literal text is kept in
71
+ * `store`. Run before every other pass, so nothing inside a code span is ever read as markup.
72
+ */
73
+ function protect(s: string, store: string[]): string {
74
+ const hold = (text: string): string => {
75
+ store.push(text)
76
+ return String.fromCharCode(PH_BASE + store.length - 1)
77
+ }
78
+ let out = ''
79
+ let i = 0
80
+ while (i < s.length) {
81
+ const ch = s[i]!
82
+ if (ch === '\\' && i + 1 < s.length && ASCII_PUNCT.has(s[i + 1]!)) {
83
+ out += hold(s[i + 1]!)
84
+ i += 2
85
+ continue
86
+ }
87
+ if (ch === '`') {
88
+ let run = 0
89
+ while (s[i + run] === '`') run++
90
+ // Find a closing run of exactly the same length.
91
+ let j = i + run
92
+ let close = -1
93
+ while (j < s.length) {
94
+ if (s[j] !== '`') { j++; continue }
95
+ let r = 0
96
+ while (s[j + r] === '`') r++
97
+ if (r === run) { close = j; break }
98
+ j += r
99
+ }
100
+ if (close === -1) {
101
+ // No closer: the backticks are literal text.
102
+ out += s.slice(i, i + run)
103
+ i += run
104
+ continue
105
+ }
106
+ let body = s.slice(i + run, close).replace(/\r?\n/g, ' ')
107
+ // CommonMark: strip ONE leading and trailing space when both exist and the body is not all spaces.
108
+ if (body.length >= 2 && body.startsWith(' ') && body.endsWith(' ') && body.trim() !== '') body = body.slice(1, -1)
109
+ out += hold(body)
110
+ i = close + run
111
+ continue
112
+ }
113
+ out += ch
114
+ i++
115
+ }
116
+ return out
117
+ }
118
+
119
+ function restore(s: string, store: string[]): string {
120
+ return s.replace(PH_RE, (c) => store[c.charCodeAt(0) - PH_BASE] ?? '')
121
+ }
122
+
123
+ /** Index of the `]` that closes the `[` at `open`, honouring nesting; -1 when none. */
124
+ function closingBracket(s: string, open: number): number {
125
+ let depth = 0
126
+ for (let i = open; i < s.length; i++) {
127
+ if (s[i] === '[') depth++
128
+ else if (s[i] === ']') {
129
+ depth--
130
+ if (depth === 0) return i
131
+ }
132
+ }
133
+ return -1
134
+ }
135
+
136
+ /** Index just past the `)` closing an inline link destination starting at `open` (the `(`); -1 when none. */
137
+ function closingParen(s: string, open: number): number {
138
+ let depth = 0
139
+ let i = open
140
+ // `<…>` destination: anything up to `>`.
141
+ for (; i < s.length; i++) {
142
+ const ch = s[i]!
143
+ if (ch === '(') depth++
144
+ else if (ch === ')') {
145
+ depth--
146
+ if (depth === 0) return i + 1
147
+ } else if (ch === '\n' && s[i + 1] === '\n') return -1
148
+ }
149
+ return -1
150
+ }
151
+
152
+ /** `![alt](…)` → alt, `[text](…)` / `[text][ref]` / `[text][]` → text. Shortcut `[text]` stays. */
153
+ function stripLinks(s: string): string {
154
+ let out = ''
155
+ let i = 0
156
+ while (i < s.length) {
157
+ const isImage = s[i] === '!' && s[i + 1] === '['
158
+ const start = isImage ? i + 1 : i
159
+ if (s[start] !== '[') {
160
+ out += s[i]
161
+ i++
162
+ continue
163
+ }
164
+ const close = closingBracket(s, start)
165
+ if (close === -1) {
166
+ out += s[i]
167
+ i++
168
+ continue
169
+ }
170
+ const text = s.slice(start + 1, close)
171
+ const next = s[close + 1]
172
+ if (next === '(') {
173
+ const end = closingParen(s, close + 1)
174
+ if (end !== -1) {
175
+ out += stripLinks(text)
176
+ i = end
177
+ continue
178
+ }
179
+ } else if (next === '[') {
180
+ const refClose = s.indexOf(']', close + 2)
181
+ if (refClose !== -1 && !s.slice(close + 2, refClose).includes('[')) {
182
+ out += stripLinks(text)
183
+ i = refClose + 1
184
+ continue
185
+ }
186
+ }
187
+ // Not a link we can resolve (shortcut reference or literal brackets): keep as written.
188
+ out += s[i]
189
+ i++
190
+ }
191
+ return out
192
+ }
193
+
194
+ /** Inline HTML / JSX tags, HTML comments, MDX comments and autolinks. Text between tags is kept. */
195
+ function stripTags(s: string): string {
196
+ return s
197
+ .replace(/<!--[\s\S]*?-->/g, '')
198
+ .replace(/\{\/\*[\s\S]*?\*\/\}/g, '')
199
+ .replace(/<((?:https?|mailto|ftp):[^\s<>]*)>/gi, '$1')
200
+ .replace(/<([^\s<>@]+@[^\s<>@]+\.[^\s<>@]+)>/g, '$1')
201
+ .replace(/<\/?[A-Za-z][A-Za-z0-9.:-]*(?:\s+(?:[^<>"'{}]|"[^"]*"|'[^']*'|\{[^{}]*\})*)?\/?>/g, '')
202
+ }
203
+
204
+ interface Delim {
205
+ /** Index into `parts`. */
206
+ part: number
207
+ ch: '*' | '_' | '~'
208
+ count: number
209
+ origCount: number
210
+ canOpen: boolean
211
+ canClose: boolean
212
+ }
213
+
214
+ /**
215
+ * CommonMark emphasis (`*`, `_`) and GFM strikethrough (`~~`, `~`): matched delimiters are
216
+ * removed, unmatched ones stay as literal text. Flanking rules decide who may open or close, so
217
+ * an intraword underscore (`kb_upsert_tag_translation`) is never emphasis while an intraword
218
+ * asterisk (`un*frigging*believable`) is.
219
+ */
220
+ function stripEmphasis(s: string): string {
221
+ const parts: string[] = []
222
+ const delims: Delim[] = []
223
+ let buf = ''
224
+ let i = 0
225
+ while (i < s.length) {
226
+ const ch = s[i]!
227
+ if (ch !== '*' && ch !== '_' && ch !== '~') {
228
+ buf += ch
229
+ i++
230
+ continue
231
+ }
232
+ let run = 0
233
+ while (s[i + run] === ch) run++
234
+ const before = i === 0 ? undefined : s[i - 1]
235
+ const after = s[i + run]
236
+ const leftFlanking =
237
+ !isWhitespace(after) && (!isPunctuation(after) || isWhitespace(before) || isPunctuation(before))
238
+ const rightFlanking =
239
+ !isWhitespace(before) && (!isPunctuation(before) || isWhitespace(after) || isPunctuation(after))
240
+ let canOpen = leftFlanking
241
+ let canClose = rightFlanking
242
+ if (ch === '_') {
243
+ canOpen = leftFlanking && (!rightFlanking || isPunctuation(before))
244
+ canClose = rightFlanking && (!leftFlanking || isPunctuation(after))
245
+ }
246
+ if (ch === '~' && run > 2) {
247
+ // GFM: three or more tildes never delimit strikethrough.
248
+ canOpen = false
249
+ canClose = false
250
+ }
251
+ if (buf) parts.push(buf)
252
+ buf = ''
253
+ parts.push(s.slice(i, i + run))
254
+ delims.push({ part: parts.length - 1, ch, count: run, origCount: run, canOpen, canClose })
255
+ i += run
256
+ }
257
+ if (buf) parts.push(buf)
258
+
259
+ // Keep left/right halves per delimiter run so partial matches leave the right characters.
260
+ const removeLeft = new Map<number, number>()
261
+ const removeRight = new Map<number, number>()
262
+ const active = delims.map(() => true)
263
+
264
+ for (let c = 0; c < delims.length; c++) {
265
+ const closer = delims[c]!
266
+ if (!closer.canClose) continue
267
+ while (closer.count > 0) {
268
+ let found = -1
269
+ for (let o = c - 1; o >= 0; o--) {
270
+ const opener = delims[o]!
271
+ if (!active[o] || opener.ch !== closer.ch || !opener.canOpen || opener.count === 0) continue
272
+ if (closer.ch === '~') {
273
+ if (opener.count !== closer.count) continue
274
+ } else if ((opener.canClose || closer.canOpen) && (opener.origCount + closer.origCount) % 3 === 0 &&
275
+ !(opener.origCount % 3 === 0 && closer.origCount % 3 === 0)) {
276
+ // CommonMark "rule of 3".
277
+ continue
278
+ }
279
+ found = o
280
+ break
281
+ }
282
+ if (found === -1) break
283
+ const opener = delims[found]!
284
+ const use = closer.ch === '~' ? closer.count : opener.count >= 2 && closer.count >= 2 ? 2 : 1
285
+ opener.count -= use
286
+ closer.count -= use
287
+ removeRight.set(opener.part, (removeRight.get(opener.part) ?? 0) + use)
288
+ removeLeft.set(closer.part, (removeLeft.get(closer.part) ?? 0) + use)
289
+ // Delimiters between the pair can no longer match anything.
290
+ for (let k = found + 1; k < c; k++) active[k] = false
291
+ if (opener.count === 0) active[found] = false
292
+ }
293
+ if (closer.count === 0) active[c] = false
294
+ }
295
+
296
+ const delimByPart = new Map(delims.map((d) => [d.part, d]))
297
+ return parts
298
+ .map((p, idx) => {
299
+ const d = delimByPart.get(idx)
300
+ if (!d) return p
301
+ const left = removeLeft.get(idx) ?? 0
302
+ const right = removeRight.get(idx) ?? 0
303
+ return p.slice(left, Math.max(left, p.length - right))
304
+ })
305
+ .join('')
306
+ }
307
+
308
+ /**
309
+ * Inline Markdown/MDX → plain text, for every place that prints a title as text.
310
+ *
311
+ * Emphasis, strong, strikethrough, code spans (their text is kept), links and images (their text /
312
+ * alt is kept), inline HTML and JSX tags (removed, text between them kept), backslash escapes and
313
+ * the common entities. Whitespace is collapsed and trimmed. Text that is not Markdown is returned
314
+ * unchanged apart from whitespace, so the function is safe to apply to any stored title.
315
+ */
316
+ export function inlineMarkdownToPlainText(input: string | null | undefined): string {
317
+ if (!input) return ''
318
+ const store: string[] = []
319
+ let s = protect(String(input), store)
320
+ s = stripTags(s)
321
+ s = stripLinks(s)
322
+ s = stripEmphasis(s)
323
+ s = restore(s, store)
324
+ s = decodeEntities(s)
325
+ return s.replace(/\s+/g, ' ').trim()
326
+ }
327
+
328
+ const ATX_H1_RE = /^ {0,3}#(?:[ \t]+(.*?))?[ \t]*$/
329
+ const SETEXT_H1_RE = /^ {0,3}=+[ \t]*$/
330
+ const FENCE_RE = /^ {0,3}(`{3,}|~{3,})(.*)$/
331
+
332
+ /** Frontmatter only when the document opens with a `---` line closed by a later `---`/`...` line. */
333
+ function bodyStartLine(lines: string[]): number {
334
+ if (lines[0]?.trim() !== '---') return 0
335
+ for (let k = 1; k < lines.length; k++) {
336
+ const t = lines[k]!.trim()
337
+ if (t === '---' || t === '...') return k + 1
338
+ }
339
+ return 0
340
+ }
341
+
342
+ /**
343
+ * The plain text of the FIRST level-1 heading of an article body, or null when it has none.
344
+ *
345
+ * ATX (`# Title`, optional closing `#`s) and setext (`Title` underlined with `===`). The heading
346
+ * need not be the first line — a draft may open with a note. Skipped: YAML frontmatter, fenced
347
+ * code, indented code, blockquotes (a quoted heading is not the page's heading), HTML comments and
348
+ * multi-line MDX expressions (`{ … }`). `#hashtag` (no space) is not a heading. A heading that is
349
+ * empty after stripping its Markdown does not count.
350
+ */
351
+ export function extractTitleHeading(content: string | null | undefined): string | null {
352
+ if (!content) return null
353
+ const lines = String(content).split(/\r?\n/)
354
+ let fence: { ch: string; len: number } | null = null
355
+ let inComment = false
356
+ let braceDepth = 0
357
+ /** Lines of the paragraph that a setext underline would turn into a heading. */
358
+ let para: string[] = []
359
+
360
+ for (let ln = bodyStartLine(lines); ln < lines.length; ln++) {
361
+ const line = lines[ln]!
362
+
363
+ if (fence) {
364
+ const m = FENCE_RE.exec(line)
365
+ if (m && m[1]![0] === fence.ch && m[1]!.length >= fence.len && m[2]!.trim() === '') fence = null
366
+ continue
367
+ }
368
+ if (inComment) {
369
+ if (line.includes('-->')) inComment = false
370
+ continue
371
+ }
372
+ if (braceDepth > 0) {
373
+ for (const ch of line) {
374
+ if (ch === '{') braceDepth++
375
+ else if (ch === '}') braceDepth--
376
+ }
377
+ if (braceDepth < 0) braceDepth = 0
378
+ continue
379
+ }
380
+
381
+ const fenceOpen = FENCE_RE.exec(line)
382
+ if (fenceOpen && !(fenceOpen[1]![0] === '`' && fenceOpen[2]!.includes('`'))) {
383
+ fence = { ch: fenceOpen[1]![0]!, len: fenceOpen[1]!.length }
384
+ para = []
385
+ continue
386
+ }
387
+ if (/^ {0,3}<!--/.test(line) && !line.includes('-->')) {
388
+ inComment = true
389
+ para = []
390
+ continue
391
+ }
392
+ if (/^ {0,3}\{/.test(line)) {
393
+ let depth = 0
394
+ for (const ch of line) {
395
+ if (ch === '{') depth++
396
+ else if (ch === '}') depth--
397
+ }
398
+ if (depth > 0) braceDepth = depth
399
+ para = []
400
+ continue
401
+ }
402
+ if (line.trim() === '') {
403
+ para = []
404
+ continue
405
+ }
406
+ // Indented code (only when it cannot be a paragraph continuation) and blockquotes.
407
+ if ((/^( {4}|\t)/.test(line) && para.length === 0) || /^ {0,3}>/.test(line)) {
408
+ para = []
409
+ continue
410
+ }
411
+
412
+ const atx = ATX_H1_RE.exec(line)
413
+ if (atx) {
414
+ // Optional closing sequence: a run of `#` preceded by a space (or the whole content).
415
+ const raw = (atx[1] ?? '').replace(/(^|[ \t]+)#+[ \t]*$/, '')
416
+ const text = inlineMarkdownToPlainText(raw)
417
+ if (text) return text
418
+ para = []
419
+ continue
420
+ }
421
+ if (SETEXT_H1_RE.test(line) && para.length > 0) {
422
+ const text = inlineMarkdownToPlainText(para.join(' '))
423
+ if (text) return text
424
+ para = []
425
+ continue
426
+ }
427
+ // Lines that start another block end the paragraph instead of joining it.
428
+ if (/^ {0,3}(#{1,6}([ \t]|$)|[-*+][ \t]|\d{1,9}[.)][ \t]|(?:[-*_][ \t]*){3,}$|\|)/.test(line) || /^ {0,3}<[A-Za-z/]/.test(line)) {
429
+ para = []
430
+ continue
431
+ }
432
+ para.push(line.trim())
433
+ }
434
+ return null
435
+ }