@noy4/docserve 0.3.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 CHANGED
@@ -4,7 +4,7 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title>__DOCS_DIR_NAME__</title>
7
- <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg%20width='64'%20height='64'%20viewBox='244.5%20242.5%20546%20546'%20xmlns='http://www.w3.org/2000/svg'%3E%3Cdefs%3E%3ClinearGradient%20id='frontFill'%20x1='0%25'%20y1='0%25'%20x2='0%25'%20y2='100%25'%3E%3Cstop%20offset='0%25'%20stop-color='%23223047'/%3E%3Cstop%20offset='55%25'%20stop-color='%23141C2B'/%3E%3Cstop%20offset='100%25'%20stop-color='%230F1520'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cg%20transform='rotate(-6%20462%20452)'%3E%3Crect%20x='262'%20y='312'%20width='400'%20height='280'%20rx='34'%20fill='%234C5FD6'%20stroke='%23818CF8'%20stroke-width='8'/%3E%3C/g%3E%3Cg%20transform='rotate(5%20575%20440)'%3E%3Crect%20x='375'%20y='300'%20width='400'%20height='280'%20rx='34'%20fill='%233E76C9'%20stroke='%235B8DEF'%20stroke-width='8'/%3E%3C/g%3E%3Cg%20transform='rotate(6%20562%20588)'%3E%3Crect%20x='362'%20y='448'%20width='400'%20height='280'%20rx='34'%20fill='%232E93C4'%20stroke='%2338BDF8'%20stroke-width='8'/%3E%3C/g%3E%3Cg%20transform='rotate(-6%20512%20536)'%3E%3Crect%20x='280'%20y='375'%20width='464'%20height='322'%20rx='38'%20fill='url(%23frontFill)'%20stroke='%2338BDF8'%20stroke-width='8'/%3E%3Cg%20stroke='%23EAF8FF'%20stroke-width='10'%20stroke-linecap='round'%20stroke-opacity='0.62'%3E%3Cline%20x1='334'%20y1='578'%20x2='690'%20y2='578'/%3E%3Cline%20x1='334'%20y1='628'%20x2='548'%20y2='628'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E">
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(--accent); flex-shrink: 0; }
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,7 +167,7 @@
166
167
 
167
168
  <body>
168
169
  <h1>
169
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 7a2 2 0 0 1 2-2h5l2 2h7a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>
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>
170
171
  __DOCS_DIR_NAME__
171
172
  </h1>
172
173
  <p class="sub">
@@ -281,7 +282,7 @@
281
282
  el("button", {
282
283
  type: "button",
283
284
  class: "copy-btn",
284
- title: file.path,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noy4/docserve",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Serves a folder of HTML as a card gallery with live reload.",
5
5
  "type": "module",
6
6
  "bin": {
package/server/files.mjs CHANGED
@@ -63,7 +63,7 @@ export async function apiFiles(docsDir) {
63
63
  files.push({
64
64
  title: title ?? basename(full),
65
65
  url: `/${rel}`,
66
- path: full,
66
+ filePath: full,
67
67
  favicon: favicon ?? undefined,
68
68
  created: date ?? created.get(key) ?? Math.floor(st.birthtimeMs / 1000),
69
69
  modified: modified.get(key) ?? Math.floor(st.mtimeMs / 1000),
package/server/index.mjs CHANGED
@@ -6,7 +6,7 @@
6
6
  // │ │ ├─ /api/files ──▶ apiFiles() │
7
7
  // │ │ ├─ /, /index.html ──▶ gallery template │
8
8
  // │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
9
- // │ ├─ WebSocketServer("/__reload") ◀─ UpdateListener │
9
+ // │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
10
10
  // │ └─ listen ──▶ listener.start() + writeState() │
11
11
  // └────────────────────────────────────────────────────────┘
12
12
  import { createServer } from "node:http"
@@ -18,7 +18,7 @@ import { clearState, writeState } from "./state.mjs"
18
18
  import { apiFiles, isWithin, normalizedRelative } from "./files.mjs"
19
19
  import { injectIntoHtml, WS_PATH } from "./inject.mjs"
20
20
  import { WebSocketServer } from "./websocket.mjs"
21
- import { UpdateListener } from "./watch.mjs"
21
+ import { FileWatcher } from "./watch.mjs"
22
22
 
23
23
  const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
24
24
  const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "index.html")
@@ -49,14 +49,14 @@ const MIME = {
49
49
 
50
50
  export async function runServer({ docsDir, port: initialPort, open = false }) {
51
51
  const wss = new WebSocketServer({ path: WS_PATH })
52
- const updateListener = new UpdateListener({ wss, docsDir, templatePath: INDEX_TEMPLATE })
52
+ const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
53
53
  let port = initialPort
54
54
 
55
55
  const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
56
56
  wss.attach(server)
57
57
 
58
58
  const cleanup = () => {
59
- updateListener.close()
59
+ fileWatcher.close()
60
60
  wss.close()
61
61
  clearState()
62
62
  server.closeAllConnections()
@@ -75,13 +75,13 @@ export async function runServer({ docsDir, port: initialPort, open = false }) {
75
75
  }
76
76
  })
77
77
  server.listen(port, "127.0.0.1", async () => {
78
- await updateListener.start()
78
+ await fileWatcher.start()
79
79
  const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
80
80
  writeState(state)
81
81
  console.log(state.url)
82
82
  if (open && process.platform === "darwin") exec(`open ${state.url}`)
83
83
  })
84
- return { server, wss, updateListener, close: cleanup }
84
+ return { server, wss, fileWatcher, close: cleanup }
85
85
  }
86
86
 
87
87
  // --- Node server adapter ---
@@ -164,6 +164,7 @@ async function galleryResponse(docsDir) {
164
164
  html = html
165
165
  .replaceAll("__DOCS_DIR_NAME__", escapeHtml(basename(docsDir) || docsDir))
166
166
  .replaceAll("__DOCS_DIR__", escapeHtml(displayPath(docsDir)))
167
+ .replaceAll("__FAVICON_HUE__", String(faviconHue(docsDir)))
167
168
  return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
168
169
  } catch {
169
170
  return new Response("Gallery template missing", { status: 500 })
@@ -176,6 +177,13 @@ function displayPath(p) {
176
177
  return p.startsWith(`${home}/`) ? `~${p.slice(home.length)}` : p
177
178
  }
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
+
179
187
  function escapeHtml(s) {
180
188
  return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
181
189
  }
package/server/watch.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // UpdateListener: watch docsDir recursively for *.html changes and broadcast
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.
@@ -12,7 +12,7 @@ import { isWithin, listHtmlFiles, normalizedRelative, shouldIgnore } from "./fil
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 UpdateListener {
15
+ export class FileWatcher {
16
16
  constructor(options = {}) {
17
17
  this.wss = options.wss
18
18
  this.docsDir = options.docsDir