@oversk7/pi-web-dsh-style 1.3.0 → 1.3.2

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/web/markdown.ts CHANGED
@@ -6,7 +6,7 @@ import powershell from "highlight.js/lib/languages/powershell";
6
6
  hljs.registerLanguage("dockerfile", dockerfile);
7
7
  hljs.registerLanguage("powershell", powershell);
8
8
 
9
- function localFileLink(target: string) {
9
+ function localFileLink(target: string, workspace = false) {
10
10
  let value = target.trim();
11
11
  let line: number | null = null;
12
12
  const location = /#L(\d+)(?:C\d+)?$/i.exec(value) || /:(\d+)(?::\d+)?$/.exec(value);
@@ -22,7 +22,15 @@ function localFileLink(target: string) {
22
22
  }
23
23
  }
24
24
  value = value.replace(/^\/([A-Za-z]:[\\/])/, "$1");
25
- if (!/^[A-Za-z]:[\\/]/.test(value) && !/^\\\\[^\\]+\\[^\\]+/.test(value)) return null;
25
+ if (!/^[A-Za-z]:[\\/]/.test(value) && !/^\\\\[^\\]+\\[^\\]+/.test(value)) {
26
+ if (!workspace || /^[a-z][a-z\d+.-]*:|^\/\//i.test(value) || /[?#]/.test(value)) return null;
27
+ if (!/\.(?:md|markdown|mdx|txt|html?|css|[cm]?[jt]sx?|json|ya?ml|toml|py|rs|go|java|c|cpp|h|hpp|cs|sh|ps1|sql|xml|csv)$/i.test(value)) return null;
28
+ try {
29
+ value = decodeURIComponent(value).replace(/^\/+/, "");
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
26
34
  const href = `/api/fs/view?path=${encodeURIComponent(value)}${line ? `#L${line}` : ""}`;
27
35
  return { path: value, line, href };
28
36
  }
@@ -52,7 +60,14 @@ parser.validateLink = (target) => Boolean(localFileLink(target)) || validateLink
52
60
  parser.renderer.rules.link_open = (tokens, index, options, env, renderer) => {
53
61
  const token = tokens[index];
54
62
  const target = String(token.attrGet("href") || "");
55
- const local = localFileLink(target);
63
+ let local = localFileLink(target, true);
64
+ if (local && typeof env?.cwd === "string" && !/^[A-Za-z]:[\\/]|^\\\\/.test(local.path)) {
65
+ const base = new URL("file:///");
66
+ base.pathname = `${env.cwd.replace(/\\/g, "/").replace(/%/g, "%25").replace(/\/+$/, "")}/`;
67
+ const relative = local.path.replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
68
+ const resolved = new URL(relative, base);
69
+ local = localFileLink(`${resolved.href}${local.line ? `#L${local.line}` : ""}`) || local;
70
+ }
56
71
  if (local) {
57
72
  token.attrSet("href", local.href);
58
73
  token.attrJoin("class", "md-local-file");
@@ -113,6 +128,6 @@ parser.renderer.rules.task_checkbox = (tokens, index) => {
113
128
  return `<input type="checkbox" disabled${checked ? " checked" : ""} aria-label="${checked ? "已完成" : "未完成"}"/> `;
114
129
  };
115
130
 
116
- export function markdown(src: unknown): string {
117
- return parser.render(String(src ?? ""));
131
+ export function markdown(src: unknown, context: { cwd?: string } = {}): string {
132
+ return parser.render(String(src ?? ""), context);
118
133
  }