@jdsteinbach/mdview 0.0.0-stage → 0.0.1
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/README.md +1 -3
- package/bin/mdview +2 -0
- package/dist/cli-BhFYKMLx.mjs +508 -0
- package/dist/cli.d.mts +2 -0
- package/dist/cli.mjs +1 -0
- package/dist/client/assets/alegreya-cyrillic-400-italic-CzOFVsaV.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-400-italic-DSxbt1-2.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-400-normal-72Io3whm.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-400-normal-VT77nruV.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-700-italic-BD_SSGE6.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-700-italic-CtIAlz4-.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-700-normal-Bftrb-m-.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-700-normal-_o_NBoYw.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-400-italic-281qz-PX.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-400-italic-CJ53xJ73.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-400-normal-Cj1kF6KR.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-400-normal-DN8azYTI.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-700-italic-CzbyOdJK.woff2 +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-700-italic-DPSlVqRs.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-700-normal-A6q6xEOT.woff +0 -0
- package/dist/client/assets/alegreya-cyrillic-ext-700-normal-Dx8o6fqX.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-400-italic-D9g29fJh.woff +0 -0
- package/dist/client/assets/alegreya-greek-400-italic-DpcfyKEk.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-400-normal-BI7tMDVB.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-400-normal-BTnTTGc4.woff +0 -0
- package/dist/client/assets/alegreya-greek-700-italic-CZD3tXcH.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-700-italic-VV5LDQFu.woff +0 -0
- package/dist/client/assets/alegreya-greek-700-normal-ByoPpMZf.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-700-normal-mWQ2Ne64.woff +0 -0
- package/dist/client/assets/alegreya-greek-ext-400-italic-BqZ1xVE4.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-ext-400-italic-EKR1I7m6.woff +0 -0
- package/dist/client/assets/alegreya-greek-ext-400-normal-UcF-MPys.woff +0 -0
- package/dist/client/assets/alegreya-greek-ext-400-normal-ueNFnE5s.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-ext-700-italic-BROTooN0.woff +0 -0
- package/dist/client/assets/alegreya-greek-ext-700-italic-nrP_sVEB.woff2 +0 -0
- package/dist/client/assets/alegreya-greek-ext-700-normal-DQPzd-kK.woff +0 -0
- package/dist/client/assets/alegreya-greek-ext-700-normal-DzPShcR4.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-400-italic-BXpar-rJ.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-400-italic-CJvUQvZT.woff +0 -0
- package/dist/client/assets/alegreya-latin-400-normal-BLlBRtDv.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-400-normal-BuQWlRPA.woff +0 -0
- package/dist/client/assets/alegreya-latin-700-italic-BGh4e_zY.woff +0 -0
- package/dist/client/assets/alegreya-latin-700-italic-Czf8XOXC.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-700-normal-BS7r-qZs.woff +0 -0
- package/dist/client/assets/alegreya-latin-700-normal-Vpz_43wJ.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-ext-400-italic-BQgzjtbe.woff +0 -0
- package/dist/client/assets/alegreya-latin-ext-400-italic-Cf8WgXZg.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-ext-400-normal-BJ1luULk.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-ext-400-normal-I24-Fo_X.woff +0 -0
- package/dist/client/assets/alegreya-latin-ext-700-italic-DPlpKdyb.woff +0 -0
- package/dist/client/assets/alegreya-latin-ext-700-italic-DtvHWMUs.woff2 +0 -0
- package/dist/client/assets/alegreya-latin-ext-700-normal-B6bzkdlx.woff +0 -0
- package/dist/client/assets/alegreya-latin-ext-700-normal-CEFih5UF.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-400-italic-Bc6UFQqS.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-400-italic-BfxaBkav.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-400-normal-C2XSGevv.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-400-normal-CFUoBDld.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-500-normal-gEBUHwKD.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-500-normal-kQHBM3cE.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-700-italic-C9nv3xSQ.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-700-italic-gW2Ijdbu.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-700-normal-BMO00Gdb.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-700-normal-qSextXl4.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-400-italic-BpLolsQ8.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-400-italic-T00rgbz1.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-400-normal-CBouUAPx.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-400-normal-ClLgPhqT.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-500-normal-BjdEjhHc.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-500-normal-CBZLRaMr.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-700-italic-CYQG5-wC.woff +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-700-italic-Cx1Q4LEJ.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-700-normal-Cpzx2u2L.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-cyrillic-ext-700-normal-YOec_wOb.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-400-italic-Y4Zag0Am.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-400-italic-omD4Z7vk.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-400-normal-8U5NAP_q.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-400-normal-B87Md6o9.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-500-normal-BwxJMQU_.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-500-normal-CjlmbScm.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-700-italic-BYKj_pBU.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-700-italic-BdQpg-px.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-700-normal-BZuMtSJ5.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-700-normal-CTof9pYC.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-400-italic-CqU0VZms.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-400-italic-Dsqv3611.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-400-normal-CaUEPd_u.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-400-normal-D6j-TQLM.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-500-normal-8OvC5Vmr.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-500-normal-Cp0bGOKK.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-700-italic-C-Qo2fwW.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-700-italic-Dkx5pnHi.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-700-normal-BVJ8GoGc.woff +0 -0
- package/dist/client/assets/alegreya-sans-greek-ext-700-normal-ChNZoGr1.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-400-italic-CgbdrOqg.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-400-italic-rmlAbObI.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-400-normal-DmA8jeke.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-400-normal-DtLlO8BS.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-500-normal-DS713u5J.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-500-normal-vXxrTKJT.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-700-italic-CQiE3p2B.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-700-italic-CgjoVhwU.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-700-normal-BnzUNlma.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-700-normal-DEbcy2sF.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-400-italic-DX574ivH.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-400-italic-DeNTv1Y-.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-400-normal-D7N6b6bd.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-400-normal-DpBVrRWA.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-500-normal-CxFoZ42m.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-500-normal-TH5u60nj.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-700-italic-BRJLKiHl.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-700-italic-zm1Jx2ZQ.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-700-normal-CkNjzB9Y.woff +0 -0
- package/dist/client/assets/alegreya-sans-latin-ext-700-normal-HuHd3-Mg.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-400-italic-CmFF8Y5k.woff +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-400-italic-DWlx-poF.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-400-normal-BFvpiE3j.woff +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-400-normal-D7fwYHgb.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-500-normal-B1LHNMw0.woff +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-500-normal-CFLbXWhL.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-700-italic-Dmd7my35.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-700-italic-lOZUTQCU.woff +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-700-normal-CHYTK2w7.woff2 +0 -0
- package/dist/client/assets/alegreya-sans-vietnamese-700-normal-D0056cUE.woff +0 -0
- package/dist/client/assets/alegreya-vietnamese-400-italic-CJsk7YCA.woff2 +0 -0
- package/dist/client/assets/alegreya-vietnamese-400-italic-DD2hiUnx.woff +0 -0
- package/dist/client/assets/alegreya-vietnamese-400-normal-D-Jsyxvv.woff +0 -0
- package/dist/client/assets/alegreya-vietnamese-400-normal-D8CgJdBg.woff2 +0 -0
- package/dist/client/assets/alegreya-vietnamese-700-italic-BHHJBd_M.woff2 +0 -0
- package/dist/client/assets/alegreya-vietnamese-700-italic-Dc7U8ydI.woff +0 -0
- package/dist/client/assets/alegreya-vietnamese-700-normal-CdvEC7Dz.woff +0 -0
- package/dist/client/assets/alegreya-vietnamese-700-normal-CvM8am9u.woff2 +0 -0
- package/dist/client/assets/inconsolata-latin-400-normal-DTZQ6lD6.woff2 +0 -0
- package/dist/client/assets/inconsolata-latin-400-normal-HYADljCo.woff +0 -0
- package/dist/client/assets/inconsolata-latin-700-normal-ByjKuJjN.woff2 +0 -0
- package/dist/client/assets/inconsolata-latin-700-normal-DzgUY3Rl.woff +0 -0
- package/dist/client/assets/inconsolata-latin-ext-400-normal-BaHVOdFB.woff2 +0 -0
- package/dist/client/assets/inconsolata-latin-ext-400-normal-yvPjCxxx.woff +0 -0
- package/dist/client/assets/inconsolata-latin-ext-700-normal-D0Kpgs_9.woff2 +0 -0
- package/dist/client/assets/inconsolata-latin-ext-700-normal-Dlt-daqV.woff +0 -0
- package/dist/client/assets/inconsolata-vietnamese-400-normal-ByiM2lek.woff +0 -0
- package/dist/client/assets/inconsolata-vietnamese-400-normal-DfC_iMic.woff2 +0 -0
- package/dist/client/assets/inconsolata-vietnamese-700-normal-DLCFFAUf.woff +0 -0
- package/dist/client/assets/inconsolata-vietnamese-700-normal-DuasYmn8.woff2 +0 -0
- package/dist/client/assets/index-B7cOmbqb.js +86 -0
- package/dist/client/assets/index-BmYR4aMM.css +1 -0
- package/dist/client/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/dist/client/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/dist/client/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/dist/client/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/dist/client/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/dist/client/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/dist/client/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/dist/client/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/client/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/client/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/client/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/client/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/client/index.html +16 -0
- package/dist/index-pE5CCz6r.d.mts +49 -0
- package/dist/index.d.mts +60 -0
- package/dist/index.mjs +1 -0
- package/package.json +76 -4
package/README.md
CHANGED
package/bin/mdview
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createAdaptorServer } from "@hono/node-server";
|
|
5
|
+
import openBrowser from "open";
|
|
6
|
+
import fs$1 from "node:fs/promises";
|
|
7
|
+
import { serveStatic } from "@hono/node-server/serve-static";
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import { streamSSE } from "hono/streaming";
|
|
10
|
+
import { parseArgs } from "node:util";
|
|
11
|
+
// src/cli/args.ts
|
|
12
|
+
const DEFAULT_PORT = 4173;
|
|
13
|
+
const HELP_TEXT = `Usage: mdview [target] [options]
|
|
14
|
+
|
|
15
|
+
Preview local markdown files in your browser with live reload.
|
|
16
|
+
|
|
17
|
+
Arguments:
|
|
18
|
+
target Directory or markdown file to preview (default: .)
|
|
19
|
+
|
|
20
|
+
Options:
|
|
21
|
+
-p, --port <number> Preferred port (default: ${DEFAULT_PORT}; auto-increments if busy)
|
|
22
|
+
--no-open Do not automatically open the browser
|
|
23
|
+
--theme <light|dark> Force color theme (defaults to OS color mode)
|
|
24
|
+
--stylesheet <path> Path to a custom CSS file
|
|
25
|
+
-h, --help Show this help message
|
|
26
|
+
`;
|
|
27
|
+
/**
|
|
28
|
+
* Parse CLI flags and positional target path.
|
|
29
|
+
*/
|
|
30
|
+
function parseCliArgs(argv) {
|
|
31
|
+
const { values, positionals } = parseArgs({
|
|
32
|
+
args: argv,
|
|
33
|
+
allowPositionals: true,
|
|
34
|
+
options: {
|
|
35
|
+
port: {
|
|
36
|
+
type: "string",
|
|
37
|
+
short: "p"
|
|
38
|
+
},
|
|
39
|
+
"no-open": {
|
|
40
|
+
type: "boolean",
|
|
41
|
+
default: false
|
|
42
|
+
},
|
|
43
|
+
theme: { type: "string" },
|
|
44
|
+
stylesheet: { type: "string" },
|
|
45
|
+
help: {
|
|
46
|
+
type: "boolean",
|
|
47
|
+
short: "h",
|
|
48
|
+
default: false
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
let port = DEFAULT_PORT;
|
|
53
|
+
if (values.port !== void 0) {
|
|
54
|
+
const parsed = Number(values.port);
|
|
55
|
+
if (!Number.isInteger(parsed) || parsed < 1 || parsed > 65535) throw new Error(`Invalid port "${values.port}". Expected an integer between 1 and 65535.`);
|
|
56
|
+
port = parsed;
|
|
57
|
+
}
|
|
58
|
+
let theme = "system";
|
|
59
|
+
if (values.theme !== void 0) {
|
|
60
|
+
if (values.theme !== "light" && values.theme !== "dark") throw new Error(`Invalid theme "${values.theme}". Expected "light" or "dark".`);
|
|
61
|
+
theme = values.theme;
|
|
62
|
+
}
|
|
63
|
+
if (positionals.length > 1) throw new Error(`Expected at most one target path, received ${positionals.length}.`);
|
|
64
|
+
return {
|
|
65
|
+
target: positionals[0] ?? ".",
|
|
66
|
+
port,
|
|
67
|
+
open: !values["no-open"],
|
|
68
|
+
theme,
|
|
69
|
+
stylesheet: values.stylesheet ?? null,
|
|
70
|
+
help: Boolean(values.help)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
// src/server/paths.ts
|
|
74
|
+
const MARKDOWN_EXTENSIONS = /* @__PURE__ */ new Set([".md", ".markdown"]);
|
|
75
|
+
const MIME_TYPES = {
|
|
76
|
+
".apng": "image/apng",
|
|
77
|
+
".avif": "image/avif",
|
|
78
|
+
".bmp": "image/bmp",
|
|
79
|
+
".css": "text/css; charset=utf-8",
|
|
80
|
+
".csv": "text/csv; charset=utf-8",
|
|
81
|
+
".gif": "image/gif",
|
|
82
|
+
".html": "text/html; charset=utf-8",
|
|
83
|
+
".ico": "image/x-icon",
|
|
84
|
+
".jpeg": "image/jpeg",
|
|
85
|
+
".jpg": "image/jpeg",
|
|
86
|
+
".js": "application/javascript; charset=utf-8",
|
|
87
|
+
".json": "application/json; charset=utf-8",
|
|
88
|
+
".md": "text/markdown; charset=utf-8",
|
|
89
|
+
".markdown": "text/markdown; charset=utf-8",
|
|
90
|
+
".mjs": "application/javascript; charset=utf-8",
|
|
91
|
+
".mp4": "video/mp4",
|
|
92
|
+
".pdf": "application/pdf",
|
|
93
|
+
".png": "image/png",
|
|
94
|
+
".svg": "image/svg+xml",
|
|
95
|
+
".txt": "text/plain; charset=utf-8",
|
|
96
|
+
".webm": "video/webm",
|
|
97
|
+
".webp": "image/webp",
|
|
98
|
+
".woff": "font/woff",
|
|
99
|
+
".woff2": "font/woff2"
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Convert an OS-specific relative path to POSIX forward slashes.
|
|
103
|
+
*/
|
|
104
|
+
function toPosixPath(filePath) {
|
|
105
|
+
return filePath.split(path.sep).join("/");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Return true if the filename has a supported markdown extension.
|
|
109
|
+
*/
|
|
110
|
+
function isMarkdownFile(fileName) {
|
|
111
|
+
return MARKDOWN_EXTENSIONS.has(path.extname(fileName).toLowerCase());
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Resolve a user-supplied relative path safely inside rootDir.
|
|
115
|
+
* Returns null if the path is empty, invalid, or escapes rootDir via .. or symlinks.
|
|
116
|
+
*/
|
|
117
|
+
function resolveSafePath(rootDir, requestedPath) {
|
|
118
|
+
if (!requestedPath || requestedPath.includes("\0")) return null;
|
|
119
|
+
const normalizedInput = requestedPath.replace(/^\/+/, "");
|
|
120
|
+
if (!normalizedInput) return null;
|
|
121
|
+
let canonicalRoot;
|
|
122
|
+
try {
|
|
123
|
+
canonicalRoot = fs.realpathSync(rootDir);
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const candidate = path.resolve(canonicalRoot, normalizedInput);
|
|
128
|
+
const relToRoot = path.relative(canonicalRoot, candidate);
|
|
129
|
+
if (!relToRoot || relToRoot.startsWith("..") || path.isAbsolute(relToRoot)) return null;
|
|
130
|
+
if (fs.existsSync(candidate)) try {
|
|
131
|
+
const canonicalTarget = fs.realpathSync(candidate);
|
|
132
|
+
const relCanonical = path.relative(canonicalRoot, canonicalTarget);
|
|
133
|
+
if (relCanonical.startsWith("..") || path.isAbsolute(relCanonical)) return null;
|
|
134
|
+
return canonicalTarget;
|
|
135
|
+
} catch {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return candidate;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Resolve a MIME type from a file extension for raw asset responses.
|
|
142
|
+
*/
|
|
143
|
+
function getAssetMimeType(filePath) {
|
|
144
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
145
|
+
return MIME_TYPES[ext] ?? "application/octet-stream";
|
|
146
|
+
}
|
|
147
|
+
// src/server/scanner.ts
|
|
148
|
+
const IGNORED_DIRECTORY_NAMES = /* @__PURE__ */ new Set([
|
|
149
|
+
"node_modules",
|
|
150
|
+
".git",
|
|
151
|
+
"dist",
|
|
152
|
+
"build",
|
|
153
|
+
".next",
|
|
154
|
+
".turbo",
|
|
155
|
+
"coverage"
|
|
156
|
+
]);
|
|
157
|
+
function shouldSkipDirectory(name) {
|
|
158
|
+
return name.startsWith(".") || IGNORED_DIRECTORY_NAMES.has(name);
|
|
159
|
+
}
|
|
160
|
+
function compareNodes(a, b) {
|
|
161
|
+
if (a.type !== b.type) return a.type === "directory" ? -1 : 1;
|
|
162
|
+
return a.name.localeCompare(b.name, void 0, { sensitivity: "base" });
|
|
163
|
+
}
|
|
164
|
+
async function scanDirectory(rootDir, currentDir, files) {
|
|
165
|
+
let entries;
|
|
166
|
+
try {
|
|
167
|
+
entries = await fs$1.readdir(currentDir, { withFileTypes: true });
|
|
168
|
+
} catch {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
const nodes = [];
|
|
172
|
+
for (const entry of entries) {
|
|
173
|
+
if (entry.isDirectory()) {
|
|
174
|
+
if (shouldSkipDirectory(entry.name)) continue;
|
|
175
|
+
const absSubDir = path.join(currentDir, entry.name);
|
|
176
|
+
const children = await scanDirectory(rootDir, absSubDir, files);
|
|
177
|
+
if (children.length > 0) nodes.push({
|
|
178
|
+
type: "directory",
|
|
179
|
+
name: entry.name,
|
|
180
|
+
path: toPosixPath(path.relative(rootDir, absSubDir)),
|
|
181
|
+
children
|
|
182
|
+
});
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if ((entry.isFile() || entry.isSymbolicLink()) && isMarkdownFile(entry.name)) {
|
|
186
|
+
const absFile = path.join(currentDir, entry.name);
|
|
187
|
+
const relPath = toPosixPath(path.relative(rootDir, absFile));
|
|
188
|
+
files.push(relPath);
|
|
189
|
+
nodes.push({
|
|
190
|
+
type: "file",
|
|
191
|
+
name: entry.name,
|
|
192
|
+
path: relPath
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
nodes.sort(compareNodes);
|
|
197
|
+
return nodes;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Recursively scan rootDir for markdown files, pruning ignored or empty directories.
|
|
201
|
+
*/
|
|
202
|
+
async function scanMarkdownTree(rootDir) {
|
|
203
|
+
const files = [];
|
|
204
|
+
return {
|
|
205
|
+
tree: await scanDirectory(rootDir, rootDir, files),
|
|
206
|
+
files,
|
|
207
|
+
totalFiles: files.length
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Resolve which file should be initially displayed:
|
|
212
|
+
* explicitFile -> root index.md -> root README.md -> single file anywhere -> null.
|
|
213
|
+
*/
|
|
214
|
+
function resolveInitialFile(scan, explicitFile) {
|
|
215
|
+
if (explicitFile) return explicitFile;
|
|
216
|
+
const rootFiles = scan.tree.filter((node) => node.type === "file");
|
|
217
|
+
const indexMatch = rootFiles.find((node) => node.name.toLowerCase() === "index.md");
|
|
218
|
+
if (indexMatch) return indexMatch.path;
|
|
219
|
+
const readmeMatch = rootFiles.find((node) => node.name.toLowerCase() === "readme.md");
|
|
220
|
+
if (readmeMatch) return readmeMatch.path;
|
|
221
|
+
if (scan.totalFiles === 1) return scan.files[0] ?? null;
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
// src/server/watcher.ts
|
|
225
|
+
/**
|
|
226
|
+
* Watch a single target file by monitoring its parent directory so atomic saves
|
|
227
|
+
* (temp file + rename used by editors like VS Code and vim) continue firing events.
|
|
228
|
+
*/
|
|
229
|
+
function watchActiveFile(absFilePath, onEvent) {
|
|
230
|
+
const dirPath = path.dirname(absFilePath);
|
|
231
|
+
const targetName = path.basename(absFilePath);
|
|
232
|
+
let timer;
|
|
233
|
+
let closed = false;
|
|
234
|
+
const emitDebounced = () => {
|
|
235
|
+
if (closed) return;
|
|
236
|
+
if (timer) clearTimeout(timer);
|
|
237
|
+
timer = setTimeout(async () => {
|
|
238
|
+
if (closed) return;
|
|
239
|
+
try {
|
|
240
|
+
await fs.promises.stat(absFilePath);
|
|
241
|
+
onEvent("change");
|
|
242
|
+
} catch {
|
|
243
|
+
onEvent("deleted");
|
|
244
|
+
}
|
|
245
|
+
}, 75);
|
|
246
|
+
};
|
|
247
|
+
let watcher;
|
|
248
|
+
try {
|
|
249
|
+
watcher = fs.watch(dirPath, (_eventType, filename) => {
|
|
250
|
+
if (filename && filename.toString() !== targetName) return;
|
|
251
|
+
emitDebounced();
|
|
252
|
+
});
|
|
253
|
+
watcher.on("error", () => {});
|
|
254
|
+
} catch {
|
|
255
|
+
return () => {};
|
|
256
|
+
}
|
|
257
|
+
return () => {
|
|
258
|
+
if (closed) return;
|
|
259
|
+
closed = true;
|
|
260
|
+
if (timer) clearTimeout(timer);
|
|
261
|
+
watcher?.close();
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
// src/server/app.ts
|
|
265
|
+
/**
|
|
266
|
+
* Create the Hono application serving API endpoints, SSE file watching,
|
|
267
|
+
* local markdown assets, optional custom CSS, and the prebuilt React client.
|
|
268
|
+
*/
|
|
269
|
+
function createPreviewApp(options) {
|
|
270
|
+
const { rootDir, explicitFile = null, theme = "system", stylesheetPath = null, clientDistDir = null } = options;
|
|
271
|
+
const app = new Hono();
|
|
272
|
+
const rootName = path.basename(path.resolve(rootDir)) || rootDir;
|
|
273
|
+
const activeWatchers = /* @__PURE__ */ new Set();
|
|
274
|
+
app.get("/api/config", async (c) => {
|
|
275
|
+
const scan = await scanMarkdownTree(rootDir);
|
|
276
|
+
const initialFile = resolveInitialFile(scan, explicitFile);
|
|
277
|
+
const body = {
|
|
278
|
+
rootName,
|
|
279
|
+
initialFile,
|
|
280
|
+
fileCount: scan.totalFiles,
|
|
281
|
+
theme,
|
|
282
|
+
hasCustomStylesheet: Boolean(stylesheetPath)
|
|
283
|
+
};
|
|
284
|
+
return c.json(body);
|
|
285
|
+
});
|
|
286
|
+
app.get("/api/tree", async (c) => {
|
|
287
|
+
const scan = await scanMarkdownTree(rootDir);
|
|
288
|
+
const body = {
|
|
289
|
+
rootName,
|
|
290
|
+
totalFiles: scan.totalFiles,
|
|
291
|
+
tree: scan.tree
|
|
292
|
+
};
|
|
293
|
+
return c.json(body);
|
|
294
|
+
});
|
|
295
|
+
app.get("/api/file", async (c) => {
|
|
296
|
+
const requestedPath = c.req.query("path") ?? "";
|
|
297
|
+
const absPath = resolveSafePath(rootDir, requestedPath);
|
|
298
|
+
if (!absPath || !isMarkdownFile(absPath)) return c.json({ error: "Invalid or forbidden markdown path" }, 403);
|
|
299
|
+
try {
|
|
300
|
+
const stat = await fs$1.stat(absPath);
|
|
301
|
+
if (!stat.isFile()) return c.json({ error: "File not found" }, 404);
|
|
302
|
+
const content = await fs$1.readFile(absPath, "utf8");
|
|
303
|
+
const body = {
|
|
304
|
+
path: requestedPath.replace(/^\/+/, ""),
|
|
305
|
+
content,
|
|
306
|
+
mtimeMs: stat.mtimeMs
|
|
307
|
+
};
|
|
308
|
+
return c.json(body);
|
|
309
|
+
} catch {
|
|
310
|
+
return c.json({ error: "File not found" }, 404);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
app.get("/api/events", async (c) => {
|
|
314
|
+
const requestedPath = c.req.query("file") ?? "";
|
|
315
|
+
const absPath = resolveSafePath(rootDir, requestedPath);
|
|
316
|
+
if (!absPath || !isMarkdownFile(absPath)) return c.json({ error: "Invalid or forbidden markdown path" }, 403);
|
|
317
|
+
return streamSSE(c, async (stream) => {
|
|
318
|
+
let finished = false;
|
|
319
|
+
let stopWatching = () => {};
|
|
320
|
+
let resolveDone;
|
|
321
|
+
const cleanup = () => {
|
|
322
|
+
if (finished) return;
|
|
323
|
+
finished = true;
|
|
324
|
+
stopWatching();
|
|
325
|
+
activeWatchers.delete(cleanup);
|
|
326
|
+
resolveDone?.();
|
|
327
|
+
};
|
|
328
|
+
activeWatchers.add(cleanup);
|
|
329
|
+
stream.onAbort(cleanup);
|
|
330
|
+
await stream.writeSSE({
|
|
331
|
+
event: "connected",
|
|
332
|
+
data: JSON.stringify({ path: requestedPath })
|
|
333
|
+
});
|
|
334
|
+
stopWatching = watchActiveFile(absPath, (eventType) => {
|
|
335
|
+
if (finished) return;
|
|
336
|
+
stream.writeSSE({
|
|
337
|
+
event: eventType,
|
|
338
|
+
data: JSON.stringify({
|
|
339
|
+
path: requestedPath,
|
|
340
|
+
ts: Date.now()
|
|
341
|
+
})
|
|
342
|
+
}).catch(cleanup);
|
|
343
|
+
});
|
|
344
|
+
await new Promise((resolve) => {
|
|
345
|
+
resolveDone = resolve;
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
app.get("/raw/*", async (c) => {
|
|
350
|
+
const rawWildcard = c.req.path.slice(5);
|
|
351
|
+
let decodedPath;
|
|
352
|
+
try {
|
|
353
|
+
decodedPath = decodeURIComponent(rawWildcard);
|
|
354
|
+
} catch {
|
|
355
|
+
return c.text("Bad path encoding", 400);
|
|
356
|
+
}
|
|
357
|
+
const absPath = resolveSafePath(rootDir, decodedPath);
|
|
358
|
+
if (!absPath) return c.text("Forbidden", 403);
|
|
359
|
+
try {
|
|
360
|
+
if (!(await fs$1.stat(absPath)).isFile()) return c.text("Not found", 404);
|
|
361
|
+
const data = await fs$1.readFile(absPath);
|
|
362
|
+
return c.body(data, 200, {
|
|
363
|
+
"Content-Type": getAssetMimeType(absPath),
|
|
364
|
+
"Cache-Control": "no-cache"
|
|
365
|
+
});
|
|
366
|
+
} catch {
|
|
367
|
+
return c.text("Not found", 404);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
app.get("/custom.css", async (c) => {
|
|
371
|
+
if (!stylesheetPath) return c.text("Not configured", 404);
|
|
372
|
+
try {
|
|
373
|
+
const css = await fs$1.readFile(stylesheetPath, "utf8");
|
|
374
|
+
return c.body(css, 200, {
|
|
375
|
+
"Content-Type": "text/css; charset=utf-8",
|
|
376
|
+
"Cache-Control": "no-store"
|
|
377
|
+
});
|
|
378
|
+
} catch {
|
|
379
|
+
return c.text("Stylesheet not found", 404);
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
if (clientDistDir && fs.existsSync(clientDistDir)) {
|
|
383
|
+
app.use("/*", serveStatic({ root: clientDistDir }));
|
|
384
|
+
app.get("/*", serveStatic({ path: path.join(clientDistDir, "index.html") }));
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
app,
|
|
388
|
+
closeWatchers: () => {
|
|
389
|
+
for (const cleanup of [...activeWatchers]) cleanup();
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
// src/cli/index.ts
|
|
394
|
+
function resolveDefaultClientDistDir() {
|
|
395
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
396
|
+
const candidates = [path.resolve(currentDir, "client"), path.resolve(currentDir, "../../dist/client")];
|
|
397
|
+
for (const candidate of candidates) if (fs.existsSync(path.join(candidate, "index.html"))) return candidate;
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
function listenOnPort(server, port, host) {
|
|
401
|
+
return new Promise((resolve, reject) => {
|
|
402
|
+
const onError = (err) => {
|
|
403
|
+
server.off("listening", onListening);
|
|
404
|
+
reject(err);
|
|
405
|
+
};
|
|
406
|
+
const onListening = () => {
|
|
407
|
+
server.off("error", onError);
|
|
408
|
+
const addr = server.address();
|
|
409
|
+
resolve(typeof addr === "object" && addr ? addr.port : port);
|
|
410
|
+
};
|
|
411
|
+
server.once("error", onError);
|
|
412
|
+
server.once("listening", onListening);
|
|
413
|
+
server.listen(port, host);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
async function listenWithFallback(server, preferredPort, host) {
|
|
417
|
+
const maxAttempts = 20;
|
|
418
|
+
for (let offset = 0; offset < maxAttempts; offset++) {
|
|
419
|
+
const candidatePort = preferredPort + offset;
|
|
420
|
+
if (candidatePort > 65535) break;
|
|
421
|
+
try {
|
|
422
|
+
return await listenOnPort(server, candidatePort, host);
|
|
423
|
+
} catch (err) {
|
|
424
|
+
if (err.code === "EADDRINUSE") continue;
|
|
425
|
+
throw err;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return listenOnPort(server, 0, host);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Resolve target paths, bind the local HTTP server, and optionally open the browser.
|
|
432
|
+
*/
|
|
433
|
+
async function startPreviewServer(options = {}) {
|
|
434
|
+
const resolvedTarget = path.resolve(options.target ?? ".");
|
|
435
|
+
if (!fs.existsSync(resolvedTarget)) throw new Error(`Target path does not exist: ${resolvedTarget}`);
|
|
436
|
+
const targetStat = fs.statSync(resolvedTarget);
|
|
437
|
+
let rootDir = resolvedTarget;
|
|
438
|
+
let explicitFile = null;
|
|
439
|
+
if (targetStat.isFile()) {
|
|
440
|
+
if (!isMarkdownFile(resolvedTarget)) throw new Error(`Target file is not a markdown document: ${resolvedTarget}`);
|
|
441
|
+
rootDir = path.dirname(resolvedTarget);
|
|
442
|
+
explicitFile = path.basename(resolvedTarget);
|
|
443
|
+
} else if (!targetStat.isDirectory()) throw new Error(`Target path must be a directory or markdown file: ${resolvedTarget}`);
|
|
444
|
+
let resolvedStylesheet = null;
|
|
445
|
+
if (options.stylesheet) {
|
|
446
|
+
resolvedStylesheet = path.resolve(options.stylesheet);
|
|
447
|
+
if (!fs.existsSync(resolvedStylesheet) || !fs.statSync(resolvedStylesheet).isFile()) throw new Error(`Stylesheet file not found: ${resolvedStylesheet}`);
|
|
448
|
+
}
|
|
449
|
+
const clientDistDir = options.clientDistDir === void 0 ? resolveDefaultClientDistDir() : options.clientDistDir;
|
|
450
|
+
const { app, closeWatchers } = createPreviewApp({
|
|
451
|
+
rootDir,
|
|
452
|
+
explicitFile,
|
|
453
|
+
theme: options.theme ?? "system",
|
|
454
|
+
stylesheetPath: resolvedStylesheet,
|
|
455
|
+
clientDistDir
|
|
456
|
+
});
|
|
457
|
+
const server = createAdaptorServer({ fetch: app.fetch });
|
|
458
|
+
const port = await listenWithFallback(server, options.port ?? 4173, "127.0.0.1");
|
|
459
|
+
const url = `http://127.0.0.1:${port}`;
|
|
460
|
+
if (options.open ?? true) await openBrowser(url);
|
|
461
|
+
return {
|
|
462
|
+
url,
|
|
463
|
+
port,
|
|
464
|
+
rootDir,
|
|
465
|
+
close: () => new Promise((resolve, reject) => {
|
|
466
|
+
closeWatchers();
|
|
467
|
+
server.close((err) => {
|
|
468
|
+
if (err) reject(err);
|
|
469
|
+
else resolve();
|
|
470
|
+
});
|
|
471
|
+
})
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
async function runCli() {
|
|
475
|
+
let options;
|
|
476
|
+
try {
|
|
477
|
+
options = parseCliArgs(process.argv.slice(2));
|
|
478
|
+
} catch (err) {
|
|
479
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
480
|
+
console.error(`Error: ${message}\n`);
|
|
481
|
+
console.error(HELP_TEXT);
|
|
482
|
+
process.exitCode = 1;
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (options.help) {
|
|
486
|
+
console.log(HELP_TEXT);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
try {
|
|
490
|
+
const running = await startPreviewServer(options);
|
|
491
|
+
console.log(`mdview serving ${running.rootDir}`);
|
|
492
|
+
console.log(`Preview URL: ${running.url}`);
|
|
493
|
+
console.log("Press Ctrl+C to stop.");
|
|
494
|
+
const shutdown = async () => {
|
|
495
|
+
await running.close();
|
|
496
|
+
process.exit(0);
|
|
497
|
+
};
|
|
498
|
+
process.once("SIGINT", () => void shutdown());
|
|
499
|
+
process.once("SIGTERM", () => void shutdown());
|
|
500
|
+
} catch (err) {
|
|
501
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
502
|
+
console.error(`Error: ${message}`);
|
|
503
|
+
process.exitCode = 1;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const invokedPath = process.argv[1] ? fs.realpathSync(process.argv[1]) : "";
|
|
507
|
+
if (invokedPath === fs.realpathSync(fileURLToPath(import.meta.url)) || path.basename(invokedPath) === "mdview") runCli();
|
|
508
|
+
export { isMarkdownFile as a, DEFAULT_PORT as c, scanMarkdownTree as i, HELP_TEXT as l, createPreviewApp as n, resolveSafePath as o, resolveInitialFile as r, toPosixPath as s, startPreviewServer as t, parseCliArgs as u };
|
package/dist/cli.d.mts
ADDED
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as startPreviewServer } from "./cli-BhFYKMLx.mjs";
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|