@lightblues/jig 0.1.5 → 0.1.6

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). */
@@ -40,6 +40,7 @@ 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;
43
44
  // Parse time: a refused URL is not a link at all (its source stays as text).
44
45
  md.validateLink = (url) => urlAllowed(url, 'link', allow) || (images && urlAllowed(url, 'image', allow));
45
46
  if (opts.cjk)
@@ -63,6 +64,25 @@ export function createMarkdown(opts = {}) {
63
64
  const href = a === null ? null : String(a);
64
65
  if (href === null || !urlAllowed(href, 'link', allow))
65
66
  dropAttr(t, 'href');
67
+ else if (map && !external(href) && !SCHEME.test(href)) {
68
+ const mapped = map(href);
69
+ if (mapped === null)
70
+ return unlink(tokens, idx);
71
+ if (mapped !== undefined && !urlAllowed(mapped, 'link', allow))
72
+ dropAttr(t, 'href');
73
+ else {
74
+ if (mapped !== undefined)
75
+ t.attrSet('href', mapped);
76
+ if (mapped === undefined || !external(mapped))
77
+ t.attrSet('data-jig-internal', '');
78
+ else {
79
+ if (target)
80
+ t.attrSet('target', target);
81
+ if (rel)
82
+ t.attrSet('rel', rel);
83
+ }
84
+ }
85
+ }
66
86
  else if (external(href)) {
67
87
  if (target)
68
88
  t.attrSet('target', target);
@@ -79,6 +99,9 @@ export function createMarkdown(opts = {}) {
79
99
  return md.utils.escapeHtml(self.renderInlineAsText(t.children ?? [], options, env));
80
100
  return image(tokens, idx, options, env, self);
81
101
  };
102
+ md.renderer.rules.jig_unlinked_close = () => '';
103
+ if (opts.taskLists)
104
+ useTaskLists(md);
82
105
  for (const p of opts.plugins ?? []) {
83
106
  if (Array.isArray(p)) {
84
107
  const [plugin, ...params] = p;
@@ -337,6 +360,41 @@ function boundInlineMath(md, texmath) {
337
360
  const rex = new RegExp(`\\$((?:[^\\s\\\\])|(?:\\S.{0,${MAX_INLINE_MATH - 2}}?[^\\s\\\\]))\\$`, 'gy');
338
361
  md.inline.ruler.at('math_inline', texmath.inline({ ...rule, rex }));
339
362
  }
363
+ /** Renders a link as its text: the open and its matching close render nothing. */
364
+ function unlink(tokens, idx) {
365
+ let depth = 0;
366
+ for (let i = idx + 1; i < tokens.length; i++) {
367
+ const t = tokens[i];
368
+ if (t.type === 'link_open')
369
+ depth++;
370
+ else if (t.type === 'link_close' && depth-- === 0) {
371
+ t.type = 'jig_unlinked_close';
372
+ break;
373
+ }
374
+ }
375
+ return '';
376
+ }
377
+ const TASK = /^\[([ xX])\][ \u00a0]/;
378
+ /** GFM task lists, read-only: `- [ ] a` / `- [x] b`. */
379
+ function useTaskLists(md) {
380
+ md.core.ruler.push('jig_task_lists', (state) => {
381
+ const tokens = state.tokens;
382
+ for (let i = 2; i < tokens.length; i++) {
383
+ const inline = tokens[i];
384
+ if (inline.type !== 'inline' || tokens[i - 1].type !== 'paragraph_open' || tokens[i - 2].type !== 'list_item_open')
385
+ continue;
386
+ const first = inline.children?.[0];
387
+ const m = first && first.type === 'text' ? TASK.exec(first.content) : null;
388
+ if (!m || !first)
389
+ continue;
390
+ first.content = first.content.slice(m[0].length);
391
+ const box = new state.Token('html_inline', '', 0);
392
+ box.content = `<input type="checkbox" class="jig-task" disabled${m[1] === ' ' ? '' : ' checked'}> `;
393
+ inline.children.unshift(box);
394
+ tokens[i - 2].attrJoin('class', 'jig-task-item');
395
+ }
396
+ });
397
+ }
340
398
  function dropAttr(t, name) {
341
399
  if (t.attrs)
342
400
  t.attrs = t.attrs.filter(([k]) => k !== name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightblues/jig",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
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",