@softure-ai/blog 0.1.6 → 0.1.7

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +135 -10
  3. package/dist/cli/report.d.ts +17 -0
  4. package/dist/cli/report.d.ts.map +1 -0
  5. package/dist/cli/report.js +149 -0
  6. package/dist/cli/report.js.map +1 -0
  7. package/dist/cli/run.d.ts +12 -5
  8. package/dist/cli/run.d.ts.map +1 -1
  9. package/dist/cli/run.js +121 -87
  10. package/dist/cli/run.js.map +1 -1
  11. package/dist/contract.d.ts +4 -0
  12. package/dist/contract.d.ts.map +1 -1
  13. package/dist/db/articles.d.ts +10 -1
  14. package/dist/db/articles.d.ts.map +1 -1
  15. package/dist/db/articles.js +36 -4
  16. package/dist/db/articles.js.map +1 -1
  17. package/dist/db/history.d.ts +33 -0
  18. package/dist/db/history.d.ts.map +1 -0
  19. package/dist/db/history.js +66 -0
  20. package/dist/db/history.js.map +1 -0
  21. package/dist/db/publish-run.d.ts +11 -0
  22. package/dist/db/publish-run.d.ts.map +1 -1
  23. package/dist/db/publish-run.js +14 -4
  24. package/dist/db/publish-run.js.map +1 -1
  25. package/dist/index.js +1 -1
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +3 -1
  28. package/dist/options.js.map +1 -1
  29. package/dist/pages/accept.d.ts +7 -0
  30. package/dist/pages/accept.d.ts.map +1 -0
  31. package/dist/pages/accept.js +34 -0
  32. package/dist/pages/accept.js.map +1 -0
  33. package/dist/proxy/index.d.ts +15 -0
  34. package/dist/proxy/index.d.ts.map +1 -1
  35. package/dist/proxy/index.js +52 -2
  36. package/dist/proxy/index.js.map +1 -1
  37. package/dist/quality/catalog.d.ts.map +1 -1
  38. package/dist/quality/catalog.js +4 -1
  39. package/dist/quality/catalog.js.map +1 -1
  40. package/dist/quality/check-article.d.ts.map +1 -1
  41. package/dist/quality/check-article.js +2 -1
  42. package/dist/quality/check-article.js.map +1 -1
  43. package/dist/quality/options.d.ts.map +1 -1
  44. package/dist/quality/options.js +3 -1
  45. package/dist/quality/options.js.map +1 -1
  46. package/dist/quality/rules/blocks.d.ts +8 -1
  47. package/dist/quality/rules/blocks.d.ts.map +1 -1
  48. package/dist/quality/rules/blocks.js +26 -0
  49. package/dist/quality/rules/blocks.js.map +1 -1
  50. package/dist/render/article-markdown.d.ts +12 -0
  51. package/dist/render/article-markdown.d.ts.map +1 -0
  52. package/dist/render/article-markdown.js +21 -0
  53. package/dist/render/article-markdown.js.map +1 -0
  54. package/dist/render/index.d.ts +2 -1
  55. package/dist/render/index.d.ts.map +1 -1
  56. package/dist/render/index.js +2 -1
  57. package/dist/render/index.js.map +1 -1
  58. package/dist/render/render-article.d.ts +40 -4
  59. package/dist/render/render-article.d.ts.map +1 -1
  60. package/dist/render/render-article.js +132 -10
  61. package/dist/render/render-article.js.map +1 -1
  62. package/dist/server/index.d.ts +2 -1
  63. package/dist/server/index.d.ts.map +1 -1
  64. package/dist/server/index.js +1 -0
  65. package/dist/server/index.js.map +1 -1
  66. package/module.json +1 -1
  67. package/package.json +1 -1
  68. package/skill/references/rules.md +2 -1
  69. package/src/cli/report.ts +182 -0
  70. package/src/cli/run.ts +121 -91
  71. package/src/contract.ts +9 -1
  72. package/src/db/articles.ts +39 -4
  73. package/src/db/history.ts +83 -0
  74. package/src/db/publish-run.ts +24 -4
  75. package/src/index.ts +1 -1
  76. package/src/options.ts +6 -2
  77. package/src/pages/accept.ts +39 -0
  78. package/src/proxy/index.ts +60 -2
  79. package/src/quality/catalog.ts +4 -1
  80. package/src/quality/check-article.ts +2 -1
  81. package/src/quality/options.ts +6 -2
  82. package/src/quality/rules/blocks.ts +26 -1
  83. package/src/render/article-markdown.ts +34 -0
  84. package/src/render/index.ts +6 -0
  85. package/src/render/render-article.ts +170 -16
  86. package/src/server/index.ts +2 -0
@@ -36,6 +36,10 @@
36
36
  // A fenced block whose type an app registers (```` ```chart ````) is rendered by the app's plugin, as
37
37
  // HTML or as a node (e.g. a React server component). Plugin output is the app's own code and is
38
38
  // trusted as is. Only top-level fences are plugin blocks; a fence inside a list or a quote stays code.
39
+ //
40
+ // A plugin with `syntax: "directive"` renders a leaf directive instead: a line of its own,
41
+ // `::chart{type="wealth" scenario="…"}` (braces optional), with double-quoted values and each key
42
+ // once. Like fences, only top-level lines count; a directive in a list, a quote or code stays text.
39
43
  import MarkdownIt, { type MarkdownIt as Markdown, type StateCore, type Token } from "markdown-it";
40
44
  import footnote from "markdown-it-footnote";
41
45
  import type { BlogFields } from "../contract.js";
@@ -61,12 +65,24 @@ export interface BlockArticle {
61
65
  readonly fields?: BlogFields;
62
66
  }
63
67
 
68
+ /** How a block is written: a fence (```` ```chart ````) or a leaf directive (`::chart{…}`). */
69
+ export type BlockSyntax = "fence" | "directive";
70
+
71
+ /** A directive's attributes, or `null` when its braces cannot be read. Always `{}` for a fence. */
72
+ export type BlockAttributes = Readonly<Record<string, string>> | null;
73
+
64
74
  export interface ArticleBlock {
65
- /** The block type, the first word of the fence's info string. */
75
+ /** The block type: the first word of the fence's info string, or the directive's name. */
66
76
  readonly type: string;
67
- /** The rest of the info string, trimmed: ```` ```chart wealth ```` → `"wealth"`. */
77
+ readonly syntax: BlockSyntax;
78
+ /**
79
+ * Fence: the rest of the info string, trimmed (```` ```chart wealth ```` → `"wealth"`). Directive:
80
+ * the text inside the braces, trimmed.
81
+ */
68
82
  readonly info: string;
69
- /** The body of the fence, as written. */
83
+ /** `key="value"` pairs of a directive; `{}` for a fence. */
84
+ readonly attributes: BlockAttributes;
85
+ /** Fence: its body, as written. Directive: the whole line, trimmed. */
70
86
  readonly content: string;
71
87
  readonly article: BlockArticle;
72
88
  }
@@ -78,6 +94,8 @@ export type BlockOutput<TNode = unknown> =
78
94
  export interface BlockPlugin<TNode = unknown> {
79
95
  /** Lower-case kebab-case, e.g. `chart`. */
80
96
  readonly type: string;
97
+ /** Which blocks the plugin renders: fences (the default) or leaf directives. */
98
+ readonly syntax?: BlockSyntax;
81
99
  /**
82
100
  * The frontmatter keys the block reads (`current_as_of` or keys of the app's `fields`), so the
83
101
  * quality gate can report a block whose article lacks them.
@@ -85,6 +103,11 @@ export interface BlockPlugin<TNode = unknown> {
85
103
  readonly requires?: readonly string[];
86
104
  /** Throws only on a bug; a block that cannot render returns its own error markup. */
87
105
  readonly render: (block: ArticleBlock) => BlockOutput<TNode>;
106
+ /**
107
+ * The block as Markdown for agents (`toArticleMarkdown`, `Accept: text/markdown`): a table or a
108
+ * sentence. Without it the block's source stays in the Markdown.
109
+ */
110
+ readonly markdown?: (block: ArticleBlock) => string;
88
111
  }
89
112
 
90
113
  export type ArticleSegment<TNode = unknown> =
@@ -127,8 +150,10 @@ export interface RenderedArticle<TNode = unknown> {
127
150
 
128
151
  export interface FoundBlock {
129
152
  readonly type: string;
153
+ readonly syntax: BlockSyntax;
130
154
  readonly info: string;
131
- /** 1-based line of the opening fence. */
155
+ readonly attributes: BlockAttributes;
156
+ /** 1-based line of the opening fence or of the directive. */
132
157
  readonly line: number;
133
158
  readonly requires: readonly string[];
134
159
  }
@@ -141,6 +166,61 @@ const FOOTNOTES_HEADING_ID = "footnotes";
141
166
  // Ids the renderer gives footnotes; a heading whose slug equals one of them gets a suffix.
142
167
  const FOOTNOTE_ID = /^(?:footnotes|fn(?:ref)?-\d+(?:-\d+)?)$/;
143
168
  const BLOCK_TOKEN = "blog_block";
169
+ const DIRECTIVE_LINE = /^::([a-z][a-z0-9]*(?:-[a-z0-9]+)*)(?:\{(.*)\})?$/;
170
+ const DIRECTIVE_START = /^::[a-z]/;
171
+ const ATTRIBUTE_KEY_START = /[a-z]/;
172
+ const ATTRIBUTE_KEY_CHAR = /[a-z0-9_-]/;
173
+ const WHITESPACE = /\s/;
174
+
175
+ interface BlockMeta {
176
+ readonly type: string;
177
+ readonly syntax: BlockSyntax;
178
+ readonly info: string;
179
+ readonly attributes: BlockAttributes;
180
+ }
181
+
182
+ /** The `key="value"` pairs inside a directive's braces; `null` when anything else is there or a key repeats. */
183
+ export function parseDirectiveAttributes(text: string): BlockAttributes {
184
+ // A hand scanner, linear in the text: a regex here runs on author input and backtracks on long runs.
185
+ const attributes: Record<string, string> = {};
186
+ let index = 0;
187
+ const skipWhitespace = (): void => {
188
+ while (index < text.length && WHITESPACE.test(text.charAt(index))) index += 1;
189
+ };
190
+ skipWhitespace();
191
+ while (index < text.length) {
192
+ if (!ATTRIBUTE_KEY_START.test(text.charAt(index))) return null;
193
+ const keyStart = index;
194
+ while (index < text.length && ATTRIBUTE_KEY_CHAR.test(text.charAt(index))) index += 1;
195
+ const key = text.slice(keyStart, index);
196
+ if (text.charAt(index) !== "=" || text.charAt(index + 1) !== '"') return null;
197
+ const valueEnd = text.indexOf('"', index + 2);
198
+ if (valueEnd === -1) return null;
199
+ if (Object.hasOwn(attributes, key)) return null;
200
+ attributes[key] = text.slice(index + 2, valueEnd);
201
+ index = valueEnd + 1;
202
+ skipWhitespace();
203
+ }
204
+ return attributes;
205
+ }
206
+
207
+ /**
208
+ * A line read as a leaf directive: its name, the text inside the braces and the attributes. `null`
209
+ * for a line that is not one (a name must follow `::`); a directive whose braces do not close keeps
210
+ * its name and gets `null` attributes.
211
+ */
212
+ export function parseDirectiveLine(line: string): { readonly name: string; readonly info: string; readonly attributes: BlockAttributes } | null {
213
+ const trimmed = line.trim();
214
+ if (!DIRECTIVE_START.test(trimmed)) return null;
215
+ const match = DIRECTIVE_LINE.exec(trimmed);
216
+ if (match === null) {
217
+ const name = /^::([a-z][a-z0-9-]*)/.exec(trimmed)?.[1] ?? "";
218
+ const rest = trimmed.slice(2 + name.length).trim();
219
+ return { name, info: rest.replace(/^\{/, "").trim(), attributes: null };
220
+ }
221
+ const info = (match[2] ?? "").trim();
222
+ return { name: match[1] ?? "", info, attributes: parseDirectiveAttributes(info) };
223
+ }
144
224
 
145
225
  function getDefaultTermHref(slug: string): string {
146
226
  return `/blog/glossary/${slug}`;
@@ -354,26 +434,77 @@ function addBlockTokens(md: Markdown, types: ReadonlySet<string>): void {
354
434
  const [type = "", ...rest] = token.info.trim().split(/\s+/);
355
435
  if (!types.has(type)) continue;
356
436
  token.type = BLOCK_TOKEN;
357
- token.meta = { type, info: rest.join(" ") };
437
+ const meta: BlockMeta = { type, syntax: "fence", info: rest.join(" "), attributes: {} };
438
+ token.meta = { block: meta };
358
439
  }
359
440
  });
360
441
  }
361
442
 
362
- function getPluginsByType<TNode>(plugins: readonly BlockPlugin<TNode>[]): Map<string, BlockPlugin<TNode>> {
363
- const byType = new Map<string, BlockPlugin<TNode>>();
443
+ /**
444
+ * A block rule for top-level lines `::name{…}` of registered directive names. Before `paragraph`
445
+ * and allowed to end one, so a directive right under a paragraph line is still a block.
446
+ */
447
+ function addDirectiveRule(md: Markdown, names: ReadonlySet<string>): void {
448
+ if (names.size === 0) return;
449
+ md.block.ruler.before(
450
+ "paragraph",
451
+ "blog_directive",
452
+ (state, startLine, _endLine, silent) => {
453
+ // Four spaces are an indented code block; a nested block (list, quote) is not top level.
454
+ if (state.level !== 0 || state.blkIndent !== 0 || (state.sCount[startLine] ?? 0) - state.blkIndent >= 4) return false;
455
+ const line = state.src.slice((state.bMarks[startLine] ?? 0) + (state.tShift[startLine] ?? 0), state.eMarks[startLine]);
456
+ const directive = parseDirectiveLine(line);
457
+ if (directive === null || !names.has(directive.name)) return false;
458
+ if (!silent) {
459
+ const token = state.push(BLOCK_TOKEN, "", 0);
460
+ token.block = true;
461
+ token.content = line.trim();
462
+ token.map = [startLine, startLine + 1];
463
+ const meta: BlockMeta = { type: directive.name, syntax: "directive", info: directive.info, attributes: directive.attributes };
464
+ token.meta = { block: meta };
465
+ }
466
+ state.line = startLine + 1;
467
+ return true;
468
+ },
469
+ { alt: ["paragraph"] },
470
+ );
471
+ }
472
+
473
+ /** The block a `blog_block` token stands for; set by `addBlockTokens` and `addDirectiveRule`. */
474
+ function readBlockMeta(token: Token): BlockMeta {
475
+ return (token.meta as { block: BlockMeta }).block;
476
+ }
477
+
478
+ /** Plugins by `syntax:type`. */
479
+ type PluginRegistry<TNode> = Map<string, BlockPlugin<TNode>>;
480
+
481
+ function getPluginKey(syntax: BlockSyntax, type: string): string {
482
+ return `${syntax}:${type}`;
483
+ }
484
+
485
+ function getPluginsByType<TNode>(plugins: readonly BlockPlugin<TNode>[]): PluginRegistry<TNode> {
486
+ const byType: PluginRegistry<TNode> = new Map();
364
487
  for (const plugin of plugins) {
365
488
  if (!BLOCK_TYPE.test(plugin.type)) {
366
489
  throw new Error(`Block plugin type "${plugin.type}" must be lower-case kebab-case, e.g. "chart".`);
367
490
  }
368
- if (byType.has(plugin.type)) throw new Error(`Block plugin type "${plugin.type}" is registered twice.`);
369
- byType.set(plugin.type, plugin);
491
+ const syntax = plugin.syntax ?? "fence";
492
+ const key = getPluginKey(syntax, plugin.type);
493
+ if (byType.has(key)) {
494
+ throw new Error(syntax === "fence" ? `Block plugin type "${plugin.type}" is registered twice.` : `Block plugin type "${plugin.type}" (directive) is registered twice.`);
495
+ }
496
+ byType.set(key, plugin);
370
497
  }
371
498
  return byType;
372
499
  }
373
500
 
501
+ function getTypes<TNode>(plugins: PluginRegistry<TNode>, syntax: BlockSyntax): Set<string> {
502
+ return new Set([...plugins.values()].filter((plugin) => (plugin.syntax ?? "fence") === syntax).map((plugin) => plugin.type));
503
+ }
504
+
374
505
  function createMarkdown<TNode>(
375
506
  options: RenderArticleOptions<TNode>,
376
- plugins: ReadonlyMap<string, BlockPlugin<TNode>>,
507
+ plugins: PluginRegistry<TNode>,
377
508
  state: RenderState,
378
509
  ): Markdown {
379
510
  const messages = options.messages ?? en.render;
@@ -383,7 +514,8 @@ function createMarkdown<TNode>(
383
514
  addImages(md, options.images);
384
515
  addFootnoteMarkup(md, messages);
385
516
  addExternalLinks(md, options.siteHosts ?? [], messages);
386
- addBlockTokens(md, new Set(plugins.keys()));
517
+ addBlockTokens(md, getTypes(plugins, "fence"));
518
+ addDirectiveRule(md, getTypes(plugins, "directive"));
387
519
  addHeadingIds(md, state);
388
520
  addGlossaryLinks(
389
521
  md,
@@ -442,10 +574,10 @@ export function renderArticle<TNode = unknown>(markdown: string, options: Render
442
574
  if (token.type !== BLOCK_TOKEN) return;
443
575
  addHtmlSegment(segments, md.renderer.render(tokens.slice(start, index), md.options, env));
444
576
  start = index + 1;
445
- const meta = token.meta as { type: string; info: string };
446
- const plugin = plugins.get(meta.type);
577
+ const meta = readBlockMeta(token);
578
+ const plugin = plugins.get(getPluginKey(meta.syntax, meta.type));
447
579
  if (plugin === undefined) return;
448
- const output = plugin.render({ type: meta.type, info: meta.info, content: token.content, article: options.article ?? {} });
580
+ const output = plugin.render({ ...meta, content: token.content, article: options.article ?? {} });
449
581
  if (output.kind === "html") {
450
582
  addHtmlSegment(segments, output.html);
451
583
  } else {
@@ -481,7 +613,29 @@ export function findArticleBlocks(markdown: string, plugins: readonly BlockPlugi
481
613
  const md = createMarkdown({ blocks: plugins }, byType, { headings: [], linkedTerms: [] });
482
614
  return md.parse(markdown, {}).flatMap((token) => {
483
615
  if (token.type !== BLOCK_TOKEN) return [];
484
- const meta = token.meta as { type: string; info: string };
485
- return [{ type: meta.type, info: meta.info, line: (token.map?.[0] ?? 0) + 1, requires: byType.get(meta.type)?.requires ?? [] }];
616
+ const meta = readBlockMeta(token);
617
+ const requires = byType.get(getPluginKey(meta.syntax, meta.type))?.requires ?? [];
618
+ return [{ type: meta.type, syntax: meta.syntax, info: meta.info, attributes: meta.attributes, line: (token.map?.[0] ?? 0) + 1, requires }];
486
619
  });
487
620
  }
621
+
622
+ /**
623
+ * The text with each plugin block that has a Markdown form (`BlockPlugin.markdown`) replaced by it;
624
+ * other blocks keep their source. For agents that read the article as Markdown.
625
+ */
626
+ export function replaceArticleBlocks(markdown: string, plugins: readonly BlockPlugin[], article: BlockArticle = {}): string {
627
+ if (!plugins.some((plugin) => plugin.markdown !== undefined)) return markdown;
628
+ const byType = getPluginsByType(plugins);
629
+ const md = createMarkdown({ blocks: plugins }, byType, { headings: [], linkedTerms: [] });
630
+ const lines = markdown.split("\n");
631
+ const blocks = md.parse(markdown, {}).filter((token) => token.type === BLOCK_TOKEN && token.map !== null);
632
+ for (const token of blocks.reverse()) {
633
+ const meta = readBlockMeta(token);
634
+ const plugin = byType.get(getPluginKey(meta.syntax, meta.type));
635
+ const [start = 0, end = start] = token.map ?? [];
636
+ if (plugin?.markdown === undefined) continue;
637
+ const replacement = plugin.markdown({ ...meta, content: token.content, article });
638
+ lines.splice(start, end - start, ...replacement.split("\n"));
639
+ }
640
+ return lines.join("\n");
641
+ }
@@ -10,7 +10,9 @@ export {
10
10
  publishArticle,
11
11
  type BlogContext,
12
12
  type ListArticlesFilter,
13
+ type PublishArticleOptions,
13
14
  } from "../db/articles.js";
15
+ export { articleHistorySchema, parseArticleHistory, type ArticleHistory, type ArticleHistoryMap } from "../db/history.js";
14
16
  export {
15
17
  runBlogPublish,
16
18
  type ArticleFile,