@noy4/docserve 0.2.0 → 0.3.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/index.html +9 -8
- package/package.json +1 -1
- package/server/files.mjs +33 -17
- package/server/index.mjs +33 -12
- package/server/inject.html +88 -0
- package/server/inject.mjs +14 -86
- package/server/watch.mjs +4 -3
- package/server/websocket.mjs +9 -0
package/index.html
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>
|
|
7
|
-
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='
|
|
6
|
+
<title>__DOCS_DIR_NAME__</title>
|
|
7
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='hsl(__FAVICON_HUE__ 70%25 52%)'%3E%3Cpath d='M6.1 10L4 18V8h17a2 2 0 0 0-2-2h-7l-2-2H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h15c.9 0 1.7-.6 1.9-1.5l2.3-8.5zM19 18H6l1.6-6h13z'/%3E%3C/svg%3E">
|
|
8
8
|
<style>
|
|
9
9
|
:root {
|
|
10
10
|
--bg: #0f1117;
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
--accent: #6c8cff;
|
|
16
16
|
--green: #4ade80;
|
|
17
17
|
--btn-bg: rgba(15, 17, 23, 0.82);
|
|
18
|
+
--folder-hue: __FAVICON_HUE__;
|
|
18
19
|
}
|
|
19
20
|
@media (prefers-color-scheme: light) {
|
|
20
21
|
:root {
|
|
@@ -45,11 +46,11 @@
|
|
|
45
46
|
font-size: 1.6rem;
|
|
46
47
|
margin-bottom: 8px;
|
|
47
48
|
}
|
|
48
|
-
h1 svg { width: 26px; height: 26px; color: var(--
|
|
49
|
+
h1 svg { width: 26px; height: 26px; color: hsl(var(--folder-hue) 70% 52%); flex-shrink: 0; }
|
|
49
50
|
.sub { color: var(--muted); margin-bottom: 32px; font-size: 0.9rem; }
|
|
50
51
|
.sub b { color: var(--green); }
|
|
51
52
|
|
|
52
|
-
.sub code {
|
|
53
|
+
.sub code, .empty code {
|
|
53
54
|
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
54
55
|
font-size: 0.78rem;
|
|
55
56
|
background: var(--card);
|
|
@@ -166,11 +167,11 @@
|
|
|
166
167
|
|
|
167
168
|
<body>
|
|
168
169
|
<h1>
|
|
169
|
-
<svg viewBox="0 0 24 24" fill="
|
|
170
|
-
|
|
170
|
+
<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M6.1 10L4 18V8h17a2 2 0 0 0-2-2h-7l-2-2H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h15c.9 0 1.7-.6 1.9-1.5l2.3-8.5zM19 18H6l1.6-6h13z"/></svg>
|
|
171
|
+
__DOCS_DIR_NAME__
|
|
171
172
|
</h1>
|
|
172
173
|
<p class="sub">
|
|
173
|
-
<code id="docs-dir">
|
|
174
|
+
<code id="docs-dir">__DOCS_DIR__</code> — <b id="total">0</b> files
|
|
174
175
|
</p>
|
|
175
176
|
|
|
176
177
|
<div class="grid" id="grid" hidden></div>
|
|
@@ -281,7 +282,7 @@
|
|
|
281
282
|
el("button", {
|
|
282
283
|
type: "button",
|
|
283
284
|
class: "copy-btn",
|
|
284
|
-
title: file.
|
|
285
|
+
title: file.filePath,
|
|
285
286
|
"aria-label": "Copy file path",
|
|
286
287
|
html: COPY_SVG + CHECK_SVG,
|
|
287
288
|
onclick: async (e) => {
|
package/package.json
CHANGED
package/server/files.mjs
CHANGED
|
@@ -11,24 +11,33 @@ import { promisify } from "node:util"
|
|
|
11
11
|
|
|
12
12
|
const run = promisify(execFile)
|
|
13
13
|
|
|
14
|
-
// Walk docsDir recursively and collect .html files;
|
|
15
|
-
// from the gallery.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
// Walk docsDir recursively and collect .html files; only the root index.html is
|
|
15
|
+
// excluded from the gallery.
|
|
16
|
+
const MAX_DEPTH = 3
|
|
17
|
+
|
|
18
|
+
export async function listHtmlFiles(docsDir) {
|
|
19
|
+
const out = []
|
|
20
|
+
await walk(docsDir, docsDir, 0)
|
|
21
|
+
return out
|
|
22
|
+
|
|
23
|
+
async function walk(dir, root, depth) {
|
|
24
|
+
if (depth > MAX_DEPTH) return
|
|
25
|
+
let entries
|
|
26
|
+
try {
|
|
27
|
+
entries = await readdir(dir, { withFileTypes: true })
|
|
28
|
+
} catch {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const full = join(dir, entry.name)
|
|
33
|
+
if (shouldIgnore(entry.name)) continue
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
await walk(full, root, depth + 1)
|
|
36
|
+
} else if (entry.name.endsWith(".html") && !(dir === root && entry.name === "index.html")) {
|
|
37
|
+
out.push(full)
|
|
38
|
+
}
|
|
29
39
|
}
|
|
30
40
|
}
|
|
31
|
-
return out
|
|
32
41
|
}
|
|
33
42
|
|
|
34
43
|
// /api/files payload, sorted by created desc with title/path tiebreaks.
|
|
@@ -54,7 +63,7 @@ export async function apiFiles(docsDir) {
|
|
|
54
63
|
files.push({
|
|
55
64
|
title: title ?? basename(full),
|
|
56
65
|
url: `/${rel}`,
|
|
57
|
-
|
|
66
|
+
filePath: full,
|
|
58
67
|
favicon: favicon ?? undefined,
|
|
59
68
|
created: date ?? created.get(key) ?? Math.floor(st.birthtimeMs / 1000),
|
|
60
69
|
modified: modified.get(key) ?? Math.floor(st.mtimeMs / 1000),
|
|
@@ -215,3 +224,10 @@ export function isWithin(base, path) {
|
|
|
215
224
|
export function normalizedRelative(base, path) {
|
|
216
225
|
return relative(base, path).split(sep).join("/")
|
|
217
226
|
}
|
|
227
|
+
|
|
228
|
+
export function shouldIgnore(relPath) {
|
|
229
|
+
return relPath
|
|
230
|
+
.split(/[/\\]/)
|
|
231
|
+
.filter(Boolean)
|
|
232
|
+
.some((part) => part.startsWith(".") || part === "node_modules")
|
|
233
|
+
}
|
package/server/index.mjs
CHANGED
|
@@ -6,18 +6,19 @@
|
|
|
6
6
|
// │ │ ├─ /api/files ──▶ apiFiles() │
|
|
7
7
|
// │ │ ├─ /, /index.html ──▶ gallery template │
|
|
8
8
|
// │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
|
|
9
|
-
// │ ├─ WebSocketServer("/__reload") ◀─
|
|
9
|
+
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
10
10
|
// │ └─ listen ──▶ listener.start() + writeState() │
|
|
11
11
|
// └────────────────────────────────────────────────────────┘
|
|
12
12
|
import { createServer } from "node:http"
|
|
13
13
|
import { readFile } from "node:fs/promises"
|
|
14
|
-
import
|
|
14
|
+
import os from "node:os"
|
|
15
|
+
import { basename, extname, resolve } from "node:path"
|
|
15
16
|
import { exec } from "node:child_process"
|
|
16
17
|
import { clearState, writeState } from "./state.mjs"
|
|
17
18
|
import { apiFiles, isWithin, normalizedRelative } from "./files.mjs"
|
|
18
19
|
import { injectIntoHtml, WS_PATH } from "./inject.mjs"
|
|
19
20
|
import { WebSocketServer } from "./websocket.mjs"
|
|
20
|
-
import {
|
|
21
|
+
import { FileWatcher } from "./watch.mjs"
|
|
21
22
|
|
|
22
23
|
const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
|
|
23
24
|
const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "index.html")
|
|
@@ -48,18 +49,22 @@ const MIME = {
|
|
|
48
49
|
|
|
49
50
|
export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
50
51
|
const wss = new WebSocketServer({ path: WS_PATH })
|
|
51
|
-
const
|
|
52
|
-
let port = initialPort
|
|
52
|
+
const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
|
|
53
|
+
let port = initialPort
|
|
54
|
+
|
|
55
|
+
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
56
|
+
wss.attach(server)
|
|
53
57
|
|
|
54
58
|
const cleanup = () => {
|
|
55
|
-
|
|
59
|
+
fileWatcher.close()
|
|
60
|
+
wss.close()
|
|
56
61
|
clearState()
|
|
62
|
+
server.closeAllConnections()
|
|
63
|
+
if (server.listening) server.close()
|
|
57
64
|
}
|
|
58
65
|
process.on("SIGINT", () => { cleanup(); process.exit(0) })
|
|
59
66
|
process.on("SIGTERM", () => { cleanup(); process.exit(0) })
|
|
60
67
|
|
|
61
|
-
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
62
|
-
wss.attach(server)
|
|
63
68
|
server.on("error", (err) => {
|
|
64
69
|
if (err.code === "EADDRINUSE" && port < initialPort + MAX_PORT_TRIES) {
|
|
65
70
|
port += 1
|
|
@@ -70,13 +75,13 @@ export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
|
70
75
|
}
|
|
71
76
|
})
|
|
72
77
|
server.listen(port, "127.0.0.1", async () => {
|
|
73
|
-
await
|
|
78
|
+
await fileWatcher.start()
|
|
74
79
|
const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
|
|
75
80
|
writeState(state)
|
|
76
81
|
console.log(state.url)
|
|
77
82
|
if (open && process.platform === "darwin") exec(`open ${state.url}`)
|
|
78
83
|
})
|
|
79
|
-
return { server, wss,
|
|
84
|
+
return { server, wss, fileWatcher, close: cleanup }
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
// --- Node server adapter ---
|
|
@@ -152,17 +157,33 @@ function createHandler({ docsDir }) {
|
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
// The gallery is an embedded template served at / and /index.html; docsDir's own
|
|
155
|
-
// index.html is shadowed by it.
|
|
160
|
+
// index.html is shadowed by it. Directory markers are substituted for display.
|
|
156
161
|
async function galleryResponse(docsDir) {
|
|
157
162
|
try {
|
|
158
163
|
let html = await readFile(INDEX_TEMPLATE, "utf8")
|
|
159
|
-
html = html
|
|
164
|
+
html = html
|
|
165
|
+
.replaceAll("__DOCS_DIR_NAME__", escapeHtml(basename(docsDir) || docsDir))
|
|
166
|
+
.replaceAll("__DOCS_DIR__", escapeHtml(displayPath(docsDir)))
|
|
167
|
+
.replaceAll("__FAVICON_HUE__", String(faviconHue(docsDir)))
|
|
160
168
|
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
|
|
161
169
|
} catch {
|
|
162
170
|
return new Response("Gallery template missing", { status: 500 })
|
|
163
171
|
}
|
|
164
172
|
}
|
|
165
173
|
|
|
174
|
+
// Collapse the home directory prefix to ~ for display.
|
|
175
|
+
function displayPath(p) {
|
|
176
|
+
const home = os.homedir()
|
|
177
|
+
return p.startsWith(`${home}/`) ? `~${p.slice(home.length)}` : p
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Deterministic hue for the folder favicon: the same path always yields the same color.
|
|
181
|
+
function faviconHue(s) {
|
|
182
|
+
let h = 0
|
|
183
|
+
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0
|
|
184
|
+
return ((h % 360) + 360) % 360
|
|
185
|
+
}
|
|
186
|
+
|
|
166
187
|
function escapeHtml(s) {
|
|
167
188
|
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
168
189
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
(() => {
|
|
3
|
+
// iframes: no reload loop from dropped sockets, no copy button
|
|
4
|
+
if (window.self !== window.top) return
|
|
5
|
+
|
|
6
|
+
const pageId = __DOCSERVE_PAGE_ID__
|
|
7
|
+
const absPath = __DOCSERVE_ABS_PATH__
|
|
8
|
+
const wsPath = __DOCSERVE_WS_PATH__
|
|
9
|
+
|
|
10
|
+
const proto = location.protocol === "https:" ? "wss:" : "ws:"
|
|
11
|
+
const ws = new WebSocket(proto + "//" + location.host + wsPath)
|
|
12
|
+
ws.addEventListener("message", (e) => {
|
|
13
|
+
let change
|
|
14
|
+
try { change = JSON.parse(e.data) } catch { return }
|
|
15
|
+
if (change?.type === "change" && Array.isArray(change.pages) && change.pages.includes(pageId)) {
|
|
16
|
+
location.reload()
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
ws.addEventListener("close", () => setTimeout(() => location.reload(), 1000))
|
|
20
|
+
|
|
21
|
+
const style = document.createElement("style")
|
|
22
|
+
style.textContent = `#docserve-copy {
|
|
23
|
+
position: fixed;
|
|
24
|
+
top: 12px;
|
|
25
|
+
right: 12px;
|
|
26
|
+
z-index: 2147483647;
|
|
27
|
+
display: inline-flex;
|
|
28
|
+
align-items: center;
|
|
29
|
+
gap: 6px;
|
|
30
|
+
height: 28px;
|
|
31
|
+
padding: 0 10px 0 9px;
|
|
32
|
+
border: 1px solid rgba(127, 127, 138, 0.35);
|
|
33
|
+
border-radius: 8px;
|
|
34
|
+
background: rgba(127, 127, 138, 0.14);
|
|
35
|
+
color: inherit;
|
|
36
|
+
cursor: pointer;
|
|
37
|
+
opacity: 0.55;
|
|
38
|
+
transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
|
|
39
|
+
}
|
|
40
|
+
#docserve-copy svg {
|
|
41
|
+
width: 14px;
|
|
42
|
+
height: 14px;
|
|
43
|
+
pointer-events: none;
|
|
44
|
+
}
|
|
45
|
+
#docserve-copy .label {
|
|
46
|
+
font: 500 11.5px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", sans-serif;
|
|
47
|
+
letter-spacing: 0.01em;
|
|
48
|
+
white-space: nowrap;
|
|
49
|
+
}
|
|
50
|
+
#docserve-copy:hover {
|
|
51
|
+
opacity: 1;
|
|
52
|
+
border-color: #6c8cff;
|
|
53
|
+
color: #6c8cff;
|
|
54
|
+
}
|
|
55
|
+
#docserve-copy.copied {
|
|
56
|
+
opacity: 1;
|
|
57
|
+
color: #4ade80;
|
|
58
|
+
border-color: #4ade80;
|
|
59
|
+
}
|
|
60
|
+
@media (prefers-color-scheme: light) {
|
|
61
|
+
#docserve-copy:hover { border-color: #2563eb; color: #2563eb; }
|
|
62
|
+
#docserve-copy.copied { color: #16a34a; border-color: #16a34a; }
|
|
63
|
+
}
|
|
64
|
+
#docserve-copy .ic-check { display: none; }
|
|
65
|
+
#docserve-copy.copied .ic-copy { display: none; }
|
|
66
|
+
#docserve-copy.copied .ic-check { display: block; }`
|
|
67
|
+
const COPY = '<svg class="ic-copy" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>'
|
|
68
|
+
const CHECK = '<svg class="ic-check" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>'
|
|
69
|
+
const btn = document.createElement("button")
|
|
70
|
+
btn.id = "docserve-copy"
|
|
71
|
+
btn.type = "button"
|
|
72
|
+
btn.title = absPath
|
|
73
|
+
btn.innerHTML = COPY + CHECK + '<span class="label">Copy file path</span>'
|
|
74
|
+
btn.addEventListener("click", async () => {
|
|
75
|
+
try {
|
|
76
|
+
await navigator.clipboard.writeText(absPath)
|
|
77
|
+
btn.classList.add("copied")
|
|
78
|
+
btn.querySelector(".label").textContent = "Copied"
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
btn.classList.remove("copied")
|
|
81
|
+
btn.querySelector(".label").textContent = "Copy file path"
|
|
82
|
+
}, 1200)
|
|
83
|
+
} catch {}
|
|
84
|
+
})
|
|
85
|
+
document.head.appendChild(style)
|
|
86
|
+
document.body.appendChild(btn)
|
|
87
|
+
})()
|
|
88
|
+
</script>
|
package/server/inject.mjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
// Injection: every text/html response served from docsDir gets one client script
|
|
2
2
|
// before the last </body> (appended at the end if the tag is missing). The script
|
|
3
3
|
// exits immediately inside iframes, so previews get neither part; the gallery
|
|
4
|
-
// template ships its own client.
|
|
4
|
+
// template ships its own client. The client template is loaded once at startup.
|
|
5
|
+
import { readFileSync } from "node:fs"
|
|
5
6
|
|
|
6
7
|
export const WS_PATH = "/__reload"
|
|
7
8
|
|
|
9
|
+
const clientTemplate = readFileSync(new URL("./inject.html", import.meta.url), "utf8")
|
|
10
|
+
|
|
8
11
|
export function injectIntoHtml(html, pageId, absPath) {
|
|
9
12
|
const idx = html.toLowerCase().lastIndexOf("</body>")
|
|
10
13
|
if (idx === -1) return html + clientJs(pageId, absPath)
|
|
@@ -12,89 +15,14 @@ export function injectIntoHtml(html, pageId, absPath) {
|
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
function clientJs(pageId, absPath) {
|
|
15
|
-
|
|
16
|
-
(()
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (change?.type === "change" && Array.isArray(change.pages) && change.pages.includes(pageId)) {
|
|
26
|
-
location.reload()
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
ws.addEventListener("close", () => setTimeout(() => location.reload(), 1000))
|
|
30
|
-
|
|
31
|
-
const absPath = ${JSON.stringify(absPath).replaceAll("<", "\\u003c")}
|
|
32
|
-
const style = document.createElement("style")
|
|
33
|
-
style.textContent = \`#docserve-copy {
|
|
34
|
-
position: fixed;
|
|
35
|
-
top: 12px;
|
|
36
|
-
right: 12px;
|
|
37
|
-
z-index: 2147483647;
|
|
38
|
-
display: inline-flex;
|
|
39
|
-
align-items: center;
|
|
40
|
-
gap: 6px;
|
|
41
|
-
height: 28px;
|
|
42
|
-
padding: 0 10px 0 9px;
|
|
43
|
-
border: 1px solid rgba(127, 127, 138, 0.35);
|
|
44
|
-
border-radius: 8px;
|
|
45
|
-
background: rgba(127, 127, 138, 0.14);
|
|
46
|
-
color: inherit;
|
|
47
|
-
cursor: pointer;
|
|
48
|
-
opacity: 0.55;
|
|
49
|
-
transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
|
|
50
|
-
}
|
|
51
|
-
#docserve-copy svg {
|
|
52
|
-
width: 14px;
|
|
53
|
-
height: 14px;
|
|
54
|
-
pointer-events: none;
|
|
55
|
-
}
|
|
56
|
-
#docserve-copy .label {
|
|
57
|
-
font: 500 11.5px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", sans-serif;
|
|
58
|
-
letter-spacing: 0.01em;
|
|
59
|
-
white-space: nowrap;
|
|
60
|
-
}
|
|
61
|
-
#docserve-copy:hover {
|
|
62
|
-
opacity: 1;
|
|
63
|
-
border-color: #6c8cff;
|
|
64
|
-
color: #6c8cff;
|
|
65
|
-
}
|
|
66
|
-
#docserve-copy.copied {
|
|
67
|
-
opacity: 1;
|
|
68
|
-
color: #4ade80;
|
|
69
|
-
border-color: #4ade80;
|
|
70
|
-
}
|
|
71
|
-
@media (prefers-color-scheme: light) {
|
|
72
|
-
#docserve-copy:hover { border-color: #2563eb; color: #2563eb; }
|
|
73
|
-
#docserve-copy.copied { color: #16a34a; border-color: #16a34a; }
|
|
74
|
-
}
|
|
75
|
-
#docserve-copy .ic-check { display: none; }
|
|
76
|
-
#docserve-copy.copied .ic-copy { display: none; }
|
|
77
|
-
#docserve-copy.copied .ic-check { display: block; }\`
|
|
78
|
-
const COPY = '<svg class="ic-copy" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>'
|
|
79
|
-
const CHECK = '<svg class="ic-check" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>'
|
|
80
|
-
const btn = document.createElement("button")
|
|
81
|
-
btn.id = "docserve-copy"
|
|
82
|
-
btn.type = "button"
|
|
83
|
-
btn.title = absPath
|
|
84
|
-
btn.innerHTML = COPY + CHECK + '<span class="label">Copy file path</span>'
|
|
85
|
-
btn.addEventListener("click", async () => {
|
|
86
|
-
try {
|
|
87
|
-
await navigator.clipboard.writeText(absPath)
|
|
88
|
-
btn.classList.add("copied")
|
|
89
|
-
btn.querySelector(".label").textContent = "Copied"
|
|
90
|
-
setTimeout(() => {
|
|
91
|
-
btn.classList.remove("copied")
|
|
92
|
-
btn.querySelector(".label").textContent = "Copy file path"
|
|
93
|
-
}, 1200)
|
|
94
|
-
} catch {}
|
|
95
|
-
})
|
|
96
|
-
document.head.appendChild(style)
|
|
97
|
-
document.body.appendChild(btn)
|
|
98
|
-
})()
|
|
99
|
-
</script>`
|
|
18
|
+
const values = {
|
|
19
|
+
PAGE_ID: JSON.stringify(pageId).replaceAll("<", "\\u003c"),
|
|
20
|
+
ABS_PATH: JSON.stringify(absPath).replaceAll("<", "\\u003c"),
|
|
21
|
+
WS_PATH: JSON.stringify(WS_PATH),
|
|
22
|
+
}
|
|
23
|
+
let rendered = clientTemplate
|
|
24
|
+
for (const [key, value] of Object.entries(values)) {
|
|
25
|
+
rendered = rendered.replaceAll(`__DOCSERVE_${key}__`, () => value)
|
|
26
|
+
}
|
|
27
|
+
return rendered
|
|
100
28
|
}
|
package/server/watch.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// FileWatcher: watch docsDir recursively for *.html changes and broadcast
|
|
2
2
|
// debounced batches over the reload socket. The gallery template is watched
|
|
3
3
|
// too; its edits set templateChanged so gallery tabs pick them up even though
|
|
4
4
|
// nothing in docsDir changed.
|
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
// set never flip contentSetChanged.
|
|
8
8
|
import { existsSync, watch } from "node:fs"
|
|
9
9
|
import { basename, dirname, resolve } from "node:path"
|
|
10
|
-
import { isWithin, listHtmlFiles, normalizedRelative } from "./files.mjs"
|
|
10
|
+
import { isWithin, listHtmlFiles, normalizedRelative, shouldIgnore } from "./files.mjs"
|
|
11
11
|
|
|
12
12
|
const DEBOUNCE_MS = 50 // like livePreview.previewDebounceDelay
|
|
13
13
|
const TEMPLATE_TOUCH = "\u0000template" // sentinel: never a real docsDir-relative path
|
|
14
14
|
|
|
15
|
-
export class
|
|
15
|
+
export class FileWatcher {
|
|
16
16
|
constructor(options = {}) {
|
|
17
17
|
this.wss = options.wss
|
|
18
18
|
this.docsDir = options.docsDir
|
|
@@ -41,6 +41,7 @@ export class UpdateListener {
|
|
|
41
41
|
this.watchers.push(
|
|
42
42
|
watch(this.docsDir, { recursive: true }, (event, filename) => {
|
|
43
43
|
if (!filename?.endsWith(".html")) return
|
|
44
|
+
if (shouldIgnore(filename)) return
|
|
44
45
|
const full = resolve(this.docsDir, filename)
|
|
45
46
|
if (!isWithin(this.docsDir, full)) return
|
|
46
47
|
this.queueBroadcast(normalizedRelative(this.docsDir, full))
|
package/server/websocket.mjs
CHANGED