@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.
Files changed (76) hide show
  1. package/bin.mjs +2 -2
  2. package/dist/widget/index.js +11803 -0
  3. package/package.json +24 -3
  4. package/src/node/adapter.mjs +67 -0
  5. package/src/node/assets.mjs +36 -0
  6. package/src/node/dates.mjs +73 -0
  7. package/src/node/docs-info.mjs +39 -0
  8. package/src/node/files.mjs +43 -0
  9. package/src/node/gallery.mjs +33 -0
  10. package/src/node/inject.mjs +43 -0
  11. package/src/node/meta.mjs +137 -0
  12. package/src/node/routes.mjs +105 -0
  13. package/src/node/scan.mjs +87 -0
  14. package/src/node/serve-static.mjs +158 -0
  15. package/src/node/server.mjs +69 -0
  16. package/src/node/watch.mjs +119 -0
  17. package/src/plugins/collapse/client.js +45 -0
  18. package/src/plugins/collapse/collapse.css +74 -0
  19. package/src/plugins/collapse/commands.ts +81 -0
  20. package/src/plugins/collapse/model.ts +123 -0
  21. package/src/plugins/collapse/sections.ts +105 -0
  22. package/src/plugins/collapse/view.ts +84 -0
  23. package/src/plugins/copy/client.js +72 -0
  24. package/src/plugins/copy/copy.css +21 -0
  25. package/src/plugins/home/client.js +24 -0
  26. package/src/plugins/sidebar/client.js +106 -0
  27. package/src/plugins/sidebar/grouping.js +108 -0
  28. package/src/plugins/sidebar/icons.js +25 -0
  29. package/src/plugins/sidebar/model.ts +91 -0
  30. package/src/plugins/sidebar/resize-model.ts +47 -0
  31. package/src/plugins/sidebar/sidebar.css +663 -0
  32. package/src/plugins/sidebar/tree-model.ts +23 -0
  33. package/src/plugins/sidebar/tree.ts +69 -0
  34. package/src/plugins/sidebar/ui/PageList.tsx +153 -0
  35. package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
  36. package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
  37. package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
  38. package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
  39. package/src/plugins/sidebar/ui/preview-model.ts +134 -0
  40. package/src/plugins/sidebar/ui/useModel.ts +25 -0
  41. package/src/plugins/toc/client.js +55 -0
  42. package/src/plugins/toc/commands.ts +49 -0
  43. package/src/plugins/toc/heading-html.ts +40 -0
  44. package/src/plugins/toc/model.ts +178 -0
  45. package/src/plugins/toc/outline.js +94 -0
  46. package/src/plugins/toc/scroll-spy.ts +35 -0
  47. package/src/plugins/toc/toc.css +214 -0
  48. package/src/plugins/toc/ui/TocPanel.tsx +87 -0
  49. package/src/shared/types.d.ts +193 -0
  50. package/src/widget/commands.js +214 -0
  51. package/src/widget/docks.js +127 -0
  52. package/src/widget/host.js +187 -0
  53. package/src/widget/index.js +22 -0
  54. package/src/widget/lib/disposables.js +44 -0
  55. package/src/widget/lib/dom.js +106 -0
  56. package/src/widget/lib/events.js +65 -0
  57. package/src/widget/lib/headings.js +132 -0
  58. package/src/widget/lib/persisted-signal.js +40 -0
  59. package/src/widget/lib/storage.js +70 -0
  60. package/src/widget/live.js +86 -0
  61. package/src/widget/pages.js +65 -0
  62. package/src/widget/palette.css +162 -0
  63. package/src/widget/palette.js +259 -0
  64. package/src/widget/placement.js +204 -0
  65. package/src/widget/plugins.js +39 -0
  66. package/src/widget/rpc.js +32 -0
  67. package/src/widget/styles.js +31 -0
  68. package/src/widget/ui.js +50 -0
  69. package/src/widget/widget.css +206 -0
  70. package/server/files.mjs +0 -261
  71. package/server/index.mjs +0 -198
  72. package/server/inject.html +0 -88
  73. package/server/inject.mjs +0 -28
  74. package/server/watch.mjs +0 -104
  75. /package/{server → src/node}/state.mjs +0 -0
  76. /package/{server → src/node}/websocket.mjs +0 -0
@@ -0,0 +1,123 @@
1
+ import { effect, signal } from "@preact/signals-react"
2
+ import type { Signal } from "@preact/signals-react"
3
+ import { persisted, persistedBool } from "../../widget/lib/persisted-signal.js"
4
+ import type { CollapseCollect, CollapseSection } from "./sections.js"
5
+ import { applyState, mountSections } from "./view.js"
6
+
7
+ export class CollapseModel {
8
+ readonly collapsed: Signal<string[]>
9
+ readonly enabled: Signal<boolean>
10
+ readonly sections = signal<CollapseSection[]>([])
11
+
12
+ #wrappers: HTMLElement[] = []
13
+ #mounted: CollapseSection[] | null = null
14
+ #unmount: (() => void) | null = null
15
+
16
+ constructor(page: string) {
17
+ this.collapsed = persisted<string[]>(`docserve:collapse:${page}`, [])
18
+ this.enabled = persistedBool("docserve:collapse:enabled", true)
19
+ }
20
+
21
+ static create(page: string): CollapseModel {
22
+ return new CollapseModel(page)
23
+ }
24
+
25
+ load(collect: CollapseCollect): void {
26
+ const ids = new Set(collect.sections.map((section) => section.id))
27
+ const current = this.collapsed.value
28
+ const next = current.filter((id) => ids.has(id))
29
+ if (next.length !== current.length) this.collapsed.value = next
30
+
31
+ this.#wrappers = collect.wrappers
32
+ this.sections.value = collect.sections
33
+ }
34
+
35
+ subscribe(): () => void {
36
+ const stop = effect(() => {
37
+ const sections = this.sections.value
38
+ const enabled = this.enabled.value
39
+
40
+ if (enabled) {
41
+ if (this.#mounted !== sections) {
42
+ this.#unmount?.()
43
+ this.#unmount = mountSections(sections, (id) => this.toggle(id))
44
+ this.#mounted = sections
45
+ }
46
+ } else if (this.#unmount) {
47
+ this.#unmount()
48
+ this.#unmount = null
49
+ this.#mounted = null
50
+ }
51
+
52
+ applyState(sections, this.collapsed.value, enabled)
53
+ })
54
+
55
+ return () => {
56
+ stop()
57
+ this.#unmount?.()
58
+ this.#unmount = null
59
+ this.#mounted = null
60
+
61
+ const wrappers = this.#wrappers
62
+ this.#wrappers = []
63
+ for (const wrapper of wrappers.reverse()) {
64
+ if (wrapper.isConnected) wrapper.replaceWith(...wrapper.childNodes)
65
+ }
66
+ }
67
+ }
68
+
69
+ toggleEnabled(): void {
70
+ this.enabled.value = !this.enabled.value
71
+ }
72
+
73
+ toggle(id: string): void {
74
+ const collapsed = new Set(this.collapsed.value)
75
+ if (collapsed.has(id)) collapsed.delete(id)
76
+ else collapsed.add(id)
77
+ this.collapsed.value = [...collapsed]
78
+ }
79
+
80
+ expand(id: string): void {
81
+ this.collapsed.value = this.collapsed.value.filter((item) => item !== id)
82
+ }
83
+
84
+ collapse(id: string): void {
85
+ if (this.isCollapsed(id)) return
86
+ this.collapsed.value = [...this.collapsed.value, id]
87
+ }
88
+
89
+ expandAncestors(id: string): void {
90
+ if (!this.enabled.value) return
91
+
92
+ let section = this.sections.value.find((item) => item.id === id)?.parent ?? null
93
+ if (!section) return
94
+
95
+ const collapsed = new Set(this.collapsed.value)
96
+ let changed = false
97
+ while (section) {
98
+ if (collapsed.delete(section.id)) changed = true
99
+ section = section.parent
100
+ }
101
+
102
+ if (changed) this.collapsed.value = [...collapsed]
103
+ }
104
+
105
+ toggleAll(): void {
106
+ const sections = this.sections.value
107
+ const collapsed = new Set(this.collapsed.value)
108
+ const allCollapsed = sections.every((section) => collapsed.has(section.id))
109
+ this.collapsed.value = allCollapsed ? [] : sections.map((section) => section.id)
110
+ }
111
+
112
+ collapseAll(): void {
113
+ this.collapsed.value = this.sections.value.map((section) => section.id)
114
+ }
115
+
116
+ expandAll(): void {
117
+ this.collapsed.value = []
118
+ }
119
+
120
+ isCollapsed(id: string): boolean {
121
+ return this.enabled.value && this.collapsed.value.includes(id)
122
+ }
123
+ }
@@ -0,0 +1,105 @@
1
+ import { collectHeadings, ensureIds, findRoot } from "../../widget/lib/headings.js"
2
+
3
+ export interface CollapseSection {
4
+ id: string
5
+ heading: HTMLHeadingElement
6
+ level: number
7
+ body: Element[]
8
+ button?: HTMLButtonElement
9
+ parent: CollapseSection | null
10
+ }
11
+
12
+ export interface CollapseCollect {
13
+ sections: CollapseSection[]
14
+ wrappers: HTMLElement[]
15
+ }
16
+
17
+ const EXCLUDED = "nav, aside, header, footer, [data-collapse-ignore], #docserve-dock-host, [data-docserve-widget]"
18
+ const DEFAULT_LEVELS: [number, number] = [2, 4]
19
+ const SKIP = new Set(["SCRIPT", "STYLE", "LINK", "TEMPLATE", "NOSCRIPT"])
20
+
21
+ export function collectSections(doc: Document = document): CollapseCollect {
22
+ const source = doc.body ?? doc.documentElement
23
+ if (!source) return { sections: [], wrappers: [] }
24
+
25
+ const root = findRoot(doc, "data-collapse-root")
26
+ if (!root) return { sections: [], wrappers: [] }
27
+ if ((root.getAttribute("data-collapse") ?? source.getAttribute("data-collapse")) === "false")
28
+ return { sections: [], wrappers: [] }
29
+
30
+ const raw = root.getAttribute("data-collapse-levels") ?? source.getAttribute("data-collapse-levels")
31
+ const range = parseLevels(raw)
32
+ if (!range) return { sections: [], wrappers: [] }
33
+
34
+ const headings = collectHeadings(root, range, EXCLUDED)
35
+ ensureIds(headings, doc)
36
+
37
+ const wrappers: HTMLElement[] = []
38
+ const sections: CollapseSection[] = headings
39
+ .map((heading) => ({
40
+ id: heading.id,
41
+ heading,
42
+ level: Number(heading.tagName[1]),
43
+ body: bodyNodes(heading, wrappers, doc),
44
+ parent: null,
45
+ }))
46
+ .filter((section) => section.body.length)
47
+
48
+ const stack: CollapseSection[] = []
49
+ for (const section of sections) {
50
+ while (stack.length && stack[stack.length - 1].level >= section.level) stack.pop()
51
+ section.parent = stack[stack.length - 1] ?? null
52
+ stack.push(section)
53
+ }
54
+
55
+ return { sections, wrappers }
56
+ }
57
+
58
+ function parseLevels(value: string | null | undefined): [number, number] | null {
59
+ if (value === "false") return null
60
+ if (!value || value.trim() === "") return [...DEFAULT_LEVELS]
61
+ if (value === "deep") return [2, 6]
62
+
63
+ const parts = value.split(/[,-]/).map((part) => Number(part.trim())).filter(Number.isFinite)
64
+ if (!parts.length) return [...DEFAULT_LEVELS]
65
+
66
+ const min = clampLevel(parts[0])
67
+ const max = clampLevel(parts.length > 1 ? parts[1] : parts[0])
68
+ return min <= max ? [min, max] : [max, min]
69
+ }
70
+
71
+ function clampLevel(value: number): number {
72
+ return Math.min(6, Math.max(1, Math.round(value)))
73
+ }
74
+
75
+ function bodyNodes(heading: HTMLHeadingElement, wrappers: HTMLElement[], doc: Document): Element[] {
76
+ const level = Number(heading.tagName[1])
77
+ const nodes: Element[] = []
78
+
79
+ let node = heading.nextSibling
80
+ while (node) {
81
+ const next = node.nextSibling
82
+
83
+ if (node.nodeType === Node.ELEMENT_NODE) {
84
+ const element = node as Element
85
+ if (element.matches("#docserve-dock-host, [data-docserve-widget]")) break
86
+ if (/^H[1-6]$/.test(element.tagName) && Number(element.tagName[1]) <= level) break
87
+ if (SKIP.has(element.tagName) || element.matches("[data-collapse-ignore]")) {
88
+ node = next
89
+ continue
90
+ }
91
+ nodes.push(element)
92
+ } else if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {
93
+ const wrapper = doc.createElement("span")
94
+ wrapper.className = "docserve-collapse__text"
95
+ node.replaceWith(wrapper)
96
+ wrapper.append(node)
97
+ wrappers.push(wrapper)
98
+ nodes.push(wrapper)
99
+ }
100
+
101
+ node = next
102
+ }
103
+
104
+ return nodes
105
+ }
@@ -0,0 +1,84 @@
1
+ import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=16&height=16"
2
+ import { el, iconSvg } from "../../widget/lib/dom.js"
3
+ import type { CollapseSection } from "./sections.js"
4
+
5
+ const ICON_CHEVRON = iconSvg(ICON_CHEVRON_RAW)
6
+ const HIDDEN_CLASS = "docserve-collapse__hidden"
7
+ const SECTION_COLLAPSED_CLASS = "docserve-collapse__collapsed"
8
+
9
+ export function mountSections(sections: CollapseSection[], onToggle: (id: string) => void): () => void {
10
+ const controller = new AbortController()
11
+
12
+ for (const section of sections) {
13
+ const button = el("button", {
14
+ type: "button",
15
+ class: "docserve-collapse__toggle",
16
+ "data-collapse-toggle": "",
17
+ "aria-expanded": "true",
18
+ "aria-label": "Collapse section",
19
+ title: "Collapse section",
20
+ html: ICON_CHEVRON,
21
+ }) as HTMLButtonElement
22
+
23
+ button.addEventListener("click", () => onToggle(section.id), { signal: controller.signal })
24
+
25
+ section.heading.classList.add("docserve-collapse__heading")
26
+ section.heading.prepend(button)
27
+ section.button = button
28
+ }
29
+
30
+ return () => {
31
+ controller.abort()
32
+
33
+ for (const section of sections) {
34
+ section.button?.remove()
35
+ section.button = undefined
36
+ section.heading.classList.remove("docserve-collapse__heading")
37
+ section.heading.parentElement?.classList.remove(SECTION_COLLAPSED_CLASS)
38
+ for (const node of section.body) node.classList.remove(HIDDEN_CLASS)
39
+ }
40
+ }
41
+ }
42
+
43
+ export function applyState(sections: CollapseSection[], collapsed: string[], enabled: boolean): void {
44
+ const collapsedIds = new Set(collapsed)
45
+ const hidden = new Set<Element>()
46
+
47
+ if (enabled) {
48
+ for (const section of sections) {
49
+ if (!collapsedIds.has(section.id)) continue
50
+ for (const node of section.body) hidden.add(node)
51
+ }
52
+ }
53
+
54
+ for (const section of sections) {
55
+ for (const node of section.body)
56
+ node.classList.toggle(HIDDEN_CLASS, enabled && hidden.has(node))
57
+
58
+ section.heading.classList.toggle("docserve-collapse__heading", enabled)
59
+
60
+ const parent = section.heading.parentElement
61
+ if (!enabled)
62
+ parent?.classList.remove(SECTION_COLLAPSED_CLASS)
63
+ else if (parent?.tagName === "SECTION" && isSectionHead(section.heading))
64
+ parent.classList.toggle(SECTION_COLLAPSED_CLASS, collapsedIds.has(section.id))
65
+
66
+ const button = section.button
67
+ if (!button) continue
68
+
69
+ const open = !collapsedIds.has(section.id)
70
+ const label = open ? "Collapse section" : "Expand section"
71
+ button.setAttribute("aria-expanded", String(open))
72
+ button.title = label
73
+ button.setAttribute("aria-label", label)
74
+ }
75
+ }
76
+
77
+ function isSectionHead(heading: HTMLHeadingElement): boolean {
78
+ let node = heading.previousElementSibling
79
+ while (node) {
80
+ if (/^H[1-6]$/.test(node.tagName)) return false
81
+ node = node.previousElementSibling
82
+ }
83
+ return true
84
+ }
@@ -0,0 +1,72 @@
1
+ // Dock action: copy the current page's absolute path.
2
+ import ICON_CHECK_RAW from "~icons/lucide/check?raw&width=18&height=18"
3
+ import ICON_COPY_RAW from "~icons/lucide/copy?raw&width=18&height=18"
4
+ import { iconSvg, writeClipboard } from "../../widget/lib/dom.js"
5
+
6
+ const ICON_COPY = iconSvg(ICON_COPY_RAW, "docserve-icon-copy")
7
+ const ICON_CHECK = iconSvg(ICON_CHECK_RAW, "docserve-icon-check")
8
+
9
+ /**
10
+ * @returns {DocservePlugin}
11
+ */
12
+ export function copyPlugin() {
13
+ return {
14
+ id: "copy",
15
+ setup(ctx) {
16
+ /** @type {ReturnType<typeof setTimeout> | null} */
17
+ let resetTimer = null
18
+
19
+ ctx.ui.style("/__docserve/plugins/copy/copy.css")
20
+
21
+ const offCopy = ctx.commands.register({
22
+ id: "plugin:copy:path",
23
+ title: "Copy file path",
24
+ description: "Absolute path of this page",
25
+ category: "Actions",
26
+ icon: ICON_COPY,
27
+ keys: ["c"],
28
+ run: copyPath,
29
+ })
30
+
31
+ const dock = ctx.docks.register({
32
+ id: "copy",
33
+ title: "Copy file path",
34
+ icon: ICON_COPY + ICON_CHECK,
35
+ category: "actions",
36
+ command: "plugin:copy:path",
37
+ })
38
+
39
+ return () => {
40
+ offCopy()
41
+ if (resetTimer) clearTimeout(resetTimer)
42
+ dock.dispose()
43
+ }
44
+
45
+ async function copyPath() {
46
+ const file = /** @type {DocPage | null} */ (await ctx.rpc.call("docserve:pages:resolve", ctx.page))
47
+ const text = file?.filePath ?? location.href
48
+ try {
49
+ await writeClipboard(text)
50
+ flash("copied")
51
+ ctx.ui.announce("File path copied")
52
+ } catch {
53
+ ctx.ui.announce("Copy failed")
54
+ }
55
+ }
56
+
57
+ /**
58
+ * @param {"copied" | ""} state
59
+ */
60
+ function flash(state) {
61
+ const element = dock.element
62
+ if (!element) return
63
+ element.dataset.state = state
64
+ if (resetTimer) clearTimeout(resetTimer)
65
+ resetTimer = setTimeout(() => {
66
+ element.dataset.state = ""
67
+ resetTimer = null
68
+ }, 1200)
69
+ }
70
+ },
71
+ }
72
+ }
@@ -0,0 +1,21 @@
1
+ .docserve-dock__button .docserve-icon-check {
2
+ display: none;
3
+ }
4
+
5
+ .docserve-dock__button[data-state="copied"] {
6
+ color: #16a34a;
7
+ }
8
+
9
+ .docserve-dock__button[data-state="copied"] .docserve-icon-copy {
10
+ display: none;
11
+ }
12
+
13
+ .docserve-dock__button[data-state="copied"] .docserve-icon-check {
14
+ display: block;
15
+ }
16
+
17
+ @media (prefers-color-scheme: dark) {
18
+ .docserve-dock__button[data-state="copied"] {
19
+ color: #4ade80;
20
+ }
21
+ }
@@ -0,0 +1,24 @@
1
+ // Dock link back to the gallery.
2
+ import { iconSvg } from "../../widget/lib/dom.js"
3
+ import ICON_HOME_RAW from "~icons/lucide/house?raw&width=18&height=18"
4
+
5
+ const ICON_HOME = iconSvg(ICON_HOME_RAW)
6
+
7
+ /**
8
+ * @returns {DocservePlugin}
9
+ */
10
+ export function homePlugin() {
11
+ return {
12
+ id: "home",
13
+ setup(ctx) {
14
+ const dock = ctx.docks.register({
15
+ id: "home",
16
+ title: "Back to gallery",
17
+ icon: ICON_HOME,
18
+ href: "/",
19
+ category: "actions",
20
+ })
21
+ return () => dock.dispose()
22
+ },
23
+ }
24
+ }
@@ -0,0 +1,106 @@
1
+ // Dock toggle plus a left panel that lists every page, as rows or preview cards.
2
+ import { createElement } from "react"
3
+ import { createRoot } from "react-dom/client"
4
+ import { createDisposables } from "../../widget/lib/disposables.js"
5
+ import { ICON_CALENDAR, ICON_GRID, ICON_SIDEBAR } from "./icons.js"
6
+ import { SidebarModel } from "./model.js"
7
+ import { Sidebar } from "./ui/Sidebar.js"
8
+
9
+ /**
10
+ * @returns {DocservePlugin}
11
+ */
12
+ export function sidebarPlugin() {
13
+ return {
14
+ id: "sidebar",
15
+ setup(ctx) {
16
+ // state
17
+ const model = SidebarModel.fromContext(ctx)
18
+ // styles
19
+ ctx.ui.style("/__docserve/plugins/sidebar/sidebar.css")
20
+ // cleanup
21
+ const cleanup = createDisposables()
22
+
23
+ // mount
24
+ const host = ctx.ui.mountPoint()
25
+ const root = createRoot(host)
26
+ cleanup.add(() => {
27
+ root.unmount()
28
+ host.remove()
29
+ })
30
+ root.render(createElement(Sidebar, { model }))
31
+
32
+ // commands
33
+ registerCommands(ctx, model, cleanup)
34
+
35
+ // dock
36
+ const dock = ctx.docks.register({
37
+ id: "sidebar",
38
+ title: "Toggle sidebar",
39
+ icon: ICON_SIDEBAR,
40
+ category: "main",
41
+ order: -1,
42
+ command: "plugin:sidebar:toggle",
43
+ isActive: () => model.open.value,
44
+ })
45
+ cleanup.add(() => dock.dispose())
46
+ cleanup.add(model.open.subscribe(() => dock.update()))
47
+
48
+ // events
49
+ cleanup.add(ctx.events.on("pages:changed", (change) => {
50
+ if (change.pages.length || change.contentSetChanged)
51
+ model.load({ force: true })
52
+ }))
53
+ cleanup.add(ctx.events.on("live:connected", () => model.load({ force: true })))
54
+
55
+ model.load()
56
+
57
+ return cleanup.dispose
58
+ },
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Register every sidebar command; each one is unregistered by `cleanup`.
64
+ * @param {DocserveContext} ctx
65
+ * @param {SidebarModel} model
66
+ * @param {import("../../widget/lib/disposables.js").Disposables} cleanup
67
+ */
68
+ function registerCommands(ctx, model, cleanup) {
69
+ cleanup.add(ctx.commands.register({
70
+ id: "plugin:sidebar:toggle",
71
+ title: "Toggle sidebar",
72
+ category: "View",
73
+ icon: ICON_SIDEBAR,
74
+ keys: ["[", "Alt+B"],
75
+ run: () => model.setOpen(!model.open.value),
76
+ }))
77
+
78
+ cleanup.add(ctx.commands.register({
79
+ id: "plugin:sidebar:close",
80
+ title: "Close sidebar",
81
+ keys: ["Escape"],
82
+ showInPalette: false,
83
+ when: () => model.open.value,
84
+ run: () => model.setOpen(false),
85
+ }))
86
+
87
+ cleanup.add(ctx.commands.register({
88
+ id: "plugin:sidebar:view",
89
+ title: "Toggle sidebar view",
90
+ category: "View",
91
+ icon: ICON_GRID,
92
+ keys: ["v"],
93
+ when: () => model.open.value,
94
+ run: () => model.toggleView(),
95
+ }))
96
+
97
+ cleanup.add(ctx.commands.register({
98
+ id: "plugin:sidebar:grouping",
99
+ title: "Cycle sidebar grouping",
100
+ category: "View",
101
+ icon: ICON_CALENDAR,
102
+ keys: ["m"],
103
+ when: () => model.open.value && model.view.value !== "tree",
104
+ run: () => model.toggleGrouping(),
105
+ }))
106
+ }
@@ -0,0 +1,108 @@
1
+ // Month buckets for the sidebar page list.
2
+
3
+ /**
4
+ * @typedef {object} PageGroup
5
+ * @property {string} key Month `YYYY-MM`, or folder path with a trailing slash.
6
+ * @property {string} label Heading text, e.g. `2026/04`, `guides/` or `/`.
7
+ * @property {DocPage[]} pages
8
+ */
9
+
10
+ const NO_DATE = "No date"
11
+
12
+ /**
13
+ * Local-time month key for a timestamp.
14
+ * @param {number | string} value Unix seconds, or a parseable date string.
15
+ * @returns {string}
16
+ */
17
+ export function monthKey(value) {
18
+ const date = toDate(value)
19
+ if (!date) return ""
20
+ const year = date.getFullYear()
21
+ const month = String(date.getMonth() + 1).padStart(2, "0")
22
+ return `${year}-${month}`
23
+ }
24
+
25
+ /**
26
+ * Heading text for a month key.
27
+ * @param {string} key
28
+ * @returns {string}
29
+ */
30
+ export function monthLabel(key) {
31
+ const match = /^(\d{4})-(\d{2})$/.exec(key)
32
+ if (!match) return NO_DATE
33
+ return `${match[1]}/${match[2]}`
34
+ }
35
+
36
+ /**
37
+ * Partition pages into month groups, newest first. Order inside a group is
38
+ * preserved; pages without a valid date go last.
39
+ * @param {DocPage[]} pages
40
+ * @returns {PageGroup[]}
41
+ */
42
+ export function groupByMonth(pages) {
43
+ /** @type {Map<string, PageGroup>} */
44
+ const groups = new Map()
45
+
46
+ for (const page of pages) {
47
+ const key = monthKey(page.created)
48
+ let group = groups.get(key)
49
+ if (!group) {
50
+ group = { key, label: key ? monthLabel(key) : NO_DATE, pages: [] }
51
+ groups.set(key, group)
52
+ }
53
+ group.pages.push(page)
54
+ }
55
+
56
+ return [...groups.values()].sort((a, b) => {
57
+ if (!a.key) return b.key ? 1 : 0
58
+ if (!b.key) return -1
59
+ return b.key.localeCompare(a.key)
60
+ })
61
+ }
62
+
63
+ /**
64
+ * @param {number | string} value
65
+ * @returns {Date | null}
66
+ */
67
+ function toDate(value) {
68
+ const date = new Date(typeof value === "number" ? value * 1000 : value)
69
+ return Number.isNaN(date.getTime()) ? null : date
70
+ }
71
+
72
+ /**
73
+ * Folder that directly holds a page, with a trailing slash; `""` for the root.
74
+ * @param {string} url Root-absolute page URL, e.g. `/guides/intro.html`.
75
+ * @returns {string}
76
+ */
77
+ export function folderOf(url) {
78
+ const parts = url.split("/").filter(Boolean)
79
+ parts.pop()
80
+ return parts.length ? `${parts.join("/")}/` : ""
81
+ }
82
+
83
+ /**
84
+ * Group pages by the folder that directly holds them, root first. Folders
85
+ * without pages of their own never appear, and groups do not nest.
86
+ * @param {DocPage[]} pages
87
+ * @returns {PageGroup[]}
88
+ */
89
+ export function groupByFolder(pages) {
90
+ /** @type {Map<string, PageGroup>} */
91
+ const groups = new Map()
92
+
93
+ for (const page of pages) {
94
+ const key = folderOf(page.url)
95
+ let group = groups.get(key)
96
+ if (!group) {
97
+ group = { key, label: key || "/", pages: [] }
98
+ groups.set(key, group)
99
+ }
100
+ group.pages.push(page)
101
+ }
102
+
103
+ return [...groups.values()].sort((a, b) => {
104
+ if (!a.key) return b.key ? -1 : 0
105
+ if (!b.key) return 1
106
+ return a.key.localeCompare(b.key, undefined, { numeric: true, sensitivity: "base" })
107
+ })
108
+ }
@@ -0,0 +1,25 @@
1
+ // Inline SVG for the dock item, the header and the view tabs.
2
+ import { iconSvg } from "../../widget/lib/dom.js"
3
+ import ICON_CALENDAR_RAW from "~icons/lucide/calendar?raw&width=18&height=18"
4
+ import ICON_CHECK_RAW from "~icons/lucide/check?raw&width=18&height=18"
5
+ import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=18&height=18"
6
+ import ICON_CHEVRON_DOWN_RAW from "~icons/lucide/chevron-down?raw&width=18&height=18"
7
+ import ICON_FILE_RAW from "~icons/lucide/file-text?raw&width=18&height=18"
8
+ import ICON_FOLDER_RAW from "~icons/mdi/folder-open-outline?raw&width=18&height=18"
9
+ import ICON_GRID_RAW from "~icons/lucide/layout-grid?raw&width=18&height=18"
10
+ import ICON_LIST_RAW from "~icons/lucide/list?raw&width=18&height=18"
11
+ import ICON_UNGROUP_RAW from "~icons/lucide/ungroup?raw&width=18&height=18"
12
+ import ICON_SIDEBAR_RAW from "~icons/lucide/panel-left?raw&width=18&height=18"
13
+ import ICON_TREE_RAW from "~icons/lucide/list-tree?raw&width=18&height=18"
14
+
15
+ export const ICON_CALENDAR = iconSvg(ICON_CALENDAR_RAW)
16
+ export const ICON_CHECK = iconSvg(ICON_CHECK_RAW)
17
+ export const ICON_CHEVRON = iconSvg(ICON_CHEVRON_RAW)
18
+ export const ICON_CHEVRON_DOWN = iconSvg(ICON_CHEVRON_DOWN_RAW)
19
+ export const ICON_FILE = iconSvg(ICON_FILE_RAW)
20
+ export const ICON_FOLDER = iconSvg(ICON_FOLDER_RAW)
21
+ export const ICON_GRID = iconSvg(ICON_GRID_RAW)
22
+ export const ICON_LIST = iconSvg(ICON_LIST_RAW)
23
+ export const ICON_UNGROUP = iconSvg(ICON_UNGROUP_RAW)
24
+ export const ICON_SIDEBAR = iconSvg(ICON_SIDEBAR_RAW)
25
+ export const ICON_TREE = iconSvg(ICON_TREE_RAW)