mdsvr 1.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/LICENSE +21 -0
- package/README.md +65 -0
- package/bin/mdsvr.js +3 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +134 -0
- package/dist/cli.js.map +1 -0
- package/dist/directory.d.ts +6 -0
- package/dist/directory.d.ts.map +1 -0
- package/dist/directory.js +143 -0
- package/dist/directory.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/renderer.d.ts +2 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +30 -0
- package/dist/renderer.js.map +1 -0
- package/dist/router.d.ts +3 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +139 -0
- package/dist/router.js.map +1 -0
- package/dist/server.d.ts +4 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +65 -0
- package/dist/server.js.map +1 -0
- package/dist/template.d.ts +6 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +188 -0
- package/dist/template.js.map +1 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Truong Ngoc Vuong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# mdsvr
|
|
2
|
+
|
|
3
|
+
Static file server with auto-render Markdown → HTML. Similar to Vercel's `serve` but understands `.md` files.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# One-shot, no install
|
|
9
|
+
npx mdsvr ./docs
|
|
10
|
+
|
|
11
|
+
# With options
|
|
12
|
+
npx mdsvr ./notes --port 4000 --open
|
|
13
|
+
|
|
14
|
+
# Expose to LAN
|
|
15
|
+
npx mdsvr . --host 0.0.0.0 --port 8080
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install -g mdsvr
|
|
22
|
+
mdsvr ./docs
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## CLI Usage
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
Usage: mdsvr [dir] [options]
|
|
29
|
+
|
|
30
|
+
Options:
|
|
31
|
+
[dir] Root directory to serve (default: .)
|
|
32
|
+
-p, --port N Port number (default: 3000)
|
|
33
|
+
--host H Bind address (default: localhost)
|
|
34
|
+
-o, --open Auto-open browser
|
|
35
|
+
-s, --silent Suppress console output
|
|
36
|
+
-v, --version Print version
|
|
37
|
+
-h, --help Print this help
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Programmatic API
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { createServer } from "mdsvr";
|
|
44
|
+
|
|
45
|
+
const server = await createServer("./docs", { port: 4000, open: true });
|
|
46
|
+
console.log(`Running at ${server.url}`);
|
|
47
|
+
|
|
48
|
+
// Graceful shutdown
|
|
49
|
+
process.on("SIGINT", () => server.close());
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- **Markdown rendering**: `.md` files automatically rendered as beautiful GitHub-style HTML
|
|
55
|
+
- **Directory listings**: Browse directories with clickable file links
|
|
56
|
+
- **Static files**: Serve any file type with correct MIME types
|
|
57
|
+
- **Syntax highlighting**: Code blocks highlighted with highlight.js
|
|
58
|
+
- **Anchor links**: Headers have clickable anchors
|
|
59
|
+
- **Mobile responsive**: Works on all devices
|
|
60
|
+
- **Path traversal protection**: Secure by default
|
|
61
|
+
- **Zero config**: Works out of the box
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/bin/mdsvr.js
ADDED
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createServer, getNetworkAddress } from "./server.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
function parseArgs(argv) {
|
|
7
|
+
const args = {
|
|
8
|
+
dir: ".",
|
|
9
|
+
port: 3000,
|
|
10
|
+
host: "localhost",
|
|
11
|
+
open: false,
|
|
12
|
+
silent: false,
|
|
13
|
+
version: false,
|
|
14
|
+
help: false,
|
|
15
|
+
};
|
|
16
|
+
for (let i = 0; i < argv.length; i++) {
|
|
17
|
+
const arg = argv[i];
|
|
18
|
+
if (arg === "--help" || arg === "-h") {
|
|
19
|
+
args.help = true;
|
|
20
|
+
}
|
|
21
|
+
else if (arg === "--version" || arg === "-v") {
|
|
22
|
+
args.version = true;
|
|
23
|
+
}
|
|
24
|
+
else if (arg === "--port" || arg === "-p") {
|
|
25
|
+
const val = argv[++i];
|
|
26
|
+
if (val)
|
|
27
|
+
args.port = parseInt(val, 10);
|
|
28
|
+
}
|
|
29
|
+
else if (arg === "--host") {
|
|
30
|
+
const val = argv[++i];
|
|
31
|
+
if (val)
|
|
32
|
+
args.host = val;
|
|
33
|
+
}
|
|
34
|
+
else if (arg === "--open" || arg === "-o") {
|
|
35
|
+
args.open = true;
|
|
36
|
+
}
|
|
37
|
+
else if (arg === "--silent" || arg === "-s") {
|
|
38
|
+
args.silent = true;
|
|
39
|
+
}
|
|
40
|
+
else if (!arg.startsWith("--") && !arg.startsWith("-")) {
|
|
41
|
+
args.dir = arg;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return args;
|
|
45
|
+
}
|
|
46
|
+
function printHelp() {
|
|
47
|
+
console.log(`
|
|
48
|
+
Usage: mdsvr [dir] [options]
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
[dir] Root directory to serve (default: .)
|
|
52
|
+
-p, --port N Port number (default: 3000)
|
|
53
|
+
--host H Bind address (default: localhost)
|
|
54
|
+
-o, --open Auto-open browser
|
|
55
|
+
-s, --silent Suppress console output
|
|
56
|
+
-v, --version Print version
|
|
57
|
+
-h, --help Print this help
|
|
58
|
+
|
|
59
|
+
Examples:
|
|
60
|
+
mdsvr ./docs
|
|
61
|
+
mdsvr ./notes --port 4000 --open
|
|
62
|
+
mdsvr . --host 0.0.0.0 --port 8080
|
|
63
|
+
`);
|
|
64
|
+
}
|
|
65
|
+
async function getVersion() {
|
|
66
|
+
try {
|
|
67
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
68
|
+
const pkgPath = path.join(__dirname, "..", "package.json");
|
|
69
|
+
const pkg = JSON.parse(await readFile(pkgPath, "utf-8"));
|
|
70
|
+
return pkg.version || "1.0.0";
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return "1.0.0";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function openBrowser(url) {
|
|
77
|
+
const { exec } = await import("node:child_process");
|
|
78
|
+
const platform = process.platform;
|
|
79
|
+
const cmd = platform === "darwin"
|
|
80
|
+
? `open`
|
|
81
|
+
: platform === "win32"
|
|
82
|
+
? `start`
|
|
83
|
+
: `xdg-open`;
|
|
84
|
+
exec(`${cmd} "${url}"`);
|
|
85
|
+
}
|
|
86
|
+
async function main() {
|
|
87
|
+
const args = parseArgs(process.argv.slice(2));
|
|
88
|
+
if (args.help) {
|
|
89
|
+
printHelp();
|
|
90
|
+
process.exit(0);
|
|
91
|
+
}
|
|
92
|
+
if (args.version) {
|
|
93
|
+
const version = await getVersion();
|
|
94
|
+
console.log(`mdsvr v${version}`);
|
|
95
|
+
process.exit(0);
|
|
96
|
+
}
|
|
97
|
+
const absoluteDir = path.resolve(args.dir);
|
|
98
|
+
try {
|
|
99
|
+
const server = await createServer(args.dir, {
|
|
100
|
+
port: args.port,
|
|
101
|
+
host: args.host,
|
|
102
|
+
open: args.open,
|
|
103
|
+
silent: args.silent,
|
|
104
|
+
});
|
|
105
|
+
const version = await getVersion();
|
|
106
|
+
if (!args.silent) {
|
|
107
|
+
console.log(`\n mdsvr v${version}\n`);
|
|
108
|
+
console.log(` Local: ${server.url}`);
|
|
109
|
+
const networkAddr = getNetworkAddress();
|
|
110
|
+
if (args.host !== "localhost" && networkAddr) {
|
|
111
|
+
console.log(` Network: http://${networkAddr}:${server.port}`);
|
|
112
|
+
}
|
|
113
|
+
console.log(`\n Serving ${absoluteDir}`);
|
|
114
|
+
console.log(` Hit Ctrl+C to stop.\n`);
|
|
115
|
+
}
|
|
116
|
+
if (args.open) {
|
|
117
|
+
await openBrowser(server.url);
|
|
118
|
+
}
|
|
119
|
+
// Graceful shutdown
|
|
120
|
+
process.on("SIGINT", async () => {
|
|
121
|
+
if (!args.silent) {
|
|
122
|
+
console.log("\nShutting down...");
|
|
123
|
+
}
|
|
124
|
+
await server.close();
|
|
125
|
+
process.exit(0);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
console.error("Error:", err instanceof Error ? err.message : err);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
main();
|
|
134
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAYzC,SAAS,SAAS,CAAC,IAAc;IAC/B,MAAM,IAAI,GAAe;QACvB,GAAG,EAAE,GAAG;QACR,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,KAAK;QACX,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,KAAK;QACd,IAAI,EAAE,KAAK;KACZ,CAAC;IAEF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC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,CAAC;YACpC,CAAC;YACD,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;YACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAClE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directory.d.ts","sourceRoot":"","sources":["../src/directory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,wBAAgB,eAAe,CAAC,MAAM,EAAE;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,GAAG,MAAM,CAgHT"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export function renderDirectory(params) {
|
|
2
|
+
const { urlPath, entries } = params;
|
|
3
|
+
// Sort: folders first, then files (alphabetically)
|
|
4
|
+
const sorted = [...entries].sort((a, b) => {
|
|
5
|
+
if (a.isDirectory() && !b.isDirectory())
|
|
6
|
+
return -1;
|
|
7
|
+
if (!a.isDirectory() && b.isDirectory())
|
|
8
|
+
return 1;
|
|
9
|
+
return a.name.localeCompare(b.name);
|
|
10
|
+
});
|
|
11
|
+
const rows = sorted
|
|
12
|
+
.map((entry) => {
|
|
13
|
+
const name = entry.name;
|
|
14
|
+
const isDir = entry.isDirectory();
|
|
15
|
+
const icon = isDir ? "📁" : name.endsWith(".md") ? "📄" : "📃";
|
|
16
|
+
const href = encodeURIComponent(name) + (isDir ? "/" : "");
|
|
17
|
+
const size = !isDir && entry.isFile()
|
|
18
|
+
? formatSize(entry.size || 0)
|
|
19
|
+
: "-";
|
|
20
|
+
return ` <tr>
|
|
21
|
+
<td>${icon} <a href="${href}">${escapeHtml(name)}${isDir ? "/" : ""}</a></td>
|
|
22
|
+
<td class="size">${size}</td>
|
|
23
|
+
</tr>`;
|
|
24
|
+
})
|
|
25
|
+
.join("\n");
|
|
26
|
+
const breadcrumb = generateBreadcrumb(urlPath);
|
|
27
|
+
return `<!DOCTYPE html>
|
|
28
|
+
<html lang="en">
|
|
29
|
+
<head>
|
|
30
|
+
<meta charset="UTF-8">
|
|
31
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
32
|
+
<title>Index of ${escapeHtml(urlPath)}</title>
|
|
33
|
+
<style>
|
|
34
|
+
* { box-sizing: border-box; }
|
|
35
|
+
body {
|
|
36
|
+
margin: 0;
|
|
37
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
|
|
38
|
+
font-size: 16px;
|
|
39
|
+
line-height: 1.6;
|
|
40
|
+
color: #24292f;
|
|
41
|
+
background: #f6f8fa;
|
|
42
|
+
}
|
|
43
|
+
.header {
|
|
44
|
+
background: #0d1117;
|
|
45
|
+
padding: 16px 24px;
|
|
46
|
+
border-bottom: 1px solid #30363d;
|
|
47
|
+
}
|
|
48
|
+
.header-logo {
|
|
49
|
+
color: #f0f6fc;
|
|
50
|
+
font-weight: 600;
|
|
51
|
+
font-size: 14px;
|
|
52
|
+
text-decoration: none;
|
|
53
|
+
}
|
|
54
|
+
.container {
|
|
55
|
+
max-width: 860px;
|
|
56
|
+
margin: 32px auto;
|
|
57
|
+
padding: 32px;
|
|
58
|
+
background: #ffffff;
|
|
59
|
+
border: 1px solid #d0d7de;
|
|
60
|
+
border-radius: 6px;
|
|
61
|
+
}
|
|
62
|
+
h1 { font-size: 20px; font-weight: 600; margin: 0 0 16px; color: #1f2328; }
|
|
63
|
+
.breadcrumb { font-size: 14px; color: #656d76; margin-bottom: 20px; }
|
|
64
|
+
.breadcrumb a { color: #0969da; text-decoration: none; }
|
|
65
|
+
.breadcrumb a:hover { text-decoration: underline; }
|
|
66
|
+
table { width: 100%; border-collapse: collapse; }
|
|
67
|
+
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #d8dee4; }
|
|
68
|
+
th { font-weight: 600; color: #656d76; font-size: 14px; }
|
|
69
|
+
td { font-size: 14px; }
|
|
70
|
+
td a { color: #0969da; text-decoration: none; }
|
|
71
|
+
td a:hover { text-decoration: underline; }
|
|
72
|
+
.size { color: #656d76; text-align: right; width: 100px; }
|
|
73
|
+
.empty { color: #656d76; font-style: italic; padding: 24px 0; }
|
|
74
|
+
@media (max-width: 900px) {
|
|
75
|
+
.container { margin: 16px; padding: 20px; }
|
|
76
|
+
.header { padding: 12px 16px; }
|
|
77
|
+
}
|
|
78
|
+
@media (max-width: 480px) {
|
|
79
|
+
.container { margin: 8px; padding: 16px; }
|
|
80
|
+
}
|
|
81
|
+
</style>
|
|
82
|
+
</head>
|
|
83
|
+
<body>
|
|
84
|
+
<header class="header">
|
|
85
|
+
<a href="/" class="header-logo">mdsvr</a>
|
|
86
|
+
</header>
|
|
87
|
+
<div class="container">
|
|
88
|
+
<h1>Index of ${escapeHtml(urlPath) || "/"}</h1>
|
|
89
|
+
<div class="breadcrumb">${breadcrumb}</div>
|
|
90
|
+
<table>
|
|
91
|
+
<thead>
|
|
92
|
+
<tr><th>Name</th><th class="size">Size</th></tr>
|
|
93
|
+
</thead>
|
|
94
|
+
<tbody>
|
|
95
|
+
${urlPath
|
|
96
|
+
? ` <tr>
|
|
97
|
+
<td>📁 <a href="../">../</a></td>
|
|
98
|
+
<td class="size">-</td>
|
|
99
|
+
</tr>`
|
|
100
|
+
: ""}
|
|
101
|
+
${rows || ' <tr><td colspan="2" class="empty">Empty directory</td></tr>'}
|
|
102
|
+
</tbody>
|
|
103
|
+
</table>
|
|
104
|
+
</div>
|
|
105
|
+
</body>
|
|
106
|
+
</html>`;
|
|
107
|
+
}
|
|
108
|
+
function generateBreadcrumb(urlPath) {
|
|
109
|
+
if (!urlPath || urlPath === "/") {
|
|
110
|
+
return "<strong>home</strong>";
|
|
111
|
+
}
|
|
112
|
+
const parts = urlPath.split("/").filter(Boolean);
|
|
113
|
+
let accum = "";
|
|
114
|
+
const links = parts.map((part, i) => {
|
|
115
|
+
accum += "/" + part;
|
|
116
|
+
const isLast = i === parts.length - 1;
|
|
117
|
+
if (isLast) {
|
|
118
|
+
return `<strong>${escapeHtml(part)}</strong>`;
|
|
119
|
+
}
|
|
120
|
+
return `<a href="${accum}/">${escapeHtml(part)}</a>`;
|
|
121
|
+
});
|
|
122
|
+
return '<a href="/">home</a> / ' + links.join(" / ");
|
|
123
|
+
}
|
|
124
|
+
function formatSize(bytes) {
|
|
125
|
+
if (bytes === 0)
|
|
126
|
+
return "0 B";
|
|
127
|
+
const units = ["B", "KB", "MB", "GB"];
|
|
128
|
+
const k = 1024;
|
|
129
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
130
|
+
const size = parseFloat((bytes / Math.pow(k, i)).toFixed(1));
|
|
131
|
+
return `${size} ${units[i]}`;
|
|
132
|
+
}
|
|
133
|
+
function escapeHtml(text) {
|
|
134
|
+
const htmlEscapes = {
|
|
135
|
+
"&": "&",
|
|
136
|
+
"<": "<",
|
|
137
|
+
">": ">",
|
|
138
|
+
'"': """,
|
|
139
|
+
"'": "'",
|
|
140
|
+
};
|
|
141
|
+
return text.replace(/[&<>"']/g, (char) => htmlEscapes[char] || char);
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=directory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directory.js","sourceRoot":"","sources":["../src/directory.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,eAAe,CAAC,MAG/B;IACC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAEpC,mDAAmD;IACnD,MAAM,MAAM,GAA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,MAAM,CAAC,GAAG,IAAI,CAAC;IACf,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,MAAM,IAAI,GAAG,UAAU,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7D,OAAO,GAAG,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/B,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC9B,MAAM,WAAW,GAA2B;QAC1C,GAAG,EAAE,OAAO;QACZ,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,QAAQ;QACb,GAAG,EAAE,QAAQ;KACd,CAAC;IACF,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;AACvE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../src/renderer.ts"],"names":[],"mappings":"AA+BA,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEtD"}
|
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import MarkdownIt from "markdown-it";
|
|
2
|
+
import markdownItAnchor from "markdown-it-anchor";
|
|
3
|
+
import hljs from "highlight.js";
|
|
4
|
+
const md = new MarkdownIt({
|
|
5
|
+
html: true,
|
|
6
|
+
linkify: true,
|
|
7
|
+
typographer: true,
|
|
8
|
+
highlight: (str, lang) => {
|
|
9
|
+
if (lang && hljs.getLanguage(lang)) {
|
|
10
|
+
try {
|
|
11
|
+
return hljs.highlight(str, { language: lang }).value;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// Fall through to plain text
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return md.utils.escapeHtml(str);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
md.use(markdownItAnchor, {
|
|
21
|
+
permalink: false,
|
|
22
|
+
slugify: (s) => encodeURIComponent(String(s)
|
|
23
|
+
.trim()
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.replace(/\s+/g, "-")),
|
|
26
|
+
});
|
|
27
|
+
export function renderMarkdown(content) {
|
|
28
|
+
return md.render(content);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=renderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderer.js","sourceRoot":"","sources":["../src/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,IAAI,MAAM,cAAc,CAAC;AAEhC,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC;IACxB,IAAI,EAAE,IAAI;IACV,OAAO,EAAE,IAAI;IACb,WAAW,EAAE,IAAI;IACjB,SAAS,EAAE,CAAC,GAAW,EAAE,IAAY,EAAU,EAAE;QAC/C,IAAI,IAAI,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YACnC,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC;YACvD,CAAC;YAAC,MAAM,CAAC;gBACP,6BAA6B;YAC/B,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;CACF,CAAC,CAAC;AAEH,EAAE,CAAC,GAAG,CAAC,gBAAgB,EAAE;IACvB,SAAS,EAAE,KAAK;IAChB,OAAO,EAAE,CAAC,CAAS,EAAE,EAAE,CACrB,kBAAkB,CAChB,MAAM,CAAC,CAAC,CAAC;SACN,IAAI,EAAE;SACN,WAAW,EAAE;SACb,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CACxB;CACJ,CAAC,CAAC;AAEH,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,OAAO,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC"}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAgC5D,wBAAsB,KAAK,CACzB,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAmCf"}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { promises as fs, createReadStream } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { renderMarkdown } from "./renderer.js";
|
|
4
|
+
import { wrapHtml } from "./template.js";
|
|
5
|
+
import { renderDirectory } from "./directory.js";
|
|
6
|
+
const MIME = {
|
|
7
|
+
".html": "text/html",
|
|
8
|
+
".css": "text/css",
|
|
9
|
+
".js": "text/javascript",
|
|
10
|
+
".mjs": "text/javascript",
|
|
11
|
+
".json": "application/json",
|
|
12
|
+
".png": "image/png",
|
|
13
|
+
".jpg": "image/jpeg",
|
|
14
|
+
".jpeg": "image/jpeg",
|
|
15
|
+
".gif": "image/gif",
|
|
16
|
+
".svg": "image/svg+xml",
|
|
17
|
+
".ico": "image/x-icon",
|
|
18
|
+
".pdf": "application/pdf",
|
|
19
|
+
".txt": "text/plain",
|
|
20
|
+
".webp": "image/webp",
|
|
21
|
+
".md": "text/html",
|
|
22
|
+
".woff": "font/woff",
|
|
23
|
+
".woff2": "font/woff2",
|
|
24
|
+
".ttf": "font/ttf",
|
|
25
|
+
".otf": "font/otf",
|
|
26
|
+
".eot": "application/vnd.ms-fontobject",
|
|
27
|
+
".xml": "application/xml",
|
|
28
|
+
".zip": "application/zip",
|
|
29
|
+
};
|
|
30
|
+
export async function route(req, res, rootDir) {
|
|
31
|
+
// Only handle GET and HEAD
|
|
32
|
+
if (req.method && !["GET", "HEAD"].includes(req.method)) {
|
|
33
|
+
sendError(res, 405, "Method Not Allowed");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const url = req.url || "/";
|
|
37
|
+
const urlPath = decodeURIComponent(url.split("?")[0]);
|
|
38
|
+
// Resolve path and check for path traversal
|
|
39
|
+
const resolvedPath = path.resolve(path.join(rootDir, urlPath));
|
|
40
|
+
if (!resolvedPath.startsWith(rootDir)) {
|
|
41
|
+
sendError(res, 403, "Forbidden");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const stat = await fs.stat(resolvedPath);
|
|
46
|
+
if (stat.isDirectory()) {
|
|
47
|
+
await serveDirectory(res, resolvedPath, urlPath, rootDir);
|
|
48
|
+
}
|
|
49
|
+
else if (urlPath.endsWith(".md")) {
|
|
50
|
+
await serveMarkdown(res, resolvedPath, urlPath);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
await serveStatic(req, res, resolvedPath);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
const code = err.code;
|
|
58
|
+
if (code === "ENOENT") {
|
|
59
|
+
sendError(res, 404, "Not Found");
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
sendError(res, 500, "Internal Server Error");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function serveDirectory(res, dirPath, urlPath, rootDir) {
|
|
67
|
+
// Try to find index.md or index.html
|
|
68
|
+
const indexMd = path.join(dirPath, "index.md");
|
|
69
|
+
const indexHtml = path.join(dirPath, "index.html");
|
|
70
|
+
try {
|
|
71
|
+
await fs.access(indexMd);
|
|
72
|
+
await serveMarkdown(res, indexMd, path.join(urlPath, "index.md"));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Continue to check index.html
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
await fs.access(indexHtml);
|
|
80
|
+
await serveStatic({ method: "GET", url: urlPath }, res, indexHtml);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// Continue to directory listing
|
|
85
|
+
}
|
|
86
|
+
// Read directory contents
|
|
87
|
+
const entries = await fs.readdir(dirPath, { withFileTypes: true });
|
|
88
|
+
// Add size information to entries
|
|
89
|
+
const entriesWithSize = await Promise.all(entries.map(async (entry) => {
|
|
90
|
+
if (entry.isFile()) {
|
|
91
|
+
const fullPath = path.join(dirPath, entry.name);
|
|
92
|
+
const stat = await fs.stat(fullPath);
|
|
93
|
+
return Object.assign(entry, { size: stat.size });
|
|
94
|
+
}
|
|
95
|
+
return Object.assign(entry, { size: 0 });
|
|
96
|
+
}));
|
|
97
|
+
const html = renderDirectory({
|
|
98
|
+
urlPath,
|
|
99
|
+
entries: entriesWithSize,
|
|
100
|
+
});
|
|
101
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
102
|
+
res.end(html);
|
|
103
|
+
}
|
|
104
|
+
async function serveMarkdown(res, filePath, urlPath) {
|
|
105
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
106
|
+
const rendered = renderMarkdown(content);
|
|
107
|
+
const title = path.basename(filePath, ".md");
|
|
108
|
+
const html = wrapHtml({
|
|
109
|
+
title,
|
|
110
|
+
body: rendered,
|
|
111
|
+
filePath: urlPath,
|
|
112
|
+
});
|
|
113
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
114
|
+
res.end(html);
|
|
115
|
+
}
|
|
116
|
+
function serveStatic(req, res, filePath) {
|
|
117
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
118
|
+
const contentType = MIME[ext] || "application/octet-stream";
|
|
119
|
+
const stream = createReadStream(filePath);
|
|
120
|
+
stream.on("error", () => {
|
|
121
|
+
sendError(res, 500, "Internal Server Error");
|
|
122
|
+
});
|
|
123
|
+
res.writeHead(200, { "Content-Type": contentType });
|
|
124
|
+
stream.pipe(res);
|
|
125
|
+
}
|
|
126
|
+
function sendError(res, code, message) {
|
|
127
|
+
const html = `<!DOCTYPE html>
|
|
128
|
+
<html>
|
|
129
|
+
<head><title>${code} — ${message}</title></head>
|
|
130
|
+
<body style="font-family: sans-serif; text-align: center; padding: 50px;">
|
|
131
|
+
<h1>${code}</h1>
|
|
132
|
+
<p>${message}</p>
|
|
133
|
+
<a href="/">← Back to home</a>
|
|
134
|
+
</body>
|
|
135
|
+
</html>`;
|
|
136
|
+
res.writeHead(code, { "Content-Type": "text/html; charset=utf-8" });
|
|
137
|
+
res.end(html);
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,gBAAgB,EAAe,MAAM,SAA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C;IAEH,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,WAAW,EAAE,CAAC,CAAC;IACpD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACnB,CAAC;AAED,SAAS,SAAS,CAAC,GAAmB,EAAE,IAAY,EAAE,OAAe;IACnE,MAAM,IAAI,GAAG;;eAEA,IAAI,MAAM,OAAO;;QAExB,IAAI;OACL,OAAO;;;QAGN,CAAC;IAEP,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,cAAc,EAAE,0BAA0B,EAAE,CAAC,CAAC;IACpE,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG1D,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,YAAiB,GACzB,OAAO,CAAC,cAAc,CAAC,CAiDzB;AAED,wBAAgB,iBAAiB,IAAI,MAAM,GAAG,SAAS,CAYtD"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { promises as fs } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import { route } from "./router.js";
|
|
6
|
+
export async function createServer(rootDir, options = {}) {
|
|
7
|
+
// Resolve rootDir to absolute path
|
|
8
|
+
const absoluteRoot = path.resolve(rootDir);
|
|
9
|
+
// Check if directory exists
|
|
10
|
+
try {
|
|
11
|
+
const stat = await fs.stat(absoluteRoot);
|
|
12
|
+
if (!stat.isDirectory()) {
|
|
13
|
+
throw new Error(`Path is not a directory: ${absoluteRoot}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
throw new Error(`Directory does not exist: ${absoluteRoot}`);
|
|
18
|
+
}
|
|
19
|
+
const port = options.port ?? 3000;
|
|
20
|
+
const host = options.host ?? "localhost";
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const server = http.createServer((req, res) => {
|
|
23
|
+
route(req, res, absoluteRoot).catch((err) => {
|
|
24
|
+
console.error("Routing error:", err);
|
|
25
|
+
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
26
|
+
res.end("Internal Server Error");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
server.listen(port, host, () => {
|
|
30
|
+
const url = `http://${host}:${port}`;
|
|
31
|
+
const instance = {
|
|
32
|
+
port,
|
|
33
|
+
host,
|
|
34
|
+
url,
|
|
35
|
+
close: () => new Promise((res, rej) => {
|
|
36
|
+
server.close((err) => {
|
|
37
|
+
if (err)
|
|
38
|
+
rej(err);
|
|
39
|
+
else
|
|
40
|
+
res();
|
|
41
|
+
});
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
resolve(instance);
|
|
45
|
+
});
|
|
46
|
+
server.on("error", (err) => {
|
|
47
|
+
reject(err);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function getNetworkAddress() {
|
|
52
|
+
const interfaces = os.networkInterfaces();
|
|
53
|
+
for (const name of Object.keys(interfaces)) {
|
|
54
|
+
const iface = interfaces[name];
|
|
55
|
+
if (!iface)
|
|
56
|
+
continue;
|
|
57
|
+
for (const info of iface) {
|
|
58
|
+
if (info.family === "IPv4" && !info.internal) {
|
|
59
|
+
return info.address;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OA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QACH,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../src/template.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,MAAM,EAAE;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB,GAAG,MAAM,CAiLT"}
|
package/dist/template.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
export function wrapHtml(params) {
|
|
2
|
+
const { title, body, filePath } = params;
|
|
3
|
+
return `<!DOCTYPE html>
|
|
4
|
+
<html lang="en">
|
|
5
|
+
<head>
|
|
6
|
+
<meta charset="UTF-8">
|
|
7
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
8
|
+
<title>${escapeHtml(title)}</title>
|
|
9
|
+
<style>
|
|
10
|
+
* { box-sizing: border-box; }
|
|
11
|
+
body {
|
|
12
|
+
margin: 0;
|
|
13
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
|
|
14
|
+
font-size: 16px;
|
|
15
|
+
line-height: 1.6;
|
|
16
|
+
color: #24292f;
|
|
17
|
+
background: #f6f8fa;
|
|
18
|
+
}
|
|
19
|
+
.header {
|
|
20
|
+
background: #0d1117;
|
|
21
|
+
padding: 16px 24px;
|
|
22
|
+
border-bottom: 1px solid #30363d;
|
|
23
|
+
display: flex;
|
|
24
|
+
align-items: center;
|
|
25
|
+
justify-content: space-between;
|
|
26
|
+
}
|
|
27
|
+
.header-left {
|
|
28
|
+
display: flex;
|
|
29
|
+
align-items: center;
|
|
30
|
+
gap: 12px;
|
|
31
|
+
}
|
|
32
|
+
.header-logo {
|
|
33
|
+
color: #f0f6fc;
|
|
34
|
+
font-weight: 600;
|
|
35
|
+
font-size: 14px;
|
|
36
|
+
text-decoration: none;
|
|
37
|
+
}
|
|
38
|
+
.header-filename {
|
|
39
|
+
color: #f0f6fc;
|
|
40
|
+
font-size: 14px;
|
|
41
|
+
font-weight: 500;
|
|
42
|
+
}
|
|
43
|
+
.header-path {
|
|
44
|
+
color: #7d8590;
|
|
45
|
+
font-size: 12px;
|
|
46
|
+
}
|
|
47
|
+
.container {
|
|
48
|
+
max-width: 860px;
|
|
49
|
+
margin: 32px auto;
|
|
50
|
+
padding: 32px;
|
|
51
|
+
background: #ffffff;
|
|
52
|
+
border: 1px solid #d0d7de;
|
|
53
|
+
border-radius: 6px;
|
|
54
|
+
}
|
|
55
|
+
.markdown-body {
|
|
56
|
+
color: #24292f;
|
|
57
|
+
}
|
|
58
|
+
.markdown-body h1, .markdown-body h2, .markdown-body h3,
|
|
59
|
+
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
|
|
60
|
+
margin-top: 24px;
|
|
61
|
+
margin-bottom: 16px;
|
|
62
|
+
font-weight: 600;
|
|
63
|
+
line-height: 1.25;
|
|
64
|
+
color: #1f2328;
|
|
65
|
+
}
|
|
66
|
+
.markdown-body h1 { font-size: 2em; border-bottom: 1px solid #d8dee4; padding-bottom: 0.3em; }
|
|
67
|
+
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid #d8dee4; padding-bottom: 0.3em; }
|
|
68
|
+
.markdown-body h3 { font-size: 1.25em; }
|
|
69
|
+
.markdown-body h4 { font-size: 1em; }
|
|
70
|
+
.markdown-body h5 { font-size: 0.875em; }
|
|
71
|
+
.markdown-body h6 { font-size: 0.85em; color: #656d76; }
|
|
72
|
+
.markdown-body p { margin-top: 0; margin-bottom: 16px; }
|
|
73
|
+
.markdown-body a { color: #0969da; text-decoration: none; }
|
|
74
|
+
.markdown-body a:hover { text-decoration: underline; }
|
|
75
|
+
.markdown-body ul, .markdown-body ol {
|
|
76
|
+
margin-top: 0;
|
|
77
|
+
margin-bottom: 16px;
|
|
78
|
+
padding-left: 2em;
|
|
79
|
+
}
|
|
80
|
+
.markdown-body ul { list-style-type: disc; }
|
|
81
|
+
.markdown-body ol { list-style-type: decimal; }
|
|
82
|
+
.markdown-body li + li { margin-top: 0.25em; }
|
|
83
|
+
.markdown-body code {
|
|
84
|
+
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
|
|
85
|
+
font-size: 85%;
|
|
86
|
+
padding: 0.2em 0.4em;
|
|
87
|
+
background: rgba(175, 184, 193, 0.2);
|
|
88
|
+
border-radius: 6px;
|
|
89
|
+
}
|
|
90
|
+
.markdown-body pre {
|
|
91
|
+
background: #161b22;
|
|
92
|
+
border-radius: 6px;
|
|
93
|
+
padding: 16px;
|
|
94
|
+
overflow: auto;
|
|
95
|
+
font-size: 85%;
|
|
96
|
+
line-height: 1.45;
|
|
97
|
+
margin-bottom: 16px;
|
|
98
|
+
}
|
|
99
|
+
.markdown-body pre code {
|
|
100
|
+
background: transparent;
|
|
101
|
+
padding: 0;
|
|
102
|
+
border-radius: 0;
|
|
103
|
+
color: #c9d1d9;
|
|
104
|
+
}
|
|
105
|
+
.markdown-body blockquote {
|
|
106
|
+
margin: 0 0 16px;
|
|
107
|
+
padding: 0 1em;
|
|
108
|
+
color: #656d76;
|
|
109
|
+
border-left: 0.25em solid #d0d7de;
|
|
110
|
+
}
|
|
111
|
+
.markdown-body table {
|
|
112
|
+
border-collapse: collapse;
|
|
113
|
+
width: 100%;
|
|
114
|
+
margin-bottom: 16px;
|
|
115
|
+
}
|
|
116
|
+
.markdown-body th, .markdown-body td {
|
|
117
|
+
padding: 6px 13px;
|
|
118
|
+
border: 1px solid #d0d7de;
|
|
119
|
+
}
|
|
120
|
+
.markdown-body th { background: #f6f8fa; font-weight: 600; }
|
|
121
|
+
.markdown-body tr:nth-child(2n) { background: #f6f8fa; }
|
|
122
|
+
.markdown-body img { max-width: 100%; height: auto; }
|
|
123
|
+
.markdown-body hr {
|
|
124
|
+
height: 0.25em;
|
|
125
|
+
padding: 0;
|
|
126
|
+
margin: 24px 0;
|
|
127
|
+
background: #d0d7de;
|
|
128
|
+
border: 0;
|
|
129
|
+
}
|
|
130
|
+
.anchor { float: left; margin-left: -20px; padding-right: 4px; line-height: 1; }
|
|
131
|
+
.anchor:focus { outline: none; }
|
|
132
|
+
.anchor-link { color: #656d76; text-decoration: none; visibility: hidden; }
|
|
133
|
+
.markdown-body h1:hover .anchor-link,
|
|
134
|
+
.markdown-body h2:hover .anchor-link,
|
|
135
|
+
.markdown-body h3:hover .anchor-link,
|
|
136
|
+
.markdown-body h4:hover .anchor-link,
|
|
137
|
+
.markdown-body h5:hover .anchor-link,
|
|
138
|
+
.markdown-body h6:hover .anchor-link { visibility: visible; }
|
|
139
|
+
@media (max-width: 900px) {
|
|
140
|
+
.container { margin: 16px; padding: 20px; }
|
|
141
|
+
.header { padding: 12px 16px; }
|
|
142
|
+
}
|
|
143
|
+
@media (max-width: 480px) {
|
|
144
|
+
.container { margin: 8px; padding: 16px; }
|
|
145
|
+
}
|
|
146
|
+
/* Syntax highlighting colors for dark code blocks */
|
|
147
|
+
.hljs-keyword { color: #ff7b72; }
|
|
148
|
+
.hljs-string { color: #a5d6ff; }
|
|
149
|
+
.hljs-number { color: #79c0ff; }
|
|
150
|
+
.hljs-comment { color: #8b949e; font-style: italic; }
|
|
151
|
+
.hljs-function { color: #d2a8ff; }
|
|
152
|
+
.hljs-class { color: #ffa657; }
|
|
153
|
+
.hljs-variable { color: #ffa657; }
|
|
154
|
+
.hljs-operator { color: #ff7b72; }
|
|
155
|
+
.hljs-punctuation { color: #c9d1d9; }
|
|
156
|
+
.hljs-property { color: #79c0ff; }
|
|
157
|
+
.hljs-tag { color: #7ee787; }
|
|
158
|
+
.hljs-attr { color: #79c0ff; }
|
|
159
|
+
</style>
|
|
160
|
+
</head>
|
|
161
|
+
<body>
|
|
162
|
+
<header class="header">
|
|
163
|
+
<div class="header-left">
|
|
164
|
+
<a href="/" class="header-logo">mdsvr</a>
|
|
165
|
+
<span class="header-path">/</span>
|
|
166
|
+
<span class="header-filename">${escapeHtml(title)}</span>
|
|
167
|
+
</div>
|
|
168
|
+
<span class="header-path">${escapeHtml(filePath)}</span>
|
|
169
|
+
</header>
|
|
170
|
+
<div class="container">
|
|
171
|
+
<div class="markdown-body">
|
|
172
|
+
${body}
|
|
173
|
+
</div>
|
|
174
|
+
</div>
|
|
175
|
+
</body>
|
|
176
|
+
</html>`;
|
|
177
|
+
}
|
|
178
|
+
function escapeHtml(text) {
|
|
179
|
+
const htmlEscapes = {
|
|
180
|
+
"&": "&",
|
|
181
|
+
"<": "<",
|
|
182
|
+
">": ">",
|
|
183
|
+
'"': """,
|
|
184
|
+
"'": "'",
|
|
185
|
+
};
|
|
186
|
+
return text.replace(/[&<>"']/g, (char) => htmlEscapes[char] || char);
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=template.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template.js","sourceRoot":"","sources":["../src/template.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,MAIxB;IACC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;IAEzC,OAAO;;;;;WAKE,UAAU,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sCA8JU,UAAU,CAAC,KAAK,CAAC;;gCAEvB,UAAU,CAAC,QAAQ,CAAC;;;;EAIlD,IAAI;;;;QAIE,CAAC;AACT,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC9B,MAAM,WAAW,GAA2B;QAC1C,GAAG,EAAE,OAAO;QACZ,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,QAAQ;QACb,GAAG,EAAE,QAAQ;KACd,CAAC;IACF,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;AACvE,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ServeOptions {
|
|
2
|
+
port?: number;
|
|
3
|
+
host?: string;
|
|
4
|
+
open?: boolean;
|
|
5
|
+
silent?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface ServerInstance {
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
port: number;
|
|
10
|
+
host: string;
|
|
11
|
+
url: string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mdsvr",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Static file server with auto Markdown → HTML rendering",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"mdsvr": "./bin/mdsvr.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"build:test": "tsc -p tsconfig.test.json",
|
|
20
|
+
"dev": "tsc --watch",
|
|
21
|
+
"test": "npm run build && npm run build:test && node --test dist-test/*.test.js",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"markdown-it": "^14.1.0",
|
|
26
|
+
"markdown-it-anchor": "^9.2.0",
|
|
27
|
+
"highlight.js": "^11.10.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/markdown-it": "^14.1.0",
|
|
31
|
+
"@types/node": "^22.0.0",
|
|
32
|
+
"typescript": "^5.5.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18.0.0"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"markdown",
|
|
39
|
+
"serve",
|
|
40
|
+
"static",
|
|
41
|
+
"server",
|
|
42
|
+
"docs",
|
|
43
|
+
"cli"
|
|
44
|
+
],
|
|
45
|
+
"files": [
|
|
46
|
+
"dist/**/*",
|
|
47
|
+
"bin/**/*",
|
|
48
|
+
"README.md"
|
|
49
|
+
],
|
|
50
|
+
"author": "Truong Ngoc Vuong <vuongtr56@gmail.com>",
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "https://github.com/satoshiman/mdsvr.git"
|
|
55
|
+
}
|
|
56
|
+
}
|