@noy4/docserve 0.3.2 → 0.3.4
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/bin.mjs +2 -2
- package/dist/widget/index.js +11803 -0
- package/package.json +24 -3
- package/src/node/adapter.mjs +67 -0
- package/src/node/assets.mjs +36 -0
- package/src/node/dates.mjs +73 -0
- package/src/node/docs-info.mjs +39 -0
- package/src/node/files.mjs +43 -0
- package/src/node/gallery.mjs +33 -0
- package/src/node/inject.mjs +43 -0
- package/src/node/meta.mjs +137 -0
- package/src/node/routes.mjs +105 -0
- package/src/node/scan.mjs +87 -0
- package/src/node/serve-static.mjs +158 -0
- package/src/node/server.mjs +69 -0
- package/src/node/watch.mjs +119 -0
- package/src/plugins/collapse/client.js +45 -0
- package/src/plugins/collapse/collapse.css +74 -0
- package/src/plugins/collapse/commands.ts +81 -0
- package/src/plugins/collapse/model.ts +123 -0
- package/src/plugins/collapse/sections.ts +105 -0
- package/src/plugins/collapse/view.ts +84 -0
- package/src/plugins/copy/client.js +72 -0
- package/src/plugins/copy/copy.css +21 -0
- package/src/plugins/home/client.js +24 -0
- package/src/plugins/sidebar/client.js +106 -0
- package/src/plugins/sidebar/grouping.js +108 -0
- package/src/plugins/sidebar/icons.js +25 -0
- package/src/plugins/sidebar/model.ts +91 -0
- package/src/plugins/sidebar/resize-model.ts +47 -0
- package/src/plugins/sidebar/sidebar.css +663 -0
- package/src/plugins/sidebar/tree-model.ts +23 -0
- package/src/plugins/sidebar/tree.ts +69 -0
- package/src/plugins/sidebar/ui/PageList.tsx +153 -0
- package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
- package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
- package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
- package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
- package/src/plugins/sidebar/ui/preview-model.ts +134 -0
- package/src/plugins/sidebar/ui/useModel.ts +25 -0
- package/src/plugins/toc/client.js +55 -0
- package/src/plugins/toc/commands.ts +49 -0
- package/src/plugins/toc/heading-html.ts +40 -0
- package/src/plugins/toc/model.ts +178 -0
- package/src/plugins/toc/outline.js +94 -0
- package/src/plugins/toc/scroll-spy.ts +35 -0
- package/src/plugins/toc/toc.css +214 -0
- package/src/plugins/toc/ui/TocPanel.tsx +87 -0
- package/src/shared/types.d.ts +193 -0
- package/src/widget/commands.js +214 -0
- package/src/widget/docks.js +127 -0
- package/src/widget/host.js +187 -0
- package/src/widget/index.js +22 -0
- package/src/widget/lib/disposables.js +44 -0
- package/src/widget/lib/dom.js +106 -0
- package/src/widget/lib/events.js +65 -0
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/lib/persisted-signal.js +40 -0
- package/src/widget/lib/storage.js +70 -0
- package/src/widget/live.js +86 -0
- package/src/widget/pages.js +65 -0
- package/src/widget/palette.css +162 -0
- package/src/widget/palette.js +259 -0
- package/src/widget/placement.js +204 -0
- package/src/widget/plugins.js +39 -0
- package/src/widget/rpc.js +32 -0
- package/src/widget/styles.js +31 -0
- package/src/widget/ui.js +50 -0
- package/src/widget/widget.css +206 -0
- package/server/files.mjs +0 -261
- package/server/index.mjs +0 -198
- package/server/inject.html +0 -88
- package/server/inject.mjs +0 -28
- package/server/watch.mjs +0 -104
- /package/{server → src/node}/state.mjs +0 -0
- /package/{server → src/node}/websocket.mjs +0 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
:host {
|
|
2
|
+
--docserve-inset-t: calc(16px + env(safe-area-inset-top));
|
|
3
|
+
--docserve-inset-r: calc(16px + env(safe-area-inset-right));
|
|
4
|
+
--docserve-inset-b: calc(16px + env(safe-area-inset-bottom));
|
|
5
|
+
--docserve-inset-l: calc(16px + env(safe-area-inset-left));
|
|
6
|
+
--docserve-dock-bg: rgba(255, 255, 255, 0.92);
|
|
7
|
+
--docserve-dock-border: rgba(15, 23, 42, 0.14);
|
|
8
|
+
--docserve-dock-text: #334155;
|
|
9
|
+
--docserve-dock-muted: #64748b;
|
|
10
|
+
--docserve-dock-hover: rgba(15, 23, 42, 0.07);
|
|
11
|
+
--docserve-dock-accent: #4f46e5;
|
|
12
|
+
--docserve-dock-accent-soft: rgba(79, 70, 229, 0.12);
|
|
13
|
+
--docserve-panel-bg: #ffffff;
|
|
14
|
+
--docserve-panel-border: rgba(15, 23, 42, 0.14);
|
|
15
|
+
--docserve-panel-text: #334155;
|
|
16
|
+
--docserve-panel-muted: #64748b;
|
|
17
|
+
position: fixed;
|
|
18
|
+
top: auto;
|
|
19
|
+
left: auto;
|
|
20
|
+
right: var(--docserve-inset-r);
|
|
21
|
+
bottom: var(--docserve-inset-b);
|
|
22
|
+
z-index: 2147483647;
|
|
23
|
+
transition: left 0.18s ease, right 0.18s ease, top 0.18s ease, bottom 0.18s ease;
|
|
24
|
+
color-scheme: light dark;
|
|
25
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
|
|
26
|
+
margin: 0;
|
|
27
|
+
padding: 0;
|
|
28
|
+
line-height: 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* Four corners: `data-corner` is persisted and rewritten by dragging. Left
|
|
32
|
+
corners follow the sidebar so the pill never sits under an open panel. */
|
|
33
|
+
:host([data-corner="bl"]) { right: auto; left: calc(var(--docserve-inset-l) + var(--docserve-panel-left, 0px)); }
|
|
34
|
+
:host([data-corner="br"]) { right: calc(var(--docserve-inset-r) + var(--docserve-panel-right, 0px)); }
|
|
35
|
+
:host([data-corner="tr"]) {
|
|
36
|
+
bottom: auto;
|
|
37
|
+
top: var(--docserve-inset-t);
|
|
38
|
+
right: calc(var(--docserve-inset-r) + var(--docserve-panel-right, 0px));
|
|
39
|
+
}
|
|
40
|
+
:host([data-corner="tl"]) {
|
|
41
|
+
right: auto;
|
|
42
|
+
bottom: auto;
|
|
43
|
+
left: calc(var(--docserve-inset-l) + var(--docserve-panel-left, 0px));
|
|
44
|
+
top: var(--docserve-inset-t);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
:host([data-dragging]) { cursor: grabbing; user-select: none; transition: none; }
|
|
48
|
+
|
|
49
|
+
@media (prefers-color-scheme: dark) {
|
|
50
|
+
:host {
|
|
51
|
+
--docserve-dock-bg: rgba(21, 26, 35, 0.92);
|
|
52
|
+
--docserve-dock-border: rgba(148, 163, 184, 0.24);
|
|
53
|
+
--docserve-dock-text: #e2e8f0;
|
|
54
|
+
--docserve-dock-muted: #94a3b8;
|
|
55
|
+
--docserve-dock-hover: rgba(148, 163, 184, 0.14);
|
|
56
|
+
--docserve-dock-accent: #a5b4fc;
|
|
57
|
+
--docserve-dock-accent-soft: rgba(165, 180, 252, 0.16);
|
|
58
|
+
--docserve-panel-bg: #151a23;
|
|
59
|
+
--docserve-panel-border: rgba(148, 163, 184, 0.2);
|
|
60
|
+
--docserve-panel-text: #e2e8f0;
|
|
61
|
+
--docserve-panel-muted: #94a3b8;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
html[data-docserve-widget] body {
|
|
66
|
+
transition: padding-left 0.18s ease, padding-right 0.18s ease;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
html[data-docserve-panel-left] body {
|
|
70
|
+
padding-left: var(--docserve-panel-left) !important;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
html[data-docserve-panel-right] {
|
|
74
|
+
--docserve-panel-right: 260px;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
html[data-docserve-panel-right] body {
|
|
78
|
+
padding-right: var(--docserve-panel-right) !important;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
html[data-docserve-palette] {
|
|
82
|
+
overflow: hidden;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.docserve-dock,
|
|
86
|
+
.docserve-dock *,
|
|
87
|
+
.docserve-panel,
|
|
88
|
+
.docserve-panel * {
|
|
89
|
+
box-sizing: border-box;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.docserve-dock {
|
|
93
|
+
display: flex;
|
|
94
|
+
align-items: center;
|
|
95
|
+
gap: 2px;
|
|
96
|
+
padding: 3px;
|
|
97
|
+
border: 1px solid var(--docserve-dock-border);
|
|
98
|
+
border-radius: 10px;
|
|
99
|
+
background: var(--docserve-dock-bg);
|
|
100
|
+
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
|
|
101
|
+
backdrop-filter: blur(12px);
|
|
102
|
+
cursor: grab;
|
|
103
|
+
/* let the pill be dragged by touch instead of scrolling the page */
|
|
104
|
+
touch-action: none;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
:host([data-dragging]) .docserve-dock {
|
|
108
|
+
cursor: grabbing;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.docserve-dock__button {
|
|
112
|
+
display: grid;
|
|
113
|
+
place-items: center;
|
|
114
|
+
width: 30px;
|
|
115
|
+
height: 30px;
|
|
116
|
+
padding: 0;
|
|
117
|
+
border: 0;
|
|
118
|
+
border-radius: 7px;
|
|
119
|
+
background: transparent;
|
|
120
|
+
color: var(--docserve-dock-muted);
|
|
121
|
+
font: inherit;
|
|
122
|
+
line-height: 0;
|
|
123
|
+
text-decoration: none;
|
|
124
|
+
cursor: pointer;
|
|
125
|
+
appearance: none;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.docserve-dock__button:hover {
|
|
129
|
+
background: var(--docserve-dock-hover);
|
|
130
|
+
color: var(--docserve-dock-text);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.docserve-dock__button:focus-visible {
|
|
134
|
+
outline: 2px solid var(--docserve-dock-accent);
|
|
135
|
+
outline-offset: 2px;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.docserve-dock__button[aria-pressed="true"] {
|
|
139
|
+
background: var(--docserve-dock-accent-soft);
|
|
140
|
+
color: var(--docserve-dock-accent);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
.docserve-dock__button svg {
|
|
144
|
+
width: 16px;
|
|
145
|
+
height: 16px;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.docserve-dock__divider {
|
|
149
|
+
width: 1px;
|
|
150
|
+
height: 18px;
|
|
151
|
+
margin: 0 2px;
|
|
152
|
+
background: var(--docserve-dock-border);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.docserve-dock__status {
|
|
156
|
+
position: absolute;
|
|
157
|
+
width: 1px;
|
|
158
|
+
height: 1px;
|
|
159
|
+
overflow: hidden;
|
|
160
|
+
clip: rect(0 0 0 0);
|
|
161
|
+
white-space: nowrap;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.docserve-panel {
|
|
165
|
+
position: fixed;
|
|
166
|
+
top: 0;
|
|
167
|
+
bottom: 0;
|
|
168
|
+
z-index: 1;
|
|
169
|
+
flex-direction: column;
|
|
170
|
+
overflow: hidden;
|
|
171
|
+
background: var(--docserve-panel-bg);
|
|
172
|
+
color: var(--docserve-panel-text);
|
|
173
|
+
font-size: 14px;
|
|
174
|
+
line-height: 1.5;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.docserve-panel:not([data-open]) {
|
|
178
|
+
display: none;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.docserve-panel[data-open] {
|
|
182
|
+
display: flex;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.docserve-panel[data-side="left"] {
|
|
186
|
+
left: 0;
|
|
187
|
+
border-right: 1px solid var(--docserve-panel-border);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.docserve-panel[data-side="right"] {
|
|
191
|
+
right: 0;
|
|
192
|
+
border-left: 1px solid var(--docserve-panel-border);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@media (pointer: coarse) {
|
|
196
|
+
.docserve-dock__button {
|
|
197
|
+
width: 38px;
|
|
198
|
+
height: 38px;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
@media print {
|
|
203
|
+
:host {
|
|
204
|
+
display: none;
|
|
205
|
+
}
|
|
206
|
+
}
|
package/server/files.mjs
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
// File metadata for /api/files: recursive *.html listing (excluding index.html),
|
|
2
|
-
// <title> / <meta name="created_at"> / <link rel="icon"> extraction, and git date
|
|
3
|
-
// maps cached per repo root.
|
|
4
|
-
//
|
|
5
|
-
// Created-date priority: meta created_at > oldest git add commit > birthtime.
|
|
6
|
-
import { open, readdir, stat } from "node:fs/promises"
|
|
7
|
-
import { realpathSync } from "node:fs"
|
|
8
|
-
import { basename, dirname, isAbsolute, join, relative, sep } from "node:path"
|
|
9
|
-
import { execFile } from "node:child_process"
|
|
10
|
-
import { promisify } from "node:util"
|
|
11
|
-
|
|
12
|
-
const run = promisify(execFile)
|
|
13
|
-
|
|
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
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// /api/files payload, sorted by created desc with title/path tiebreaks.
|
|
44
|
-
export async function apiFiles(docsDir) {
|
|
45
|
-
const paths = await listHtmlFiles(docsDir)
|
|
46
|
-
const { created, modified } = await gitDateMaps(docsDir)
|
|
47
|
-
const files = []
|
|
48
|
-
for (const full of paths) {
|
|
49
|
-
let st
|
|
50
|
-
try {
|
|
51
|
-
st = await stat(full)
|
|
52
|
-
} catch {
|
|
53
|
-
continue // deleted between listing and stat
|
|
54
|
-
}
|
|
55
|
-
const { title, date, favicon } = await metaOf(full, st.mtimeMs, docsDir)
|
|
56
|
-
let key
|
|
57
|
-
try {
|
|
58
|
-
key = realpathSync(full)
|
|
59
|
-
} catch {
|
|
60
|
-
continue
|
|
61
|
-
}
|
|
62
|
-
const rel = normalizedRelative(docsDir, full)
|
|
63
|
-
files.push({
|
|
64
|
-
title: title ?? basename(full),
|
|
65
|
-
url: `/${rel}`,
|
|
66
|
-
filePath: full,
|
|
67
|
-
favicon: favicon ?? undefined,
|
|
68
|
-
created: date ?? created.get(key) ?? Math.floor(st.birthtimeMs / 1000),
|
|
69
|
-
modified: modified.get(key) ?? Math.floor(st.mtimeMs / 1000),
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
files.sort((a, b) => b.created - a.created || a.title.localeCompare(b.title, "ja") || a.url.localeCompare(b.url))
|
|
73
|
-
return files
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// <title>, <meta name="created_at"> and <link rel="icon"> extracted from the head
|
|
77
|
-
// of a report; cached by path + mtime so repeated /api/files calls only re-read
|
|
78
|
-
// files that changed.
|
|
79
|
-
const metaCache = new Map() // "path:mtimeMs" -> { title, date, favicon }
|
|
80
|
-
|
|
81
|
-
// Decode <title> entities so cards read "A & B", not "A & B".
|
|
82
|
-
const ENTITIES = {
|
|
83
|
-
amp: "&",
|
|
84
|
-
lt: "<",
|
|
85
|
-
gt: ">",
|
|
86
|
-
quot: '"',
|
|
87
|
-
apos: "'",
|
|
88
|
-
nbsp: "\u00a0",
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function decodeEntities(s) {
|
|
92
|
-
return s.replace(/&(#[xX]?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);/g, (m, ref) => {
|
|
93
|
-
if (ref[0] === "#") {
|
|
94
|
-
const hex = /[xX]/.test(ref[1])
|
|
95
|
-
const cp = parseInt(hex ? ref.slice(2) : ref.slice(1), hex ? 16 : 10)
|
|
96
|
-
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : m
|
|
97
|
-
}
|
|
98
|
-
return ENTITIES[ref.toLowerCase()] ?? m
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async function metaOf(full, mtimeMs, docsDir) {
|
|
103
|
-
const key = `${full}:${mtimeMs}`
|
|
104
|
-
const cached = metaCache.get(key)
|
|
105
|
-
if (cached !== undefined) return cached
|
|
106
|
-
const meta = await extractMeta(full, docsDir)
|
|
107
|
-
metaCache.set(key, meta)
|
|
108
|
-
return meta
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async function extractMeta(full, docsDir) {
|
|
112
|
-
const findTitle = (text) => {
|
|
113
|
-
const raw = text.match(/<title>([^<]*)<\/title>/)?.[1]?.trim()
|
|
114
|
-
return raw == null ? null : decodeEntities(raw)
|
|
115
|
-
}
|
|
116
|
-
const findDate = (text) => {
|
|
117
|
-
for (const tag of text.match(/<meta\b[^>]*>/gi) ?? []) {
|
|
118
|
-
if (!/\bname\s*=\s*["']created_at["']/i.test(tag)) continue
|
|
119
|
-
const d = new Date(tag.match(/\bcontent\s*=\s*["']([^"']*)["']/i)?.[1]?.trim() ?? "")
|
|
120
|
-
if (!isNaN(d.getTime())) return Math.floor(d.getTime() / 1000)
|
|
121
|
-
}
|
|
122
|
-
return null
|
|
123
|
-
}
|
|
124
|
-
// Attribute value from a tag string ("v", 'v', or bare); null when absent.
|
|
125
|
-
const attrOf = (tag, name) => {
|
|
126
|
-
const m = tag.match(new RegExp(`(?<![\\w-])${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, "i"))
|
|
127
|
-
return m?.[1] ?? m?.[2] ?? m?.[3] ?? null
|
|
128
|
-
}
|
|
129
|
-
// First <link rel="...icon..."> href resolved to a URL served from docsDir;
|
|
130
|
-
// data: and absolute http(s) URLs are kept as-is.
|
|
131
|
-
const findFavicon = (text) => {
|
|
132
|
-
// Quoted values may contain ">" (inline SVG data URIs), so scan links
|
|
133
|
-
// value-aware instead of with a plain [^>]* tag match.
|
|
134
|
-
for (const tag of text.match(/<link\b(?:"[^"]*"|'[^']*'|[^>])*>/gi) ?? []) {
|
|
135
|
-
const rel = attrOf(tag, "rel")
|
|
136
|
-
if (!rel || !/\bicon\b/i.test(rel)) continue
|
|
137
|
-
const href = attrOf(tag, "href")?.trim()
|
|
138
|
-
if (!href) continue
|
|
139
|
-
if (/^data:/i.test(href)) return href.replaceAll("#", "%23")
|
|
140
|
-
if (/^(https?:|\/\/)/i.test(href)) return href
|
|
141
|
-
const target = href.startsWith("/")
|
|
142
|
-
? join(docsDir, href.split(/[?#]/)[0])
|
|
143
|
-
: join(dirname(full), href.split(/[?#]/)[0])
|
|
144
|
-
if (!isWithin(docsDir, target)) return null
|
|
145
|
-
return `/${normalizedRelative(docsDir, target)}`
|
|
146
|
-
}
|
|
147
|
-
return null
|
|
148
|
-
}
|
|
149
|
-
try {
|
|
150
|
-
const handle = await open(full, "r")
|
|
151
|
-
try {
|
|
152
|
-
const read = async (len, pos) => {
|
|
153
|
-
const { buffer, bytesRead } = await handle.read(Buffer.alloc(len), 0, len, pos)
|
|
154
|
-
return buffer.toString("utf8", 0, bytesRead)
|
|
155
|
-
}
|
|
156
|
-
// Titles and meta tags live in <head>; read only the first 4 KB.
|
|
157
|
-
const head = await read(4096, 0)
|
|
158
|
-
let meta = {
|
|
159
|
-
title: findTitle(head),
|
|
160
|
-
date: findDate(head),
|
|
161
|
-
favicon: findFavicon(head),
|
|
162
|
-
}
|
|
163
|
-
if (meta.title === null || meta.date === null || meta.favicon === null) {
|
|
164
|
-
// Fallback for documents with a long preamble (inline favicon etc.)
|
|
165
|
-
const text = await read(1 << 20, 0)
|
|
166
|
-
meta = {
|
|
167
|
-
title: meta.title ?? findTitle(text),
|
|
168
|
-
date: meta.date ?? findDate(text),
|
|
169
|
-
favicon: meta.favicon ?? findFavicon(text),
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return meta
|
|
173
|
-
} finally {
|
|
174
|
-
await handle.close()
|
|
175
|
-
}
|
|
176
|
-
} catch {
|
|
177
|
-
return { title: null, date: null, favicon: null }
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Resolve created/modified dates from git history, keyed by absolute path.
|
|
182
|
-
// A single full-repo scan with cwd set to the docs dir, cached per repo root in
|
|
183
|
-
// module scope. git log outputs newest first: overwriting keeps the oldest add
|
|
184
|
-
// (= creation date) and the newest touch (= modified date).
|
|
185
|
-
const gitMapsCache = new Map() // repo root (or docsDir when not a repo) -> { created, modified }
|
|
186
|
-
|
|
187
|
-
async function gitDateMaps(docsDir) {
|
|
188
|
-
const cwd = realpath(docsDir)
|
|
189
|
-
const top = (await git(["rev-parse", "--show-toplevel"], cwd)).trim()
|
|
190
|
-
const root = top ? realpath(top) : null
|
|
191
|
-
const cacheKey = root ?? cwd
|
|
192
|
-
let maps = gitMapsCache.get(cacheKey)
|
|
193
|
-
if (maps) return maps
|
|
194
|
-
|
|
195
|
-
maps = { created: new Map(), modified: new Map() }
|
|
196
|
-
if (root) {
|
|
197
|
-
try {
|
|
198
|
-
const [first, last] = await Promise.all([
|
|
199
|
-
git(["log", "--diff-filter=A", "--name-only", "--format=c:%at"], cwd),
|
|
200
|
-
git(["log", "--name-only", "--format=m:%at"], cwd),
|
|
201
|
-
])
|
|
202
|
-
let ts
|
|
203
|
-
for (const line of first.split("\n")) {
|
|
204
|
-
if (line.startsWith("c:")) {
|
|
205
|
-
ts = parseInt(line.slice(2), 10)
|
|
206
|
-
} else if (line && ts !== undefined) {
|
|
207
|
-
maps.created.set(join(root, line), ts) // oldest add wins
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
ts = undefined
|
|
211
|
-
for (const line of last.split("\n")) {
|
|
212
|
-
if (line.startsWith("m:")) {
|
|
213
|
-
ts = parseInt(line.slice(2), 10)
|
|
214
|
-
} else if (line && ts !== undefined) {
|
|
215
|
-
maps.modified.set(join(root, line), ts) // newest touch wins
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} catch {
|
|
219
|
-
// unreadable history → fall back to fs dates
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
gitMapsCache.set(cacheKey, maps)
|
|
223
|
-
return maps
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async function git(args, cwd) {
|
|
227
|
-
try {
|
|
228
|
-
const { stdout } = await run("git", args, { cwd, timeout: 10000, maxBuffer: 64 * 1024 * 1024 })
|
|
229
|
-
return stdout
|
|
230
|
-
} catch {
|
|
231
|
-
return ""
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// On macOS /var is a symlink to /private/var; canonicalize to real paths so
|
|
236
|
-
// lookups match git output.
|
|
237
|
-
function realpath(p) {
|
|
238
|
-
try {
|
|
239
|
-
return realpathSync(p)
|
|
240
|
-
} catch {
|
|
241
|
-
return p
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// --- Path utilities (shared by the HTTP handler and the watcher) ---
|
|
246
|
-
|
|
247
|
-
export function isWithin(base, path) {
|
|
248
|
-
const rel = relative(base, path)
|
|
249
|
-
return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel))
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function normalizedRelative(base, path) {
|
|
253
|
-
return relative(base, path).split(sep).join("/")
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export function shouldIgnore(relPath) {
|
|
257
|
-
return relPath
|
|
258
|
-
.split(/[/\\]/)
|
|
259
|
-
.filter(Boolean)
|
|
260
|
-
.some((part) => part.startsWith(".") || part === "node_modules")
|
|
261
|
-
}
|
package/server/index.mjs
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
// docserve server core: HTTP layer + server lifecycle.
|
|
2
|
-
//
|
|
3
|
-
// ┌────────────────────────────────────────────────────────┐
|
|
4
|
-
// │ runServer() │
|
|
5
|
-
// │ ├─ createServer ──▶ handler() │ # HTTP
|
|
6
|
-
// │ │ ├─ /api/files ──▶ apiFiles() │
|
|
7
|
-
// │ │ ├─ /, /index.html ──▶ gallery template │
|
|
8
|
-
// │ │ └─ other paths ──▶ raw files ──▶ injectIntoHtml() │
|
|
9
|
-
// │ │ └─ /dir/ ──▶ dir/index.html │
|
|
10
|
-
// │ ├─ WebSocketServer("/__reload") ◀─ FileWatcher │
|
|
11
|
-
// │ └─ listen ──▶ listener.start() + writeState() │
|
|
12
|
-
// └────────────────────────────────────────────────────────┘
|
|
13
|
-
import { createServer } from "node:http"
|
|
14
|
-
import { readFile, stat } from "node:fs/promises"
|
|
15
|
-
import os from "node:os"
|
|
16
|
-
import { basename, extname, resolve } from "node:path"
|
|
17
|
-
import { exec } from "node:child_process"
|
|
18
|
-
import { clearState, writeState } from "./state.mjs"
|
|
19
|
-
import { apiFiles, isWithin, normalizedRelative } from "./files.mjs"
|
|
20
|
-
import { injectIntoHtml, WS_PATH } from "./inject.mjs"
|
|
21
|
-
import { WebSocketServer } from "./websocket.mjs"
|
|
22
|
-
import { FileWatcher } from "./watch.mjs"
|
|
23
|
-
|
|
24
|
-
const MAX_PORT_TRIES = 20 // on conflict, try the next port up to 20 times
|
|
25
|
-
const INDEX_TEMPLATE = resolve(import.meta.dirname, "..", "index.html")
|
|
26
|
-
|
|
27
|
-
const MIME = {
|
|
28
|
-
".html": "text/html",
|
|
29
|
-
".htm": "text/html",
|
|
30
|
-
".js": "text/javascript",
|
|
31
|
-
".mjs": "text/javascript",
|
|
32
|
-
".css": "text/css",
|
|
33
|
-
".json": "application/json",
|
|
34
|
-
".txt": "text/plain",
|
|
35
|
-
".md": "text/markdown",
|
|
36
|
-
".xml": "application/xml",
|
|
37
|
-
".png": "image/png",
|
|
38
|
-
".jpg": "image/jpeg",
|
|
39
|
-
".jpeg": "image/jpeg",
|
|
40
|
-
".gif": "image/gif",
|
|
41
|
-
".svg": "image/svg+xml",
|
|
42
|
-
".webp": "image/webp",
|
|
43
|
-
".ico": "image/x-icon",
|
|
44
|
-
".pdf": "application/pdf",
|
|
45
|
-
".woff": "font/woff",
|
|
46
|
-
".woff2": "font/woff2",
|
|
47
|
-
".mp4": "video/mp4",
|
|
48
|
-
".webm": "video/webm",
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export async function runServer({ docsDir, port: initialPort, open = false }) {
|
|
52
|
-
const wss = new WebSocketServer({ path: WS_PATH })
|
|
53
|
-
const fileWatcher = new FileWatcher({ wss, docsDir, templatePath: INDEX_TEMPLATE })
|
|
54
|
-
let port = initialPort
|
|
55
|
-
|
|
56
|
-
const server = createServer(createNodeServerAdapter(createHandler({ docsDir })))
|
|
57
|
-
wss.attach(server)
|
|
58
|
-
|
|
59
|
-
const cleanup = () => {
|
|
60
|
-
fileWatcher.close()
|
|
61
|
-
wss.close()
|
|
62
|
-
clearState()
|
|
63
|
-
server.closeAllConnections()
|
|
64
|
-
if (server.listening) server.close()
|
|
65
|
-
}
|
|
66
|
-
process.on("SIGINT", () => { cleanup(); process.exit(0) })
|
|
67
|
-
process.on("SIGTERM", () => { cleanup(); process.exit(0) })
|
|
68
|
-
|
|
69
|
-
server.on("error", (err) => {
|
|
70
|
-
if (err.code === "EADDRINUSE" && port < initialPort + MAX_PORT_TRIES) {
|
|
71
|
-
port += 1
|
|
72
|
-
server.listen(port, "127.0.0.1")
|
|
73
|
-
} else {
|
|
74
|
-
console.error(err)
|
|
75
|
-
process.exit(1)
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
server.listen(port, "127.0.0.1", async () => {
|
|
79
|
-
await fileWatcher.start()
|
|
80
|
-
const state = { pid: process.pid, port, url: `http://localhost:${port}/`, docsDir }
|
|
81
|
-
writeState(state)
|
|
82
|
-
console.log(state.url)
|
|
83
|
-
if (open && process.platform === "darwin") exec(`open ${state.url}`)
|
|
84
|
-
})
|
|
85
|
-
return { server, wss, fileWatcher, close: cleanup }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// --- Node server adapter ---
|
|
89
|
-
|
|
90
|
-
function createNodeServerAdapter(handler) {
|
|
91
|
-
return async (req, res) => {
|
|
92
|
-
try {
|
|
93
|
-
const response = await handler(toWebRequest(req))
|
|
94
|
-
res.writeHead(response.status, Object.fromEntries(response.headers))
|
|
95
|
-
res.end(Buffer.from(await response.arrayBuffer()))
|
|
96
|
-
} catch (err) {
|
|
97
|
-
console.error(err)
|
|
98
|
-
res.writeHead(500, { "content-type": "text/plain" })
|
|
99
|
-
res.end("Internal Server Error")
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function toWebRequest(req) {
|
|
105
|
-
const host = req.headers.host || "localhost"
|
|
106
|
-
return new Request(`http://${host}${req.url}`, { method: req.method, headers: req.headers })
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// --- HTTP handler ---
|
|
110
|
-
|
|
111
|
-
function createHandler({ docsDir }) {
|
|
112
|
-
return async function handler(request) {
|
|
113
|
-
const url = new URL(request.url)
|
|
114
|
-
|
|
115
|
-
if (url.pathname === "/api/files") {
|
|
116
|
-
try {
|
|
117
|
-
return Response.json(await apiFiles(docsDir))
|
|
118
|
-
} catch (err) {
|
|
119
|
-
console.error(err)
|
|
120
|
-
return Response.json([])
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
let pathname
|
|
125
|
-
try {
|
|
126
|
-
pathname = decodeURIComponent(url.pathname)
|
|
127
|
-
} catch {
|
|
128
|
-
return new Response("Bad Request", { status: 400 })
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (pathname === "/" || pathname === "/index.html") {
|
|
132
|
-
return galleryResponse(docsDir)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (pathname.includes("\0")) {
|
|
136
|
-
return new Response("Bad Request", { status: 400 })
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// /sub/ → /sub/index.html
|
|
140
|
-
const target = pathname.endsWith("/") ? `${pathname}index.html` : pathname
|
|
141
|
-
const path = resolve(docsDir, target.slice(1))
|
|
142
|
-
if (!isWithin(docsDir, path)) {
|
|
143
|
-
return new Response("Forbidden", { status: 403 })
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
const body = await readFile(path)
|
|
148
|
-
const type = MIME[extname(path).toLowerCase()] || "application/octet-stream"
|
|
149
|
-
// Inject the reload client + copy path button into docsDir pages only;
|
|
150
|
-
// the gallery template ships its own client.
|
|
151
|
-
if (type === "text/html") {
|
|
152
|
-
const html = injectIntoHtml(body.toString(), normalizedRelative(docsDir, path), path)
|
|
153
|
-
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
|
|
154
|
-
}
|
|
155
|
-
return new Response(body, { headers: { "content-type": type } })
|
|
156
|
-
} catch {
|
|
157
|
-
// Redirect /sub → /sub/
|
|
158
|
-
try {
|
|
159
|
-
if ((await stat(path)).isDirectory()) {
|
|
160
|
-
return Response.redirect(new URL(`${url.pathname}/`, url), 301)
|
|
161
|
-
}
|
|
162
|
-
} catch { }
|
|
163
|
-
return new Response("Not Found", { status: 404 })
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// The gallery is an embedded template served at / and /index.html; docsDir's own
|
|
169
|
-
// index.html is shadowed by it. Directory markers are substituted for display.
|
|
170
|
-
async function galleryResponse(docsDir) {
|
|
171
|
-
try {
|
|
172
|
-
let html = await readFile(INDEX_TEMPLATE, "utf8")
|
|
173
|
-
html = html
|
|
174
|
-
.replaceAll("__DOCS_DIR_NAME__", escapeHtml(basename(docsDir) || docsDir))
|
|
175
|
-
.replaceAll("__DOCS_DIR__", escapeHtml(displayPath(docsDir)))
|
|
176
|
-
.replaceAll("__FAVICON_HUE__", String(faviconHue(docsDir)))
|
|
177
|
-
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8" } })
|
|
178
|
-
} catch {
|
|
179
|
-
return new Response("Gallery template missing", { status: 500 })
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Collapse the home directory prefix to ~ for display.
|
|
184
|
-
function displayPath(p) {
|
|
185
|
-
const home = os.homedir()
|
|
186
|
-
return p.startsWith(`${home}/`) ? `~${p.slice(home.length)}` : p
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Deterministic hue for the folder favicon: the same path always yields the same color.
|
|
190
|
-
function faviconHue(s) {
|
|
191
|
-
let h = 0
|
|
192
|
-
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0
|
|
193
|
-
return ((h % 360) + 360) % 360
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function escapeHtml(s) {
|
|
197
|
-
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
198
|
-
}
|