@each1024/pi-mini-mode 1.3.15 → 2.0.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/lib/file-links.ts CHANGED
@@ -2,16 +2,23 @@ import { existsSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import { dirname, isAbsolute, resolve } from "node:path";
4
4
  import { pathToFileURL } from "node:url";
5
- import { getCapabilities, sliceByColumn, stripTerminalSequences, visibleWidth } from "@earendil-works/pi-tui";
5
+ import { Marked, getCapabilities, sliceByColumn, stripTerminalSequences, visibleWidth } from "@earendil-works/pi-tui";
6
6
 
7
- /** Pi 0.85's allowlist predates Otty; preserve explicit opt-outs and multiplexer detection. */
8
- export function inputCapabilities(env = process.env) {
9
- const caps = getCapabilities();
10
- if (env.TERM_PROGRAM?.toLowerCase() !== "otty" || env.TMUX || /^(screen|tmux)/.test(env.TERM ?? "")) return caps;
11
- return { ...caps, hyperlinks: env.PI_HYPERLINKS === "0" ? false : true,
12
- images: env.PI_IMAGE_PROTOCOL === "0" || env.PI_IMAGE_PROTOCOL === "none" ? null : caps.images ?? "kitty" as const };
7
+ const linkParser = new Marked();
8
+ export function webPaths(text: string) {
9
+ let offset = 0;
10
+ return linkParser.Lexer.lexInline(text, { gfm: true }).flatMap(token => {
11
+ const start = offset;
12
+ offset += token.raw.length;
13
+ if (token.type !== "link" || !/^https?:\/\//i.test(token.href) || /[\x00-\x20\x7f]/.test(token.href)) return [];
14
+ try { new URL(token.href); } catch { return []; }
15
+ return [{ path: token.href, start, end: offset }];
16
+ });
13
17
  }
14
18
 
19
+ /** Use Pi's effective capabilities, including user overrides and renderer restrictions. */
20
+ export const inputCapabilities = getCapabilities;
21
+
15
22
  export function localPath(path: string, cwd: string): string {
16
23
  return path.startsWith("~/") ? resolve(homedir(), path.slice(2)) : isAbsolute(path) ? path : resolve(cwd, path);
17
24
  }
@@ -36,17 +43,19 @@ function fenceRanges(text: string): Array<[number, number]> {
36
43
  return ranges;
37
44
  }
38
45
 
39
- /** Quoted paths may contain spaces; unquoted paths end at prose delimiters. */
46
+ /** Quoted or shell-escaped paths may contain spaces; spans refer to the original text. */
40
47
  export function filePaths(text: string) {
41
48
  const matches: Array<{ path: string; start: number; end: number }> = [];
42
49
  const fences = fenceRanges(text);
43
- const pattern = /(["'`])([^\r\n]*?)\1|[^\s"'`<>()[\]{},。;!?]+/g;
50
+ const pattern = /(["'`])([^\r\n]*?)\1|(?:\\[^\r\n]|[^\s"'`<>()[\]{},。;!?])+/g;
44
51
  for (const match of text.matchAll(pattern)) {
45
- const path = (match[2] ?? match[0]).replace(/[.,;:!?]+$/, "");
52
+ const raw = (match[2] ?? match[0]).replace(/[.,;:!?]+$/, "");
53
+ const path = !match[1] && /^(?:\/|~\/|\.\.?\/)/.test(raw)
54
+ ? raw.replace(/\\([ \t"'`()\[\]{}!#$&;<>?|*\\])/g, "$1") : raw;
46
55
  if (!path || /^[\\/]+$/.test(path) || /[\x00-\x1f\x7f]/.test(path) || /^[a-z][\w+.-]*:\/\//i.test(path)) continue;
47
56
  if (!/^(?:\/|~\/|\.{1,2}\/|[a-z]:[\\/])|[\\/]|\.[a-z\d]{1,12}$/i.test(path)) continue;
48
57
  const start = match.index + (match[1] ? 1 : 0);
49
- const end = start + path.length;
58
+ const end = start + raw.length;
50
59
  if (fences.some(([from, to]) => start < to && end > from)) continue;
51
60
  matches.push({ path, start, end });
52
61
  }
@@ -54,19 +63,27 @@ export function filePaths(text: string) {
54
63
  }
55
64
 
56
65
  // BEL survives Markdown's backslash-escape parsing, unlike the ST terminator.
57
- export const fileLink = (text: string, path: string) => `\x1b]8;;${pathToFileURL(path).href}\x07\x1b[4m${text}\x1b[24m\x1b]8;;\x07`;
66
+ export const fileLink = (text: string, path: string, underline = true) => `\x1b]8;;${pathToFileURL(path).href}\x07${underline ? `\x1b[4m${text}\x1b[24m` : text}\x1b]8;;\x07`;
58
67
 
59
- /** Display-only: never change persisted messages, existing links, or remote URLs. */
68
+ /** Display-only: normalize local targets without changing persisted messages or remote URLs. */
60
69
  export function linkMessageFiles(text: string, cwd: string): string {
61
70
  if (!inputCapabilities().hyperlinks) return text;
62
71
  let imageNumber = 0;
63
72
  return text.split(/(\x1b\]8;[^\x07]*(?:\x07)[\s\S]*?\x1b\]8;;\x07|\[[^\]\n]*\]\([^\n]*?\))/g).map((part, index) => {
64
- if (index % 2) return part;
65
- const matches = filePaths(part).map(match => ({ ...match, label: /\.(png|jpe?g|gif|webp)$/i.test(match.path) && existsSync(localPath(match.path, cwd)) ? `[image${++imageNumber}]` : match.path }));
73
+ if (index % 2) {
74
+ return part.replace(/^(\[[^\]\n]*\]\()(<[^>\n]*>|[^\s)]+)([\s\S]*)$/, (original, prefix, target, suffix) => {
75
+ const value = target.startsWith("<") ? target.slice(1, -1) : target;
76
+ if (/^[a-z][\w+.-]*:|^\/\//i.test(value) || /[\x00-\x1f\x7f]/.test(value)) return original;
77
+ const path = localPath(value, cwd);
78
+ return existsSync(path) ? `${prefix}<${pathToFileURL(path).href}>${suffix}` : original;
79
+ });
80
+ }
81
+ const matches = filePaths(part).map(match => ({ ...match, image: /\.(png|jpe?g|gif|webp)$/i.test(match.path) && existsSync(localPath(match.path, cwd)), number: 0 }));
82
+ for (const match of matches) if (match.image) match.number = ++imageNumber;
66
83
  for (const match of matches.reverse()) {
67
84
  const path = localPath(match.path, cwd);
68
85
  if (!existsSync(path)) continue;
69
- part = part.slice(0, match.start) + fileLink(match.label, path) + part.slice(match.end);
86
+ part = part.slice(0, match.start) + fileLink(match.image ? ` #${match.number}` : match.path, path, !match.image) + part.slice(match.end);
70
87
  }
71
88
  return part;
72
89
  }).join("");
@@ -75,6 +92,8 @@ export function linkMessageFiles(text: string, cwd: string): string {
75
92
  /** Decorate rendered columns without removing the editor's ANSI cursor marker. */
76
93
  export function linkRenderedPath(line: string, start: number, end: number, path: string, directory = false): string {
77
94
  if (!inputCapabilities().hyperlinks || end <= start) return line;
78
- return sliceByColumn(line, 0, start) + fileLink(sliceByColumn(line, start, end - start), directory ? dirname(path) : path)
95
+ const label = sliceByColumn(line, start, end - start);
96
+ const link = /^https?:\/\//i.test(path) ? `\x1b]8;;${path}\x07${label}\x1b]8;;\x07` : fileLink(label, directory ? dirname(path) : path, false);
97
+ return sliceByColumn(line, 0, start) + link
79
98
  + sliceByColumn(line, end, Math.max(0, visibleWidth(stripTerminalSequences(line)) - end));
80
99
  }