@lightblues/jig 0.1.5 → 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.
@@ -67,7 +67,18 @@ export type MarkdownOptions = {
67
67
  * `vbscript:`, `file:` and `data:` (except raster `data:image/…` for images) are refused whatever it says.
68
68
  */
69
69
  allow?: (url: string, kind: LinkKind) => boolean | undefined;
70
+ /**
71
+ * Rewrite an internal link (relative or root-relative, after `allow` admitted it): return the new href, `null` to
72
+ * render the link as its plain text (no `<a>`), or `undefined` to keep it. The result is checked by the URL rules
73
+ * again. With `map` set, internal links carry `data-jig-internal`, for a host's delegated click handler (SPA navigation).
74
+ */
75
+ map?: (href: string) => string | null | undefined;
70
76
  };
77
+ /**
78
+ * GFM task lists: a list item starting `[ ]` or `[x]` gets a read-only checkbox
79
+ * (`<input type="checkbox" class="jig-task" disabled>`) and its `<li>` the class `jig-task-item`. Off by default.
80
+ */
81
+ taskLists?: boolean;
71
82
  /** Images (`![alt](src)`). Off by default: an image renders as its alt text. */
72
83
  images?: boolean;
73
84
  /** Single newlines become `<br>`. Default `true` (model and chat text). */
@@ -105,6 +116,11 @@ export type MarkdownOptions = {
105
116
  export type RenderOptions = {
106
117
  /** The text is a partial answer: close what it left open first (`closeOpenMarkdown`). */
107
118
  streaming?: boolean;
119
+ /**
120
+ * Per-render `links.map` override — avoids a module variable when route mapping depends on per-page data
121
+ * (forge-018, assay-020). Replaces the instance-level `links.map` for this render only.
122
+ */
123
+ map?: (href: string) => string | null | undefined;
108
124
  };
109
125
  /** `renderDocument`'s result: the HTML with its slots, and what goes in them. */
110
126
  export type MarkdownDocument = BlockEnv & {
@@ -40,6 +40,9 @@ export function createMarkdown(opts = {}) {
40
40
  const images = opts.images === true;
41
41
  const rel = opts.links?.rel ?? DEFAULT_REL;
42
42
  const target = opts.links?.target;
43
+ const map = opts.links?.map;
44
+ // Per-render override (RenderOptions.map): set around each md.render call and cleared afterwards.
45
+ let renderMap;
43
46
  // Parse time: a refused URL is not a link at all (its source stays as text).
44
47
  md.validateLink = (url) => urlAllowed(url, 'link', allow) || (images && urlAllowed(url, 'image', allow));
45
48
  if (opts.cjk)
@@ -63,6 +66,25 @@ export function createMarkdown(opts = {}) {
63
66
  const href = a === null ? null : String(a);
64
67
  if (href === null || !urlAllowed(href, 'link', allow))
65
68
  dropAttr(t, 'href');
69
+ else if ((map || renderMap) && !external(href) && !SCHEME.test(href)) {
70
+ const mapped = (renderMap ?? map)(href);
71
+ if (mapped === null)
72
+ return unlink(tokens, idx);
73
+ if (mapped !== undefined && !urlAllowed(mapped, 'link', allow))
74
+ dropAttr(t, 'href');
75
+ else {
76
+ if (mapped !== undefined)
77
+ t.attrSet('href', mapped);
78
+ if (mapped === undefined || !external(mapped))
79
+ t.attrSet('data-jig-internal', '');
80
+ else {
81
+ if (target)
82
+ t.attrSet('target', target);
83
+ if (rel)
84
+ t.attrSet('rel', rel);
85
+ }
86
+ }
87
+ }
66
88
  else if (external(href)) {
67
89
  if (target)
68
90
  t.attrSet('target', target);
@@ -79,6 +101,9 @@ export function createMarkdown(opts = {}) {
79
101
  return md.utils.escapeHtml(self.renderInlineAsText(t.children ?? [], options, env));
80
102
  return image(tokens, idx, options, env, self);
81
103
  };
104
+ md.renderer.rules.jig_unlinked_close = () => '';
105
+ if (opts.taskLists)
106
+ useTaskLists(md);
82
107
  for (const p of opts.plugins ?? []) {
83
108
  if (Array.isArray(p)) {
84
109
  const [plugin, ...params] = p;
@@ -90,16 +115,22 @@ export function createMarkdown(opts = {}) {
90
115
  const blocks = useBlocks(md, opts);
91
116
  const strip = opts.streamingStrip;
92
117
  const doc = (text, r, out) => {
93
- if (!r.streaming)
94
- return md.render(text, blocks ? { [ENV]: out } : {});
95
- if (!blocks)
96
- return md.render(closeOpenMarkdown(text, strip ? { strip } : {}));
97
- // Which fence, if any, the closer added: that block is still being written.
98
- const s = strip ? stripTail(text, strip) : text;
99
- const closed = closeOpenMarkdown(s);
100
- const tail = closed.slice(s.length);
101
- const open = closed.startsWith(s) && /^\n?(`{3,}|~{3,})$/.test(tail);
102
- return md.render(closed, { [ENV]: out, [OPEN]: open });
118
+ renderMap = r.map;
119
+ try {
120
+ if (!r.streaming)
121
+ return md.render(text, blocks ? { [ENV]: out } : {});
122
+ if (!blocks)
123
+ return md.render(closeOpenMarkdown(text, strip ? { strip } : {}));
124
+ // Which fence, if any, the closer added: that block is still being written.
125
+ const s = strip ? stripTail(text, strip) : text;
126
+ const closed = closeOpenMarkdown(s);
127
+ const tail = closed.slice(s.length);
128
+ const open = closed.startsWith(s) && /^\n?(`{3,}|~{3,})$/.test(tail);
129
+ return md.render(closed, { [ENV]: out, [OPEN]: open });
130
+ }
131
+ finally {
132
+ renderMap = undefined;
133
+ }
103
134
  };
104
135
  return {
105
136
  md,
@@ -337,6 +368,41 @@ function boundInlineMath(md, texmath) {
337
368
  const rex = new RegExp(`\\$((?:[^\\s\\\\])|(?:\\S.{0,${MAX_INLINE_MATH - 2}}?[^\\s\\\\]))\\$`, 'gy');
338
369
  md.inline.ruler.at('math_inline', texmath.inline({ ...rule, rex }));
339
370
  }
371
+ /** Renders a link as its text: the open and its matching close render nothing. */
372
+ function unlink(tokens, idx) {
373
+ let depth = 0;
374
+ for (let i = idx + 1; i < tokens.length; i++) {
375
+ const t = tokens[i];
376
+ if (t.type === 'link_open')
377
+ depth++;
378
+ else if (t.type === 'link_close' && depth-- === 0) {
379
+ t.type = 'jig_unlinked_close';
380
+ break;
381
+ }
382
+ }
383
+ return '';
384
+ }
385
+ const TASK = /^\[([ xX])\][ \u00a0]/;
386
+ /** GFM task lists, read-only: `- [ ] a` / `- [x] b`. */
387
+ function useTaskLists(md) {
388
+ md.core.ruler.push('jig_task_lists', (state) => {
389
+ const tokens = state.tokens;
390
+ for (let i = 2; i < tokens.length; i++) {
391
+ const inline = tokens[i];
392
+ if (inline.type !== 'inline' || tokens[i - 1].type !== 'paragraph_open' || tokens[i - 2].type !== 'list_item_open')
393
+ continue;
394
+ const first = inline.children?.[0];
395
+ const m = first && first.type === 'text' ? TASK.exec(first.content) : null;
396
+ if (!m || !first)
397
+ continue;
398
+ first.content = first.content.slice(m[0].length);
399
+ const box = new state.Token('html_inline', '', 0);
400
+ box.content = `<input type="checkbox" class="jig-task" disabled${m[1] === ' ' ? '' : ' checked'}> `;
401
+ inline.children.unshift(box);
402
+ tokens[i - 2].attrJoin('class', 'jig-task-item');
403
+ }
404
+ });
405
+ }
340
406
  function dropAttr(t, name) {
341
407
  if (t.attrs)
342
408
  t.attrs = t.attrs.filter(([k]) => k !== name);
@@ -30,5 +30,6 @@ export declare const MERMAID_CONFIG: {
30
30
  * shows the source as escaped code with class `jig-mermaid-error`. Resolves to whether it drew. `mermaid` is the module,
31
31
  * or a loader (`() => import('mermaid')`) called on first use.
32
32
  */
33
- export declare function drawMermaid(el: Element, source: string, mermaid: MermaidSource, o?: DrawMermaidOptions): Promise<boolean>;
33
+ /** Draws `source` into `el`. Resolves to `null` on success, or the parse/render error message on failure (forge-018). */
34
+ export declare function drawMermaid(el: Element, source: string, mermaid: MermaidSource, o?: DrawMermaidOptions): Promise<null | string>;
34
35
  export {};
@@ -50,6 +50,7 @@ function scrub(root) {
50
50
  * shows the source as escaped code with class `jig-mermaid-error`. Resolves to whether it drew. `mermaid` is the module,
51
51
  * or a loader (`() => import('mermaid')`) called on first use.
52
52
  */
53
+ /** Draws `source` into `el`. Resolves to `null` on success, or the parse/render error message on failure (forge-018). */
53
54
  export async function drawMermaid(el, source, mermaid, o = {}) {
54
55
  const id = `jig-mmd-${++seq}`;
55
56
  const doc = el.ownerDocument;
@@ -72,9 +73,9 @@ export async function drawMermaid(el, source, mermaid, o = {}) {
72
73
  view.className = 'jig-mermaid';
73
74
  view.append(tpl.content);
74
75
  el.replaceChildren(view);
75
- return true;
76
+ return null;
76
77
  }
77
- catch {
78
+ catch (err) {
78
79
  cleanup();
79
80
  const pre = doc.createElement('pre');
80
81
  pre.className = 'jig-mermaid-error';
@@ -83,6 +84,6 @@ export async function drawMermaid(el, source, mermaid, o = {}) {
83
84
  code.textContent = source;
84
85
  pre.append(code);
85
86
  el.replaceChildren(pre);
86
- return false;
87
+ return err instanceof Error ? err.message : String(err);
87
88
  }
88
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightblues/jig",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Reusable parts for my apps: agent runtimes (Paseo, pi) that speak AG-UI, a thin thread-following client, one markdown renderer, and more as they prove reusable.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -87,7 +87,8 @@
87
87
  "typecheck": "tsc -p tsconfig.json --noEmit",
88
88
  "test": "node --test --experimental-strip-types 'test/**/*.test.ts'",
89
89
  "test:contract": "node --test --experimental-strip-types 'test/agent-paseo-contract.ts'",
90
- "prepack": "npm run build"
90
+ "prepack": "npm run build",
91
+ "gallery": "node --experimental-strip-types scripts/gallery.ts"
91
92
  },
92
93
  "devDependencies": {
93
94
  "@ag-ui/core": "1.0.5",