Skip to content

Commit b780739

Browse files
authored
feat(annotate): support HTML files and URL annotation (#545)
* fix(annotate): sanitize dangerous link protocols in markdown renderer Block javascript:, data:, and vbscript: URLs in InlineMarkdown link rendering. Links with dangerous protocols render as plain text instead of clickable anchors. Uses a blocklist approach so existing links with custom protocols (obsidian://, vscode://, Windows C:\ paths) continue to work. For provenance purposes, this commit was AI assisted. * feat(annotate): add HTML-to-markdown and URL-to-markdown utilities - html-to-markdown.ts: Turndown wrapper with GFM table rule, strips script/style/noscript tags - url-to-markdown.ts: Jina Reader (free, returns markdown) with fetch+Turndown fallback. Warns on Jina failure, auto-skips Jina for local/private URLs (localhost, 192.168.*, 10.*, etc.) - config.ts: add jina setting and resolveUseJina() with priority chain --no-jina flag > PLANNOTATOR_JINA env > config.json > default true For provenance purposes, this commit was AI assisted. * feat(annotate): support HTML files and URLs in annotate command Extend the annotate subcommand to accept .html/.htm local files (converted via Turndown) and https:// URLs (fetched via Jina Reader with fetch+Turndown fallback). URL content is fetched terminal-side before opening the browser. Add --no-jina global flag to disable Jina Reader per-invocation. Add 10MB file size guard for local HTML files. For provenance purposes, this commit was AI assisted. * feat(annotate): HTML files in folder browser and on-demand conversion - Widen file browser glob to include .html/.htm alongside markdown - handleDoc converts HTML files via Turndown on demand when selected - hasMarkdownFiles accepts optional extensions param for folder validation - Add sourceInfo field to annotate server API response - Add _site/, public/, out/, .docusaurus/, .jekyll-cache/, storybook-static/ to FILE_BROWSER_EXCLUDED For provenance purposes, this commit was AI assisted. * feat(annotate): source attribution badge for HTML/URL annotations Show a subtle badge in DocBadges displaying the URL hostname or HTML filename for converted content. Thread sourceInfo from API response through App → Viewer → DocBadges. Also update Pi extension to accept HTML-only folders in annotate mode. For provenance purposes, this commit was AI assisted. * test: update CLI help text assertion for HTML/URL annotate support For provenance purposes, this commit was AI assisted. * fix(annotate): address PR review findings Security: - Add project-root containment check for HTML files in /api/doc handler using exported isWithinProjectRoot() from resolve-file.ts - Blocks path traversal via absolute paths or ../ escapes isLocalUrl fixes: - Add bracketed IPv6 loopback [::1] detection - Replace hostname.startsWith('10.') with proper IPv4 regex to avoid matching public hostnames like 10.example.com Revert Pi extension change: - Pi server doesn't implement HTML file browsing or conversion yet - Keep Pi folder validation markdown-only until both implementations are updated per CLAUDE.md guidelines Cleanup: - Remove dead el.children || el.childNodes fallback in table rule - Extract hostnameOrFallback() helper to @plannotator/shared/project replacing duplicated try/catch IIFEs in DocBadges and index.ts For provenance purposes, this commit was AI assisted. * feat(annotate): Pi extension HTML annotation parity Bring the Pi extension to full parity with the Bun server for HTML annotation support: - Vendor html-to-markdown and url-to-markdown via vendor.sh - walkMarkdownFiles now scans .html/.htm alongside markdown - handleDocRequest converts HTML files on-demand via Turndown with isWithinProjectRoot containment check - serverAnnotate includes sourceInfo in /api/plan response - index.ts supports URL detection (Jina Reader + fallback), HTML file detection with Turndown conversion, folder HTML validation, and 10MB file size guard - openMarkdownAnnotation accepts and threads sourceInfo - Add turndown as a Pi extension dependency For provenance purposes, this commit was AI assisted. * fix(pi): Obsidian vault walks stay markdown-only, add try/catch for HTML - Add extensions param to walkMarkdownFiles (default: HTML-inclusive) - Obsidian callers pass /\.mdx?$/i to match Bun server behavior - Add try/catch around HTML file reads in handleDocRequest For provenance purposes, this commit was AI assisted. * fix(annotate): address second review — base-block traversal, metadata IP, dead code Security: - Add isWithinProjectRoot check to the base-relative block for HTML files in both Bun and Pi /api/doc handlers. Previously HTML files served via the base query param bypassed the containment guard. - Add 169.254.0.0/16 (link-local / cloud metadata) to isLocalUrl private IP ranges Cleanup: - Remove dead hostname === "[::1]" check (WHATWG URL parser strips brackets; hostname === "::1" already handles it) - Remove dead parent?.childNodes fallback in table cell() function For provenance purposes, this commit was AI assisted. * refactor(annotate): replace custom table rules with turndown-plugin-gfm Drop ~60 lines of hand-rolled GFM table conversion that had a bug (tables without explicit <thead> produced invalid GFM). Use the official turndown-plugin-gfm plugin (24KB) which correctly handles all table patterns plus adds strikethrough and task list support. For provenance purposes, this commit was AI assisted. * fix(annotate): handle all CommonMark backslash escapes in InlineMarkdown Expand the backslash escape regex to cover all CommonMark-defined escapable characters (. ) - # > + | { } &), not just the subset the parser uses for formatting. Fixes literal backslashes appearing in rendered output for Turndown-escaped content like "1\." → "1.". For provenance purposes, this commit was AI assisted. * fix(annotate): prevent SSRF via redirect to private/local URLs Replace redirect: "follow" with redirect: "manual" in fetchViaTurndown and validate each redirect hop against isLocalUrl. Blocks attacks where an external URL redirects to cloud metadata endpoints (169.254.169.254) or other private IPs. Limits redirect chain to 10 hops. For provenance purposes, this commit was AI assisted. * chore: update lockfile for turndown-plugin-gfm in Pi extension bun install needed to resolve turndown-plugin-gfm in the Pi extension workspace after adding it to apps/pi-extension/package.json. For provenance purposes, this commit was AI assisted. * fix(annotate): switch to @joplin/turndown-plugin-gfm, fix TS errors Replace unmaintained turndown-plugin-gfm (2017, v1.0.2) with the actively maintained Joplin fork (2025, v1.0.64, 16KB). Fix TypeScript errors that broke CI: - Add @ts-expect-error for untyped @joplin/turndown-plugin-gfm import - Restructure fetchViaTurndown redirect loop to avoid uninitialized variable — first fetch before loop, loop only for redirects For provenance purposes, this commit was AI assisted. * fix(annotate): use proper declarations.d.ts instead of ts-expect-error Add declarations.d.ts for @joplin/turndown-plugin-gfm with typed function signatures, remove the ts-expect-error suppression. For provenance purposes, this commit was AI assisted. * fix: explicitly include declarations.d.ts in shared tsconfig CI's tsc wasn't finding the ambient module declaration with implicit include. Add explicit include to ensure declarations.d.ts is always picked up regardless of environment. For provenance purposes, this commit was AI assisted. * fix: use ts-expect-error for @joplin/turndown-plugin-gfm types CI's tsc does not pick up ambient declarations.d.ts files despite local tsc finding them — likely a module resolution discrepancy between environments. Revert to @ts-expect-error which passes in both CI and local typecheck. For provenance purposes, this commit was AI assisted. * fix(annotate): body size limit for URL fetches, redirect error, file: protocol - Add 10MB body size limit to both Jina and fetch+Turndown URL paths, matching the local HTML file guard. Streams response body and aborts if limit exceeded. - Distinguish "Too many redirects" from a genuine 3xx response after redirect loop exhaustion. - Add file: to the dangerous protocol blocklist in sanitizeLinkUrl. For provenance purposes, this commit was AI assisted. * fix(annotate): HTML folder outside cwd, HTML linked doc navigation - Remove containment check from base-relative block for HTML files in both Bun and Pi /api/doc handlers. Matches markdown behavior so HTML files in annotated folders outside cwd are served correctly. Standalone block (no base) retains its cwd check as fallback. - Widen isLocalMd → isLocalDoc to treat .html/.htm links as linked documents. Clicking [Next](next.html) in a converted page now opens it via /api/doc with Turndown conversion instead of a new browser tab. For provenance purposes, this commit was AI assisted. * fix(annotate): full loopback range, drain redirect bodies, document env vars - Expand loopback check from just 127.0.0.1 to the full 127.0.0.0/8 range so all loopback addresses skip Jina Reader - Cancel redirect response body before re-fetching to avoid leaking TCP connections back to the pool - Document PLANNOTATOR_JINA and JINA_API_KEY in CLAUDE.md env var table For provenance purposes, this commit was AI assisted. * fix(annotate): IPv6 loopback, readBodyWithLimit fallback, env var docs, comments - Add [::1] back to isLocalUrl — WHATWG URL hostname getter preserves brackets for IPv6 (verified: Bun and Node both return "[::1]"). Add comment explaining the empirical verification so future reviewers don't re-flag. - Fix readBodyWithLimit null-body fallback to still enforce the 10MB limit via text length check instead of silently falling through. - Document PLANNOTATOR_JINA and JINA_API_KEY in AGENTS.md env var table (CLAUDE.md is a symlink to AGENTS.md). - Add comments to base-relative blocks in both Bun and Pi handleDoc explaining the intentional lack of containment check (matches pre-existing markdown behavior, base is set server-side). For provenance purposes, this commit was AI assisted. * fix(annotate): block IPv4-mapped IPv6 and private IPv6 ranges in isLocalUrl Add PRIVATE_IPV6 regex matching bracketed IPv6 private/reserved ranges: - ::ffff: (IPv4-mapped — embeds private IPv4 as hex, e.g. [::ffff:c0a8:1]) - fe80: (link-local) - fc00::/7 (unique-local, covers fc00:: through fdff::) Closes the redirect-SSRF bypass where a public URL redirects to a private address expressed as IPv4-mapped IPv6, e.g. http://[::ffff:169.254.169.254]/latest/meta-data/ For provenance purposes, this commit was AI assisted. * fix(annotate): document IPv6 hostname verification, sourceInfo type, annotate flow - Expand isLocalUrl comment with full empirical verification table showing actual hostname getter output for every IPv6 format in both Bun and Node — prevents false-positive review findings about brackets - Add sourceInfo to /api/plan response type in App.tsx for type safety - Update CLAUDE.md annotate flow diagram to reflect HTML/URL/folder input types For provenance purposes, this commit was AI assisted. * fix(annotate): escape \(, cancel response bodies on error, doc sourceInfo - Add ( to backslash escape regex alongside existing ) — Turndown emits \( in link-adjacent contexts - Cancel response body before throwing on !res.ok in both fetchViaJina and fetchViaTurndown error paths (redirect loop already did this) - Document sourceInfo field in AGENTS.md annotate server API table For provenance purposes, this commit was AI assisted. * fix(annotate): skip base injection for URL annotations, body cleanup - Skip dirname(filePath) base injection when filePath is a URL in both Bun and Pi annotate servers. dirname on a URL string produces a nonsensical filesystem path, causing linked doc clicks to 404. URL annotations now let links open normally instead. - Cancel response body before throwing on content-type mismatch and content-length overflow in fetchViaTurndown/readBodyWithLimit. - Fix double parseInt in readBodyWithLimit content-length check. - Correct AGENTS.md flow diagram: OpenCode not yet implemented for HTML/URL annotation. For provenance purposes, this commit was AI assisted. * feat(annotate): OpenCode HTML file and URL annotation support Add URL detection (Jina Reader + fallback), HTML file detection with Turndown conversion, 10MB file size guard, and sourceInfo threading to OpenCode's handleAnnotateCommand. Uses the same shared utilities as the Bun CLI and Pi extension. OpenCode uses the Bun server directly (startAnnotateServer from @plannotator/server/annotate), so no server-side changes needed — only the command handler routing was missing. Note: folder annotation mode is not added (OpenCode didn't have it before this PR for markdown either — separate scope). For provenance purposes, this commit was AI assisted. * chore(annotate): update slash command description, align fetch log messages - OpenCode plannotator-annotate.md description now mentions HTML/URL - Align fetch progress messages across all three clients: all now show "(via Jina Reader)" or "(via fetch+Turndown)" consistently For provenance purposes, this commit was AI assisted. * fix(annotate): skip conversion for .md URLs, wikilink HTML targets, cleanup - URLs ending in .md/.mdx are fetched raw — no Jina, no Turndown. Content is already markdown. Removes text/plain from fetchViaTurndown content-type whitelist since .md URLs are now short-circuited. - Wikilink regex widened to preserve .html/.htm targets instead of appending .md (e.g. [[page.html]] no longer becomes page.html.md) - Remove redundant existsSync before statSync in OpenCode handler For provenance purposes, this commit was AI assisted. * test(annotate): add htmlToMarkdown conversion tests Tests cover the core conversion utility that all three clients depend on: - Basic HTML → markdown (headings, paragraphs, links, code blocks) - Tables with and without <thead> (the GFM plugin bug that was caught) - Script/style/noscript stripping - Strikethrough (GFM) - Empty HTML handling - Dangerous links preserved (sanitization is in the renderer, not here) For provenance purposes, this commit was AI assisted. * fix(annotate): check content-type before treating .md URLs as raw markdown URLs ending in .md/.mdx (e.g. GitHub's viewer page for README.md) may return HTML instead of raw markdown. fetchRawText now checks the response content-type — if the server returns HTML, returns null so the caller falls through to Jina/Turndown for proper conversion. For provenance purposes, this commit was AI assisted. * fix(annotate): add SSRF redirect protection to fetchRawText fetchRawText (for .md/.mdx URLs) was using default redirect: "follow" with no isLocalUrl validation on redirect hops — a .md URL redirecting to 169.254.169.254 would be followed and credentials returned as "markdown". Now uses redirect: "manual" with per-hop isLocalUrl checks, matching fetchViaTurndown's SSRF protection. For provenance purposes, this commit was AI assisted.
1 parent aa73295 commit b780739

27 files changed

Lines changed: 821 additions & 132 deletions

AGENTS.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ claude --plugin-dir ./apps/hook
104104
| `PLANNOTATOR_SHARE` | Set to `disabled` to turn off URL sharing entirely. Default: enabled. |
105105
| `PLANNOTATOR_SHARE_URL` | Custom base URL for share links (self-hosted portal). Default: `https://share.plannotator.ai`. |
106106
| `PLANNOTATOR_PASTE_URL` | Base URL of the paste service API for short URL sharing. Default: `https://plannotator-paste.plannotator.workers.dev`. |
107+
| `PLANNOTATOR_JINA` | Set to `0` / `false` to disable Jina Reader for URL annotation, or `1` / `true` to enable. Default: enabled. Can also be set via `~/.plannotator/config.json` (`{ "jina": false }`) or per-invocation via `--no-jina`. |
108+
| `JINA_API_KEY` | Optional Jina Reader API key for higher rate limits (500 RPM vs 20 RPM unauthenticated). Free keys include 10M tokens. |
107109
| `PLANNOTATOR_VERIFY_ATTESTATION` | **Read by the install scripts only**, not by the runtime binary. Set to `1` / `true` to have `scripts/install.sh` / `install.ps1` / `install.cmd` run `gh attestation verify` on every install. Off by default. Can also be set persistently via `~/.plannotator/config.json` (`{ "verifyAttestation": true }`) or per-invocation via `--verify-attestation`. Requires `gh` installed and authenticated. |
108110

109111
**Legacy:** `SSH_TTY` and `SSH_CONNECTION` are still detected when `PLANNOTATOR_REMOTE` is unset. Set `PLANNOTATOR_REMOTE=1` / `true` to force remote mode or `0` / `false` to force local mode.
@@ -152,16 +154,20 @@ Approve → "LGTM" sent to agent session
152154
## Annotate Flow
153155

154156
```
155-
User runs /plannotator-annotate <file.md> command
157+
User runs /plannotator-annotate <file.md | file.html | https://... | folder/>
156158
157159
Claude Code: plannotator annotate subcommand runs
158-
OpenCode: event handler intercepts command
160+
OpenCode/Pi: event handler intercepts command
159161
160-
Markdown file read from disk
162+
Input type detected:
163+
.md/.mdx → file read from disk
164+
.html/.htm → file read, converted to markdown via Turndown
165+
https:// → fetched via Jina Reader (default) or fetch+Turndown (--no-jina)
166+
folder/ → file browser opened, files converted on demand
161167
162168
Annotate server starts (reuses plan editor HTML with mode:"annotate")
163169
164-
User annotates markdown, provides feedback
170+
User annotates content, provides feedback
165171
166172
Send Annotations → feedback sent to agent session
167173
```
@@ -247,7 +253,7 @@ During normal plan review, an Archive sidebar tab provides the same browsing via
247253

248254
| Endpoint | Method | Purpose |
249255
| --------------------- | ------ | ------------------------------------------ |
250-
| `/api/plan` | GET | Returns `{ plan, origin, mode: "annotate", filePath }` |
256+
| `/api/plan` | GET | Returns `{ plan, origin, mode: "annotate", filePath, sourceInfo? }` |
251257
| `/api/feedback` | POST | Submit annotations (body: feedback, annotations) |
252258
| `/api/image` | GET | Serve image by path query param |
253259
| `/api/upload` | POST | Upload image, returns `{ path, originalName }` |

apps/hook/server/cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("CLI top-level help", () => {
1919
expect(output).toContain("plannotator --help");
2020
expect(output).toContain("plannotator [--browser <name>]");
2121
expect(output).toContain("plannotator review [PR_URL]");
22-
expect(output).toContain("plannotator annotate <file.md | folder/>");
22+
expect(output).toContain("plannotator annotate <file.md | file.html | https://... | folder/>");
2323
expect(output).toContain("running 'plannotator' without arguments is for hook integration");
2424
});
2525
});

apps/hook/server/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function formatTopLevelHelp(): string {
1515
" plannotator --help",
1616
" plannotator [--browser <name>]",
1717
" plannotator review [PR_URL]",
18-
" plannotator annotate <file.md | folder/>",
18+
" plannotator annotate <file.md | file.html | https://... | folder/> [--no-jina]",
1919
" plannotator last",
2020
" plannotator archive",
2121
" plannotator sessions",
@@ -33,7 +33,7 @@ export function formatInteractiveNoArgClarification(): string {
3333
"",
3434
"For interactive use, try:",
3535
" plannotator review",
36-
" plannotator annotate <file.md>",
36+
" plannotator annotate <file.md | file.html | https://...>",
3737
" plannotator last",
3838
" plannotator archive",
3939
" plannotator sessions",

apps/hook/server/index.ts

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ import {
6464
handleAnnotateServerReady,
6565
} from "@plannotator/server/annotate";
6666
import { type DiffType, getVcsContext, runVcsDiff, gitRuntime } from "@plannotator/server/vcs";
67-
import { loadConfig, resolveDefaultDiffType } from "@plannotator/shared/config";
67+
import { loadConfig, resolveDefaultDiffType, resolveUseJina } from "@plannotator/shared/config";
68+
import { htmlToMarkdown } from "@plannotator/shared/html-to-markdown";
69+
import { urlToMarkdown } from "@plannotator/shared/url-to-markdown";
6870
import { fetchRef, createWorktree, removeWorktree, ensureObjectAvailable } from "@plannotator/shared/worktree";
6971
import { parsePRUrl, checkPRAuth, fetchPR, getCliName, getCliInstallUrl, getMRLabel, getMRNumberLabel, getDisplayRepo } from "@plannotator/server/pr";
7072
import { writeRemoteShareLink } from "@plannotator/server/share-url";
7173
import { resolveMarkdownFile, hasMarkdownFiles } from "@plannotator/shared/resolve-file";
7274
import { FILE_BROWSER_EXCLUDED } from "@plannotator/shared/reference-common";
73-
import { statSync, rmSync, realpathSync } from "fs";
75+
import { statSync, rmSync, realpathSync, existsSync } from "fs";
7476
import { parseRemoteUrl } from "@plannotator/shared/repo";
7577
import { registerSession, unregisterSession, listSessions } from "@plannotator/server/sessions";
7678
import { openBrowser } from "@plannotator/server/browser";
7779
import { detectProjectName } from "@plannotator/server/project";
80+
import { hostnameOrFallback } from "@plannotator/shared/project";
7881
import { planDenyFeedback } from "@plannotator/shared/feedback-templates";
7982
import { readImprovementHook } from "@plannotator/shared/improvement-hooks";
8083
import type { Origin } from "@plannotator/shared/agents";
@@ -109,6 +112,11 @@ if (browserIdx !== -1 && args[browserIdx + 1]) {
109112
args.splice(browserIdx, 2);
110113
}
111114

115+
// Global flag: --no-jina (disables Jina Reader for URL annotation)
116+
const noJinaIdx = args.indexOf("--no-jina");
117+
const cliNoJina = noJinaIdx !== -1;
118+
if (cliNoJina) args.splice(noJinaIdx, 1);
119+
112120
if (isTopLevelHelpInvocation(args)) {
113121
console.log(formatTopLevelHelp());
114122
process.exit(0);
@@ -451,7 +459,7 @@ if (args[0] === "sessions") {
451459

452460
let filePath = args[1];
453461
if (!filePath) {
454-
console.error("Usage: plannotator annotate <file.md | folder/>");
462+
console.error("Usage: plannotator annotate <file.md | file.html | https://... | folder/> [--no-jina]");
455463
process.exit(1);
456464
}
457465

@@ -468,50 +476,87 @@ if (args[0] === "sessions") {
468476
console.error(`[DEBUG] File path arg: ${filePath}`);
469477
}
470478

471-
// Check if the argument is a directory (folder annotation mode)
472-
const resolvedArg = path.resolve(projectRoot, filePath);
473-
let isFolder = false;
474-
try {
475-
isFolder = statSync(resolvedArg).isDirectory();
476-
} catch {
477-
// Not a directory, fall through to file resolution
478-
}
479-
480479
let markdown: string;
481480
let absolutePath: string;
482481
let folderPath: string | undefined;
483482
let annotateMode: "annotate" | "annotate-folder" = "annotate";
483+
let sourceInfo: string | undefined;
484484

485-
if (isFolder) {
486-
// Folder annotation mode
487-
if (!hasMarkdownFiles(resolvedArg, FILE_BROWSER_EXCLUDED)) {
488-
console.error(`No markdown files found in ${resolvedArg}`);
489-
process.exit(1);
490-
}
491-
folderPath = resolvedArg;
492-
absolutePath = resolvedArg;
493-
markdown = "";
494-
annotateMode = "annotate-folder";
495-
console.error(`Folder: ${resolvedArg}`);
496-
} else {
497-
// Single file annotation mode
498-
const resolved = resolveMarkdownFile(filePath, projectRoot);
485+
// --- URL annotation ---
486+
const isUrl = /^https?:\/\//i.test(filePath);
499487

500-
if (resolved.kind === "ambiguous") {
501-
console.error(`Ambiguous filename "${resolved.input}" — found ${resolved.matches.length} matches:`);
502-
for (const match of resolved.matches) {
503-
console.error(` ${match}`);
488+
if (isUrl) {
489+
const useJina = resolveUseJina(cliNoJina, loadConfig());
490+
console.error(`Fetching: ${filePath}${useJina ? " (via Jina Reader)" : " (via fetch+Turndown)"}`);
491+
try {
492+
const result = await urlToMarkdown(filePath, { useJina });
493+
markdown = result.markdown;
494+
if (process.env.PLANNOTATOR_DEBUG) {
495+
console.error(`[DEBUG] Fetched via ${result.source} (${markdown.length} chars)`);
504496
}
497+
} catch (err) {
498+
console.error(`Failed to fetch URL: ${err instanceof Error ? err.message : String(err)}`);
505499
process.exit(1);
506500
}
507-
if (resolved.kind === "not_found") {
508-
console.error(`File not found: ${resolved.input}`);
509-
process.exit(1);
501+
absolutePath = filePath; // Use URL as the "path" for display
502+
sourceInfo = filePath; // Full URL for source attribution
503+
} else {
504+
// Check if the argument is a directory (folder annotation mode)
505+
const resolvedArg = path.resolve(projectRoot, filePath);
506+
let isFolder = false;
507+
try {
508+
isFolder = statSync(resolvedArg).isDirectory();
509+
} catch {
510+
// Not a directory, fall through to file resolution
510511
}
511512

512-
absolutePath = resolved.path;
513-
markdown = await Bun.file(absolutePath).text();
514-
console.error(`Resolved: ${absolutePath}`);
513+
if (isFolder) {
514+
// Folder annotation mode (markdown + HTML files)
515+
if (!hasMarkdownFiles(resolvedArg, FILE_BROWSER_EXCLUDED, /\.(mdx?|html?)$/i)) {
516+
console.error(`No markdown or HTML files found in ${resolvedArg}`);
517+
process.exit(1);
518+
}
519+
folderPath = resolvedArg;
520+
absolutePath = resolvedArg;
521+
markdown = "";
522+
annotateMode = "annotate-folder";
523+
console.error(`Folder: ${resolvedArg}`);
524+
} else if (/\.html?$/i.test(resolvedArg)) {
525+
// HTML file annotation mode — convert to markdown via Turndown
526+
if (!existsSync(resolvedArg)) {
527+
console.error(`File not found: ${filePath}`);
528+
process.exit(1);
529+
}
530+
const htmlFile = Bun.file(resolvedArg);
531+
if (htmlFile.size > 10 * 1024 * 1024) {
532+
console.error(`File too large (${Math.round(htmlFile.size / 1024 / 1024)}MB, max 10MB): ${resolvedArg}`);
533+
process.exit(1);
534+
}
535+
const html = await htmlFile.text();
536+
markdown = htmlToMarkdown(html);
537+
absolutePath = resolvedArg;
538+
sourceInfo = path.basename(resolvedArg);
539+
console.error(`Converted: ${absolutePath}`);
540+
} else {
541+
// Single markdown file annotation mode
542+
const resolved = resolveMarkdownFile(filePath, projectRoot);
543+
544+
if (resolved.kind === "ambiguous") {
545+
console.error(`Ambiguous filename "${resolved.input}" — found ${resolved.matches.length} matches:`);
546+
for (const match of resolved.matches) {
547+
console.error(` ${match}`);
548+
}
549+
process.exit(1);
550+
}
551+
if (resolved.kind === "not_found") {
552+
console.error(`File not found: ${resolved.input}`);
553+
process.exit(1);
554+
}
555+
556+
absolutePath = resolved.path;
557+
markdown = await Bun.file(absolutePath).text();
558+
console.error(`Resolved: ${absolutePath}`);
559+
}
515560
}
516561

517562
const annotateProject = (await detectProjectName()) ?? "_unknown";
@@ -523,6 +568,7 @@ if (args[0] === "sessions") {
523568
origin: detectedOrigin,
524569
mode: annotateMode,
525570
folderPath,
571+
sourceInfo,
526572
sharingEnabled,
527573
shareBaseUrl,
528574
pasteApiUrl,
@@ -543,7 +589,9 @@ if (args[0] === "sessions") {
543589
mode: "annotate",
544590
project: annotateProject,
545591
startedAt: new Date().toISOString(),
546-
label: folderPath ? `annotate-${path.basename(folderPath)}` : `annotate-${path.basename(absolutePath)}`,
592+
label: folderPath
593+
? `annotate-${path.basename(folderPath)}`
594+
: `annotate-${isUrl ? hostnameOrFallback(absolutePath) : path.basename(absolutePath)}`,
547595
});
548596

549597
// Wait for user feedback

apps/opencode-plugin/commands.ts

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ import {
2020
} from "@plannotator/server/annotate";
2121
import { getGitContext, runGitDiffWithContext } from "@plannotator/server/git";
2222
import { parsePRUrl, checkPRAuth, fetchPR, getCliName, getMRLabel, getMRNumberLabel, getDisplayRepo } from "@plannotator/server/pr";
23-
import { loadConfig, resolveDefaultDiffType } from "@plannotator/shared/config";
23+
import { loadConfig, resolveDefaultDiffType, resolveUseJina } from "@plannotator/shared/config";
2424
import { resolveMarkdownFile } from "@plannotator/shared/resolve-file";
25+
import { htmlToMarkdown } from "@plannotator/shared/html-to-markdown";
26+
import { urlToMarkdown } from "@plannotator/shared/url-to-markdown";
27+
import { statSync } from "fs";
28+
import path from "path";
2529

2630
/** Shared dependencies injected by the plugin */
2731
export interface CommandDeps {
@@ -149,35 +153,79 @@ export async function handleAnnotateCommand(
149153
const filePath = event.properties?.arguments || event.arguments || "";
150154

151155
if (!filePath) {
152-
client.app.log({ level: "error", message: "Usage: /plannotator-annotate <file.md>" });
156+
client.app.log({ level: "error", message: "Usage: /plannotator-annotate <file.md | file.html | https://...>" });
153157
return;
154158
}
155159

156-
client.app.log({ level: "info", message: `Opening annotation UI for ${filePath}...` });
160+
let markdown: string;
161+
let absolutePath: string;
162+
let sourceInfo: string | undefined;
157163

158-
const projectRoot = process.cwd();
159-
const resolved = await resolveMarkdownFile(filePath, projectRoot);
164+
// --- URL annotation ---
165+
const isUrl = /^https?:\/\//i.test(filePath);
160166

161-
if (resolved.kind === "ambiguous") {
162-
client.app.log({
163-
level: "error",
164-
message: `Ambiguous filename "${resolved.input}" — found ${resolved.matches.length} matches:\n${resolved.matches.map((m) => ` ${m}`).join("\n")}`,
165-
});
166-
return;
167-
}
168-
if (resolved.kind === "not_found") {
169-
client.app.log({ level: "error", message: `File not found: ${resolved.input}` });
170-
return;
171-
}
167+
if (isUrl) {
168+
const useJina = resolveUseJina(false, loadConfig());
169+
client.app.log({ level: "info", message: `Fetching: ${filePath}${useJina ? " (via Jina Reader)" : " (via fetch+Turndown)"}...` });
170+
try {
171+
const result = await urlToMarkdown(filePath, { useJina });
172+
markdown = result.markdown;
173+
} catch (err) {
174+
client.app.log({ level: "error", message: `Failed to fetch URL: ${err instanceof Error ? err.message : String(err)}` });
175+
return;
176+
}
177+
absolutePath = filePath;
178+
sourceInfo = filePath;
179+
} else {
180+
const projectRoot = process.cwd();
181+
const resolvedArg = path.resolve(projectRoot, filePath);
172182

173-
const absolutePath = resolved.path;
174-
client.app.log({ level: "info", message: `Resolved: ${absolutePath}` });
175-
const markdown = await Bun.file(absolutePath).text();
183+
if (/\.html?$/i.test(resolvedArg)) {
184+
// HTML file annotation — convert to markdown via Turndown
185+
let fileSize: number;
186+
try {
187+
fileSize = statSync(resolvedArg).size;
188+
} catch {
189+
client.app.log({ level: "error", message: `File not found: ${filePath}` });
190+
return;
191+
}
192+
if (fileSize > 10 * 1024 * 1024) {
193+
client.app.log({ level: "error", message: `File too large (${Math.round(fileSize / 1024 / 1024)}MB, max 10MB)` });
194+
return;
195+
}
196+
const html = await Bun.file(resolvedArg).text();
197+
markdown = htmlToMarkdown(html);
198+
absolutePath = resolvedArg;
199+
sourceInfo = path.basename(resolvedArg);
200+
client.app.log({ level: "info", message: `Converted: ${absolutePath}` });
201+
} else {
202+
// Markdown file annotation
203+
client.app.log({ level: "info", message: `Opening annotation UI for ${filePath}...` });
204+
const resolved = await resolveMarkdownFile(filePath, projectRoot);
205+
206+
if (resolved.kind === "ambiguous") {
207+
client.app.log({
208+
level: "error",
209+
message: `Ambiguous filename "${resolved.input}" — found ${resolved.matches.length} matches:\n${resolved.matches.map((m) => ` ${m}`).join("\n")}`,
210+
});
211+
return;
212+
}
213+
if (resolved.kind === "not_found") {
214+
client.app.log({ level: "error", message: `File not found: ${resolved.input}` });
215+
return;
216+
}
217+
218+
absolutePath = resolved.path;
219+
client.app.log({ level: "info", message: `Resolved: ${absolutePath}` });
220+
markdown = await Bun.file(absolutePath).text();
221+
}
222+
}
176223

177224
const server = await startAnnotateServer({
178225
markdown,
179226
filePath: absolutePath,
180227
origin: "opencode",
228+
sourceInfo,
181229
sharingEnabled: await getSharingEnabled(),
182230
shareBaseUrl: getShareBaseUrl(),
183231
htmlContent,

apps/opencode-plugin/commands/plannotator-annotate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Open interactive annotation UI for a markdown file
2+
description: Open interactive annotation UI for a markdown file, HTML file, or URL
33
---
44

55
The Plannotator Annotate UI has been triggered. Opening the annotation UI...

0 commit comments

Comments
 (0)