@shikijs/cli 3.15.0 → 3.17.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/dist/cli.d.mts CHANGED
@@ -1,6 +1,12 @@
1
+ declare function isUrl(path: string): boolean;
2
+ declare function getExtFromUrl(url: string): string;
3
+ declare function readSource(path: string): Promise<{
4
+ content: string;
5
+ ext: string;
6
+ }>;
1
7
  declare function run(argv?: string[], log?: {
2
8
  (...data: any[]): void;
3
9
  (message?: any, ...optionalParams: any[]): void;
4
10
  }): Promise<void>;
5
11
 
6
- export { run };
12
+ export { getExtFromUrl, isUrl, readSource, run };
package/dist/cli.mjs CHANGED
@@ -7,21 +7,47 @@ import '@shikijs/vscode-textmate';
7
7
  import 'ansis';
8
8
  import 'shiki';
9
9
 
10
- const version = "3.15.0";
10
+ const version = "3.17.0";
11
11
 
12
+ function isUrl(path) {
13
+ return path.startsWith("http://") || path.startsWith("https://");
14
+ }
15
+ function getExtFromUrl(url) {
16
+ try {
17
+ const pathname = new URL(url).pathname;
18
+ return parse(pathname).ext.slice(1);
19
+ } catch {
20
+ return "";
21
+ }
22
+ }
23
+ async function readSource(path) {
24
+ if (isUrl(path)) {
25
+ const response = await fetch(path);
26
+ if (!response.ok) {
27
+ throw new Error(`Failed to fetch ${path}: ${response.status} ${response.statusText}`);
28
+ }
29
+ const content = await response.text();
30
+ const ext = getExtFromUrl(path);
31
+ return { content, ext };
32
+ } else {
33
+ const content = await fs.readFile(path, "utf-8");
34
+ const ext = parse(path).ext.slice(1);
35
+ return { content, ext };
36
+ }
37
+ }
12
38
  async function run(argv = process.argv, log = console.log) {
13
39
  const cli = cac("shiki");
14
40
  cli.option("--theme <theme>", "Color theme to use", { default: "vitesse-dark" }).option("--lang <lang>", "Programming language").help().version(version);
15
41
  const { options, args } = cli.parse(argv);
16
42
  const files = args;
17
43
  const codes = await Promise.all(files.map(async (path) => {
18
- const content = await fs.readFile(path, "utf-8");
19
- const ext = options.lang || parse(path).ext.slice(1);
20
- return await codeToANSI(content, ext, options.theme);
44
+ const { content, ext } = await readSource(path);
45
+ const lang = options.lang || ext;
46
+ return await codeToANSI(content, lang, options.theme);
21
47
  }));
22
48
  for (const code of codes)
23
49
  log(code);
24
50
  }
25
51
  run();
26
52
 
27
- export { run };
53
+ export { getExtFromUrl, isUrl, readSource, run };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/cli",
3
3
  "type": "module",
4
- "version": "3.15.0",
4
+ "version": "3.17.0",
5
5
  "description": "Shiki in the command line",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "@shikijs/vscode-textmate": "^10.0.2",
40
40
  "ansis": "^4.2.0",
41
41
  "cac": "^6.7.14",
42
- "shiki": "3.15.0"
42
+ "shiki": "3.17.0"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "unbuild",