@in-the-loop-labs/pair-review 5.0.2 → 5.1.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.
- package/README.md +44 -3
- package/package.json +5 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
- package/public/js/utils/markdown.js +202 -65
- package/public/js/vendor/pierre-diffs-worker.js +3446 -5992
- package/public/js/vendor/pierre-diffs.js +731 -1047
- package/public/local.html +2 -0
- package/public/pr.html +2 -0
- package/src/ai/abort-signal-wiring.js +91 -46
- package/src/ai/analyzer.js +64 -11
- package/src/ai/antigravity-provider.js +31 -5
- package/src/ai/claude-cli.js +8 -2
- package/src/ai/claude-provider.js +34 -5
- package/src/ai/codex-provider.js +31 -5
- package/src/ai/copilot-provider.js +28 -4
- package/src/ai/cursor-agent-provider.js +32 -5
- package/src/ai/executable-provider.js +8 -2
- package/src/ai/index.js +1 -0
- package/src/ai/muse-provider.js +1224 -0
- package/src/ai/opencode-provider.js +31 -5
- package/src/ai/pi-provider.js +29 -4
- package/src/ai/provider.js +111 -28
- package/src/ai/stream-parser.js +71 -0
- package/src/config.js +35 -2
- package/src/database.js +96 -16
- package/src/git/fetch-helpers.js +48 -0
- package/src/git/pack-cleanup.js +120 -0
- package/src/git/worktree.js +65 -43
- package/src/main.js +243 -47
- package/src/routes/pr.js +2 -3
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ pair-review is a local web application for keeping humans in the loop with AI co
|
|
|
67
67
|
- **Local-First**: All data and processing happens on your machine - no cloud dependencies
|
|
68
68
|
- **GitHub-Familiar UI**: Interface feels instantly familiar to GitHub users
|
|
69
69
|
- **Human-in-the-Loop**: AI suggests, you decide
|
|
70
|
-
- **Multiple AI Providers**: Support for Claude, Antigravity, Codex, Copilot, OpenCode, Cursor, and
|
|
70
|
+
- **Multiple AI Providers**: Support for Claude, Antigravity, Codex, Copilot, OpenCode, Cursor, Pi, and Muse. Use your existing subscription!
|
|
71
71
|
- **Progressive**: Start simple with manual review, add AI analysis when you need it
|
|
72
72
|
|
|
73
73
|
## Workflows
|
|
@@ -373,6 +373,7 @@ consolidated suggestion layer the app shows by default):
|
|
|
373
373
|
{
|
|
374
374
|
"ok": true, // false on failure (see below)
|
|
375
375
|
"mode": "local", // "pr" or "local"
|
|
376
|
+
"consolidation": "success", // "success", "failed", "skipped", or null (see below)
|
|
376
377
|
"run": {
|
|
377
378
|
"id": "…", // analysis run id
|
|
378
379
|
"review_id": 1,
|
|
@@ -418,6 +419,25 @@ consolidated suggestion layer the app shows by default):
|
|
|
418
419
|
}
|
|
419
420
|
```
|
|
420
421
|
|
|
422
|
+
**`consolidation`** reports the outcome of the final cross-level (and, for a
|
|
423
|
+
council, cross-voice) consolidation step:
|
|
424
|
+
|
|
425
|
+
| Value | Meaning |
|
|
426
|
+
| --- | --- |
|
|
427
|
+
| `"success"` | Consolidation ran and merged the inputs. |
|
|
428
|
+
| `"failed"` | It threw. The run still completes and `ok` stays `true`, but `suggestions` is the raw union of every analysis level and every council voice rather than a merged review — expect duplicates and a much larger `count`. |
|
|
429
|
+
| `"skipped"` | There was nothing to merge — a council that resolved to a single voice or a single level, or an executable provider that returns its own final set. The suggestion set is the intended one; this is a healthy outcome, not a degraded one. |
|
|
430
|
+
| `null` | The run recorded no consolidation stage. |
|
|
431
|
+
|
|
432
|
+
Only `"failed"` indicates degraded output, so branch on that value rather than on
|
|
433
|
+
`!= "success"` — a single-voice council legitimately reports `"skipped"`. The
|
|
434
|
+
human-readable (non-`--json`) output prints a warning for `"failed"` only.
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
# Treat a degraded run as a failure
|
|
438
|
+
[ "$(echo "$result" | jq -r '.consolidation')" = "failed" ] && echo "unconsolidated!" >&2
|
|
439
|
+
```
|
|
440
|
+
|
|
421
441
|
**On failure** (invalid flags, an unreadable `--instructions-file`, or an
|
|
422
442
|
analysis error raised while running the headless flow) `--json` emits a JSON
|
|
423
443
|
document on stdout — a compact failure envelope — and the exit code is non-zero,
|
|
@@ -558,6 +578,7 @@ pair-review supports several environment variables for customizing behavior:
|
|
|
558
578
|
| `PAIR_REVIEW_OPENCODE_CMD` | Custom command to invoke OpenCode CLI | `opencode` |
|
|
559
579
|
| `PAIR_REVIEW_CURSOR_AGENT_CMD` | Custom command to invoke Cursor Agent CLI | `agent` |
|
|
560
580
|
| `PAIR_REVIEW_PI_CMD` | Custom command to invoke Pi CLI | `pi` |
|
|
581
|
+
| `PAIR_REVIEW_MUSE_CMD` | Custom command to invoke Muse Code CLI | `muse` |
|
|
561
582
|
|
|
562
583
|
**Note:** `GITHUB_TOKEN` is the standard environment variable used by many GitHub tools (gh CLI, GitHub Actions, etc.). When set, it takes precedence over the `github_token` field in the config file.
|
|
563
584
|
|
|
@@ -615,12 +636,32 @@ pair-review integrates with AI providers via their CLI tools:
|
|
|
615
636
|
- **OpenCode**: Uses OpenCode CLI (requires model configuration)
|
|
616
637
|
- **Cursor**: Uses Cursor Agent CLI (streaming output with sandbox mode)
|
|
617
638
|
- **Pi**: Uses Pi coding agent CLI (requires model configuration)
|
|
639
|
+
- **Muse**: Uses Meta's Muse Code CLI (`muse`), a terminal coding agent released in beta in August 2026 and powered by the Muse Spark 1.2 model. Install it with the single shell command from Meta's Muse Code install instructions (macOS/Linux), then authenticate once with `muse login` (credentials are stored in `~/.config/muse/auth.json`). pair-review drives it headlessly via `muse exec --json`. Within pair-review, Muse is analysis-only: the CLI exposes no ACP server mode, so there is no Muse chat provider.
|
|
618
640
|
|
|
619
641
|
You can select your preferred provider and model in the repository settings UI.
|
|
620
642
|
|
|
621
643
|
#### Built-in vs. Configurable Providers
|
|
622
644
|
|
|
623
|
-
Most providers (Claude, Antigravity, Codex, Copilot) come with built-in model definitions. **OpenCode and Pi are different** - they have no built-in models and require you to configure which models to use.
|
|
645
|
+
Most providers (Claude, Antigravity, Codex, Copilot, Muse) come with built-in model definitions. **OpenCode and Pi are different** - they have no built-in models and require you to configure which models to use.
|
|
646
|
+
|
|
647
|
+
##### Muse models and the contributor tradeoff
|
|
648
|
+
|
|
649
|
+
Muse exposes its built-in models as reasoning-effort variants over two underlying CLI models:
|
|
650
|
+
|
|
651
|
+
| Model ID | Tier | Underlying CLI model |
|
|
652
|
+
|----------|------|----------------------|
|
|
653
|
+
| `muse-spark-1.2-ultra` | thorough | `muse-spark-1.2` |
|
|
654
|
+
| `muse-spark-1.2-contributor-ultra` | thorough | `muse-spark-1.2-contributor` |
|
|
655
|
+
| `muse-spark-1.2-xhigh` | thorough | `muse-spark-1.2` |
|
|
656
|
+
| `muse-spark-1.2-contributor-xhigh` | thorough | `muse-spark-1.2-contributor` |
|
|
657
|
+
| `muse-spark-1.2-high` | balanced (**default**) | `muse-spark-1.2` |
|
|
658
|
+
| `muse-spark-1.2-contributor-high` | balanced | `muse-spark-1.2-contributor` |
|
|
659
|
+
| `muse-spark-1.2-low` | fast | `muse-spark-1.2` |
|
|
660
|
+
| `muse-spark-1.2-contributor-low` | fast | `muse-spark-1.2-contributor` |
|
|
661
|
+
|
|
662
|
+
`muse-spark-1.2` and `muse-spark` are convenience aliases for `muse-spark-1.2-high`. `muse-spark-1.2-contributor` is likewise an alias for `muse-spark-1.2-contributor-high` — worth knowing because it is the bare CLI model name, so `--model muse-spark-1.2-contributor` selects the data-sharing tier described below rather than erroring out.
|
|
663
|
+
|
|
664
|
+
The `-contributor` models are substantially cheaper ($0.10/M input, $0.20/M output versus $1.25/M and $4.25/M) **because Meta may use their content for product improvement**. Since a code review sends your diff and surrounding source to the model, pair-review deliberately defaults to the non-contributor `muse-spark-1.2-high` — opting into data sharing is always an explicit choice you make by selecting a `-contributor` model, whether by its full ID or by that alias.
|
|
624
665
|
|
|
625
666
|
#### Configuring Custom Models
|
|
626
667
|
|
|
@@ -802,7 +843,7 @@ Configure your preferred models in `providers.pi.models` — see [AI Provider Co
|
|
|
802
843
|
}
|
|
803
844
|
```
|
|
804
845
|
|
|
805
|
-
Available chat provider IDs: `pi`, `claude`, `codex`, `copilot-acp`, `opencode-acp`, `cursor-acp`. Each supports `command`, `args` (replaces defaults), `extra_args` (appends), and `env` overrides. Codex chat also supports `sandbox`: use `workspace-write` by default, or `read-only` for discussion-only sessions. (Antigravity
|
|
846
|
+
Available chat provider IDs: `pi`, `claude`, `codex`, `copilot-acp`, `opencode-acp`, `cursor-acp`. Each supports `command`, `args` (replaces defaults), `extra_args` (appends), and `env` overrides. Codex chat also supports `sandbox`: use `workspace-write` by default, or `read-only` for discussion-only sessions. (Antigravity and Muse are analysis-only providers — neither CLI has an ACP mode, so there are no Antigravity or Muse chat providers.)
|
|
806
847
|
|
|
807
848
|
**Keyboard shortcut:** Press `p` then `c` to toggle the chat panel.
|
|
808
849
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@in-the-loop-labs/pair-review",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Your AI-powered code review partner - Close the feedback loop with AI coding agents",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
"cursor",
|
|
52
52
|
"copilot",
|
|
53
53
|
"codex",
|
|
54
|
-
"antigravity"
|
|
54
|
+
"antigravity",
|
|
55
|
+
"muse"
|
|
55
56
|
],
|
|
56
57
|
"author": "Tim Perkins",
|
|
57
58
|
"license": "Apache-2.0",
|
|
@@ -67,7 +68,7 @@
|
|
|
67
68
|
"@agentclientprotocol/sdk": "^0.14.1",
|
|
68
69
|
"@modelcontextprotocol/sdk": "^1.25.3",
|
|
69
70
|
"@octokit/rest": "^19.0.11",
|
|
70
|
-
"@pierre/diffs": "^1.
|
|
71
|
+
"@pierre/diffs": "^1.2.12",
|
|
71
72
|
"better-sqlite3": "^12.11.1",
|
|
72
73
|
"express": "^4.18.2",
|
|
73
74
|
"glob": "^13.0.6",
|
|
@@ -84,6 +85,7 @@
|
|
|
84
85
|
"@changesets/cli": "^2.29.8",
|
|
85
86
|
"@playwright/test": "^1.57.0",
|
|
86
87
|
"@vitest/coverage-v8": "^4.0.16",
|
|
88
|
+
"dompurify": "^3.2.6",
|
|
87
89
|
"esbuild": "^0.27.4",
|
|
88
90
|
"highlight.js": "^11.11.1",
|
|
89
91
|
"jsdom": "^29.0.1",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pair-review",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "pair-review app integration — Open PRs and local changes in the pair-review web UI, run server-side AI analysis, and address review feedback. Requires the pair-review MCP server.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "in-the-loop-labs",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pair-loop",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Agent-orchestrated review loop — implement, review with pair-review multi-model councils via the headless CLI, triage, fix, and repeat until a final review returns no blockers. MCP-independent.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "in-the-loop-labs",
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
// Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
/**
|
|
3
|
-
* Safe markdown renderer for comments
|
|
4
|
-
*
|
|
3
|
+
* Safe markdown renderer for comments.
|
|
4
|
+
*
|
|
5
|
+
* Uses markdown-it to render markdown and DOMPurify to sanitize the result.
|
|
6
|
+
* Enabling raw HTML (`html: true`) lets us render the GitHub-supported inline
|
|
7
|
+
* HTML subset (e.g. <sub>, <sup>, <kbd>) and lets DOMPurify strip HTML comments
|
|
8
|
+
* (e.g. tracking markers) from the rendered view. DOMPurify is what keeps this
|
|
9
|
+
* safe from XSS — it must be present before we enable raw HTML.
|
|
5
10
|
*/
|
|
6
11
|
|
|
7
12
|
/**
|
|
@@ -21,82 +26,214 @@ function escapeHtmlAttribute(text) {
|
|
|
21
26
|
.replace(/'/g, ''');
|
|
22
27
|
}
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
29
|
+
/**
|
|
30
|
+
* GitHub-like allowlist for inline HTML permitted in comment bodies.
|
|
31
|
+
* Standard markdown output tags plus the safe inline subset GitHub renders.
|
|
32
|
+
*/
|
|
33
|
+
const ALLOWED_TAGS = [
|
|
34
|
+
// Standard markdown output
|
|
35
|
+
'p', 'a', 'code', 'pre', 'blockquote',
|
|
36
|
+
'ul', 'ol', 'li',
|
|
37
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
38
|
+
'em', 'strong', 's', 'hr', 'br', 'span',
|
|
39
|
+
'table', 'thead', 'tbody', 'tr', 'th', 'td',
|
|
40
|
+
'img',
|
|
41
|
+
// GitHub-supported inline HTML subset
|
|
42
|
+
'sub', 'sup', 'kbd', 'ins', 'del', 'mark',
|
|
43
|
+
'details', 'summary', 'abbr'
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const ALLOWED_ATTR = [
|
|
47
|
+
'href', 'title', 'target', 'rel',
|
|
48
|
+
'src', 'alt', 'align', 'class',
|
|
49
|
+
'start', 'colspan', 'rowspan'
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Configure a markdown-it instance with the project's rendering options.
|
|
54
|
+
* Raw HTML is enabled here; sanitization happens separately via DOMPurify.
|
|
55
|
+
* @param {function} markdownit - the markdown-it factory (window.markdownit)
|
|
56
|
+
* @param {object} [opts]
|
|
57
|
+
* @param {boolean} [opts.html=true] - allow raw HTML tokens
|
|
58
|
+
* @param {object} [opts.emoji] - markdown-it-emoji plugin (optional)
|
|
59
|
+
* @returns {object} configured markdown-it instance
|
|
60
|
+
*/
|
|
61
|
+
function configureMarkdownIt(markdownit, opts = {}) {
|
|
62
|
+
const html = opts.html !== undefined ? opts.html : true;
|
|
63
|
+
|
|
64
|
+
const md = markdownit({
|
|
65
|
+
html, // Allow raw HTML; DOMPurify sanitizes the output
|
|
66
|
+
xhtmlOut: false, // Don't use self-closing tags
|
|
67
|
+
breaks: true, // Convert \n to <br>
|
|
68
|
+
langPrefix: 'language-', // CSS class prefix for code blocks
|
|
69
|
+
linkify: true, // Auto-convert URLs to links
|
|
70
|
+
typographer: true // Enable smartquotes and other typographic replacements
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Enable emoji shortcode support (e.g., :smile: -> 😄)
|
|
74
|
+
if (opts.emoji) {
|
|
75
|
+
md.use(opts.emoji);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Configure link rendering to open in new tab and add security.
|
|
79
|
+
// (DOMPurify's afterSanitizeAttributes hook re-applies these as a backstop.)
|
|
80
|
+
const defaultLinkRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
|
|
81
|
+
return self.renderToken(tokens, idx, options);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
md.renderer.rules.link_open = function(tokens, idx, options, env, self) {
|
|
85
|
+
const token = tokens[idx];
|
|
86
|
+
token.attrPush(['target', '_blank']);
|
|
87
|
+
token.attrPush(['rel', 'noopener noreferrer']);
|
|
88
|
+
return defaultLinkRender(tokens, idx, options, env, self);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return md;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sanitize rendered HTML with DOMPurify using the project allowlist.
|
|
96
|
+
* Removes HTML comments (default) and dangerous tags/attributes, converts
|
|
97
|
+
* markdown-it's table-alignment inline style into the allowlisted `align`
|
|
98
|
+
* attribute (so arbitrary inline styles never need to be permitted), forces
|
|
99
|
+
* safe link attributes on anchors, and scopes `class` to markdown code
|
|
100
|
+
* language hints only.
|
|
101
|
+
* @param {object} purify - a configured DOMPurify instance
|
|
102
|
+
* @param {string} html - HTML to sanitize
|
|
103
|
+
* @returns {string} sanitized HTML
|
|
104
|
+
*/
|
|
105
|
+
function sanitizeHtml(purify, html) {
|
|
106
|
+
// Hooks are registered idempotently (removed then re-added) so repeated
|
|
107
|
+
// sanitize() calls on the same instance don't stack duplicates.
|
|
41
108
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
* Escape HTML characters (fallback for when markdown rendering fails)
|
|
56
|
-
* NOTE: This only escapes <, >, and &. It does NOT escape quotes.
|
|
57
|
-
* Use escapeHtmlAttribute() when placing content in HTML attributes.
|
|
58
|
-
* @param {string} text - Text to escape
|
|
59
|
-
* @returns {string} Escaped text
|
|
60
|
-
*/
|
|
61
|
-
function escapeHtml(text) {
|
|
62
|
-
if (!text) return '';
|
|
63
|
-
const div = document.createElement('div');
|
|
64
|
-
div.textContent = text;
|
|
65
|
-
return div.innerHTML;
|
|
109
|
+
// markdown-it encodes table column alignment as an inline `text-align` style.
|
|
110
|
+
// Convert it to the allowlisted `align` attribute BEFORE sanitization so we
|
|
111
|
+
// never have to permit arbitrary inline styles (which could hide or cover
|
|
112
|
+
// review UI via position/z-index/display). The `style` attribute itself is
|
|
113
|
+
// then dropped by the allowlist.
|
|
114
|
+
purify.removeHook('beforeSanitizeAttributes');
|
|
115
|
+
purify.addHook('beforeSanitizeAttributes', function (node) {
|
|
116
|
+
const tag = node.tagName;
|
|
117
|
+
if (tag === 'TH' || tag === 'TD') {
|
|
118
|
+
const align = node.style && node.style.textAlign;
|
|
119
|
+
if (align === 'left' || align === 'center' || align === 'right') {
|
|
120
|
+
node.setAttribute('align', align);
|
|
121
|
+
}
|
|
66
122
|
}
|
|
123
|
+
});
|
|
67
124
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
125
|
+
purify.removeHook('afterSanitizeAttributes');
|
|
126
|
+
purify.addHook('afterSanitizeAttributes', function (node) {
|
|
127
|
+
const tag = node.tagName;
|
|
128
|
+
// Force safe rel/target on links regardless of DOMPurify version defaults.
|
|
129
|
+
if (tag === 'A' && node.getAttribute('href')) {
|
|
130
|
+
node.setAttribute('target', '_blank');
|
|
131
|
+
node.setAttribute('rel', 'noopener noreferrer');
|
|
132
|
+
}
|
|
133
|
+
// Scope `class` to expected markdown output: only `language-*` on code/pre.
|
|
134
|
+
// Stops rendered repo content from reusing layout-sensitive app classes.
|
|
135
|
+
if (node.hasAttribute('class')) {
|
|
136
|
+
const kept =
|
|
137
|
+
tag === 'CODE' || tag === 'PRE'
|
|
138
|
+
? node.getAttribute('class').split(/\s+/).filter((c) => /^language-[\w-]+$/.test(c))
|
|
139
|
+
: [];
|
|
140
|
+
if (kept.length) {
|
|
141
|
+
node.setAttribute('class', kept.join(' '));
|
|
142
|
+
} else {
|
|
143
|
+
node.removeAttribute('class');
|
|
83
144
|
}
|
|
84
145
|
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return purify.sanitize(html, {
|
|
149
|
+
ALLOWED_TAGS,
|
|
150
|
+
ALLOWED_ATTR,
|
|
151
|
+
ALLOW_DATA_ATTR: false,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Create the renderMarkdown function.
|
|
157
|
+
* When a DOMPurify instance is provided, the markdown-it output is sanitized
|
|
158
|
+
* (safe to enable raw HTML). Without it, callers should pass an md configured
|
|
159
|
+
* with `html: false` so no unsanitized HTML is ever emitted.
|
|
160
|
+
* @param {object} config
|
|
161
|
+
* @param {object} config.md - configured markdown-it instance
|
|
162
|
+
* @param {object} [config.purify] - DOMPurify instance (optional)
|
|
163
|
+
* @param {function} [config.escape] - fallback escaper for render errors
|
|
164
|
+
* @returns {function(string): string} renderMarkdown
|
|
165
|
+
*/
|
|
166
|
+
function createRenderMarkdown(config) {
|
|
167
|
+
const { md, purify, escape } = config;
|
|
168
|
+
const fallbackEscape = escape || ((text) => escapeHtmlAttribute(text));
|
|
169
|
+
|
|
170
|
+
return function renderMarkdown(text) {
|
|
171
|
+
if (!text) return '';
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const rendered = md.render(text);
|
|
175
|
+
return purify ? sanitizeHtml(purify, rendered) : rendered;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
// eslint-disable-next-line no-console
|
|
178
|
+
console.error('Markdown rendering error:', error);
|
|
179
|
+
// Fall back to escaped text if rendering fails
|
|
180
|
+
return fallbackEscape(text);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Wire the markdown renderer onto a window/global object.
|
|
187
|
+
* Raw HTML is only enabled when a DOMPurify instance is present to sanitize it;
|
|
188
|
+
* otherwise we fall back to html:false so unsanitized HTML is never emitted.
|
|
189
|
+
* Defines `win.renderMarkdown` and `win.markdownRenderer`. No-op without
|
|
190
|
+
* `win.markdownit`.
|
|
191
|
+
* @param {object} win - a window-like object (expects markdownit, optionally
|
|
192
|
+
* DOMPurify, markdownitEmoji, and document)
|
|
193
|
+
*/
|
|
194
|
+
function initMarkdownGlobals(win) {
|
|
195
|
+
if (!win || !win.markdownit) return;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Escape HTML characters (fallback for when markdown rendering fails).
|
|
199
|
+
* NOTE: This only escapes <, >, and &. It does NOT escape quotes.
|
|
200
|
+
* Use escapeHtmlAttribute() when placing content in HTML attributes.
|
|
201
|
+
*/
|
|
202
|
+
function escapeHtml(text) {
|
|
203
|
+
if (!text) return '';
|
|
204
|
+
const div = win.document.createElement('div');
|
|
205
|
+
div.textContent = text;
|
|
206
|
+
return div.innerHTML;
|
|
207
|
+
}
|
|
85
208
|
|
|
86
|
-
|
|
87
|
-
|
|
209
|
+
// Only enable raw HTML when DOMPurify is available to sanitize it.
|
|
210
|
+
const purify = win.DOMPurify || null;
|
|
211
|
+
const md = configureMarkdownIt(win.markdownit, {
|
|
212
|
+
html: !!purify,
|
|
213
|
+
emoji: win.markdownitEmoji || undefined
|
|
214
|
+
});
|
|
88
215
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
216
|
+
win.renderMarkdown = createRenderMarkdown({ md, purify, escape: escapeHtml });
|
|
217
|
+
// Also expose the markdown instance for advanced usage if needed.
|
|
218
|
+
win.markdownRenderer = md;
|
|
92
219
|
}
|
|
93
220
|
|
|
94
|
-
//
|
|
221
|
+
// Browser-only code: markdown rendering requires the markdown-it library.
|
|
95
222
|
if (typeof window !== 'undefined') {
|
|
223
|
+
initMarkdownGlobals(window);
|
|
224
|
+
// Export escapeHtmlAttribute regardless of markdown-it availability.
|
|
96
225
|
window.escapeHtmlAttribute = escapeHtmlAttribute;
|
|
97
226
|
}
|
|
98
227
|
|
|
99
228
|
// Export for Node.js testing
|
|
100
229
|
if (typeof module !== 'undefined' && module.exports) {
|
|
101
|
-
module.exports = {
|
|
230
|
+
module.exports = {
|
|
231
|
+
escapeHtmlAttribute,
|
|
232
|
+
configureMarkdownIt,
|
|
233
|
+
sanitizeHtml,
|
|
234
|
+
createRenderMarkdown,
|
|
235
|
+
initMarkdownGlobals,
|
|
236
|
+
ALLOWED_TAGS,
|
|
237
|
+
ALLOWED_ATTR
|
|
238
|
+
};
|
|
102
239
|
}
|