md4x 0.0.1 → 0.0.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.
Files changed (2) hide show
  1. package/lib/cli.mjs +62 -1
  2. package/package.json +3 -2
package/lib/cli.mjs CHANGED
@@ -34,6 +34,10 @@ General options:
34
34
  -h, --help Display this help and exit
35
35
  -v, --version Display version and exit
36
36
 
37
+ Input can be a file path, "-" for stdin, an HTTP/HTTPS URL, or a shorthand:
38
+ gh:<owner>/<repo>[/path] GitHub (auto-converted to raw content)
39
+ npm:<package>[@version][/path] npm package file via unpkg
40
+
37
41
  HTML output options:
38
42
  -f, --full-html Generate full HTML document, including header
39
43
  --html-title=TITLE Sets the title of the document
@@ -62,7 +66,23 @@ if (!["html", "json", "ansi"].includes(format)) {
62
66
  process.exit(1);
63
67
  }
64
68
 
65
- const inputPath = positionals[0];
69
+ let inputPath = positionals[0];
70
+ // gh:owner/repo[/path] → https://github.com/owner/repo[/path]
71
+ if (inputPath && /^gh:/i.test(inputPath)) {
72
+ inputPath = `https://github.com/${inputPath.slice(3)}`;
73
+ }
74
+ // npm:package[@version][/path] → https://unpkg.com/package[@version][/path]
75
+ if (inputPath && /^npm:/i.test(inputPath)) {
76
+ const spec = inputPath.slice(4);
77
+ // Check if spec includes a file path (after the package name + optional version)
78
+ // Scoped: @scope/pkg[@ver][/path] — path starts after 2nd slash
79
+ // Unscoped: pkg[@ver][/path] — path starts after 1st slash
80
+ const slashIdx = spec.startsWith("@")
81
+ ? spec.indexOf("/", spec.indexOf("/") + 1)
82
+ : spec.indexOf("/");
83
+ const hasPath = slashIdx > 0;
84
+ inputPath = `https://unpkg.com/${spec}${hasPath ? "" : "/README.md"}`;
85
+ }
66
86
  let input;
67
87
  if (!inputPath || inputPath === "-") {
68
88
  if (process.stdin.isTTY) {
@@ -75,6 +95,23 @@ if (!inputPath || inputPath === "-") {
75
95
  usage();
76
96
  process.exit(1);
77
97
  }
98
+ } else if (/^https?:\/\//i.test(inputPath)) {
99
+ const fetchUrl = toRawUrl(inputPath);
100
+ try {
101
+ const res = await fetch(fetchUrl, {
102
+ headers: { Accept: "text/markdown, text/plain;q=0.9, */*;q=0.1" },
103
+ });
104
+ if (!res.ok) {
105
+ process.stderr.write(
106
+ `Failed to fetch ${inputPath}: ${res.status} ${res.statusText}\n`,
107
+ );
108
+ process.exit(1);
109
+ }
110
+ input = await res.text();
111
+ } catch (err) {
112
+ process.stderr.write(`Failed to fetch ${inputPath}: ${err.message}\n`);
113
+ process.exit(1);
114
+ }
78
115
  } else {
79
116
  try {
80
117
  input = readFileSync(inputPath, "utf8");
@@ -136,3 +173,27 @@ if (values.output && values.output !== "-") {
136
173
  } else {
137
174
  process.stdout.write(result);
138
175
  }
176
+
177
+ // --- internal helpers ---
178
+
179
+ /** Convert GitHub web URLs to raw content URLs */
180
+ function toRawUrl(url) {
181
+ const u = new URL(url);
182
+ if (u.hostname === "github.com" || u.hostname === "www.github.com") {
183
+ // github.com/:owner/:repo/blob/:ref/path → raw.githubusercontent.com/:owner/:repo/:ref/path
184
+ const blob = u.pathname.match(/^\/([^/]+\/[^/]+)\/blob\/(.+)/);
185
+ if (blob) {
186
+ return `https://raw.githubusercontent.com/${blob[1]}/${blob[2]}`;
187
+ }
188
+ // github.com/:owner/:repo → raw.githubusercontent.com/:owner/:repo/HEAD/README.md
189
+ const repo = u.pathname.match(/^\/([^/]+\/[^/]+)\/?$/);
190
+ if (repo) {
191
+ return `https://raw.githubusercontent.com/${repo[1]}/HEAD/README.md`;
192
+ }
193
+ }
194
+ // gist.github.com/:user/:id → gist.githubusercontent.com/:user/:id/raw
195
+ if (u.hostname === "gist.github.com") {
196
+ return `https://gist.githubusercontent.com${u.pathname}/raw`;
197
+ }
198
+ return url;
199
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "md4x",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "bin": {
@@ -23,7 +23,8 @@
23
23
  },
24
24
  "files": [
25
25
  "build",
26
- "lib"
26
+ "lib",
27
+ "README.md"
27
28
  ],
28
29
  "devDependencies": {
29
30
  "@milkdown/crepe": "^7.18.0",