@noy4/docserve 0.3.3 → 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/dist/widget/index.js +3266 -2618
- package/package.json +2 -2
- 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/sidebar/client.js +1 -0
- package/src/plugins/sidebar/icons.js +1 -1
- 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/widget/index.js +9 -1
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/widget.css +15 -2
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
const DEFAULT_ROOTS = ["main", "article", ".container"]
|
|
2
|
+
const DEFAULT_EXCLUDED = "nav, aside, header, footer"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {{ element: HTMLHeadingElement, level: number, children: TocNode[] }} TocNode
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Content root: `data-*` root selector, else the first of main / article / .container, else body.
|
|
10
|
+
* @param {Document} [doc]
|
|
11
|
+
* @param {string} [attribute]
|
|
12
|
+
* @param {string[]} [selectors]
|
|
13
|
+
* @returns {HTMLElement | null}
|
|
14
|
+
*/
|
|
15
|
+
export function findRoot(doc = document, attribute = "data-toc-root", selectors = DEFAULT_ROOTS) {
|
|
16
|
+
const source = doc.body ?? doc.documentElement
|
|
17
|
+
if (!source) return null
|
|
18
|
+
|
|
19
|
+
const selector = source.getAttribute(attribute)
|
|
20
|
+
if (selector) {
|
|
21
|
+
try {
|
|
22
|
+
const custom = doc.querySelector(selector)
|
|
23
|
+
if (custom) return /** @type {HTMLElement} */ (custom)
|
|
24
|
+
} catch {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for (const candidate of selectors) {
|
|
28
|
+
const element = doc.querySelector(candidate)
|
|
29
|
+
if (element) return /** @type {HTMLElement} */ (element)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return doc.body
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Headings inside `root` whose level falls in `range`, in document order.
|
|
37
|
+
* @param {HTMLElement | null} root
|
|
38
|
+
* @param {[number, number] | null} range
|
|
39
|
+
* @param {string} [excludedSelector]
|
|
40
|
+
* @returns {HTMLHeadingElement[]}
|
|
41
|
+
*/
|
|
42
|
+
export function collectHeadings(root, range, excludedSelector = DEFAULT_EXCLUDED) {
|
|
43
|
+
if (!root || !range) return []
|
|
44
|
+
|
|
45
|
+
const [min, max] = range
|
|
46
|
+
const headings = [...root.querySelectorAll("h1, h2, h3, h4, h5, h6")].filter((element) => {
|
|
47
|
+
const level = Number(element.tagName[1])
|
|
48
|
+
if (level < min || level > max) return false
|
|
49
|
+
if (!element.textContent?.trim()) return false
|
|
50
|
+
if (isHidden(element)) return false
|
|
51
|
+
|
|
52
|
+
const excluded = excludedSelector ? element.closest(excludedSelector) : null
|
|
53
|
+
return !excluded || excluded === root || !root.contains(excluded)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
return /** @type {HTMLHeadingElement[]} */ (headings)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Give every heading a stable, unique id; existing ids are kept.
|
|
61
|
+
* @param {HTMLHeadingElement[]} headings
|
|
62
|
+
* @param {Document} [doc]
|
|
63
|
+
*/
|
|
64
|
+
export function ensureIds(headings, doc = document) {
|
|
65
|
+
const reserved = new Set()
|
|
66
|
+
for (const element of doc.querySelectorAll("[id]")) {
|
|
67
|
+
if (element.id) reserved.add(element.id)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const heading of headings) {
|
|
71
|
+
if (heading.id) continue
|
|
72
|
+
|
|
73
|
+
const base = slugify(heading.textContent ?? "")
|
|
74
|
+
let id = base
|
|
75
|
+
let index = 2
|
|
76
|
+
while (!id || reserved.has(id)) id = base ? `${base}-${index++}` : `section-${index++}`
|
|
77
|
+
|
|
78
|
+
heading.id = id
|
|
79
|
+
reserved.add(id)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* `Hello, World!` → `hello-world`.
|
|
85
|
+
* @param {string} text
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
88
|
+
export function slugify(text) {
|
|
89
|
+
return text.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "")
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Nest headings by level: a heading becomes a child while its level is greater
|
|
94
|
+
* than the previous one.
|
|
95
|
+
* @param {HTMLHeadingElement[]} headings
|
|
96
|
+
* @returns {TocNode[]}
|
|
97
|
+
*/
|
|
98
|
+
export function buildTree(headings) {
|
|
99
|
+
/** @type {TocNode[]} */
|
|
100
|
+
const roots = []
|
|
101
|
+
/** @type {TocNode[]} */
|
|
102
|
+
const stack = []
|
|
103
|
+
|
|
104
|
+
for (const element of headings) {
|
|
105
|
+
const node = { element, level: Number(element.tagName[1]), children: [] }
|
|
106
|
+
while (stack.length && stack[stack.length - 1].level >= node.level) stack.pop()
|
|
107
|
+
if (stack.length) stack[stack.length - 1].children.push(node)
|
|
108
|
+
else roots.push(node)
|
|
109
|
+
stack.push(node)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return roots
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param {Element} element
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
function isHidden(element) {
|
|
120
|
+
if (element.hasAttribute("hidden") || element.getAttribute("aria-hidden") === "true") return true
|
|
121
|
+
if (element.closest("details:not([open])")) return true
|
|
122
|
+
|
|
123
|
+
const view = element.ownerDocument.defaultView
|
|
124
|
+
if (!view) return false
|
|
125
|
+
|
|
126
|
+
for (let node = element; node; node = node.parentElement) {
|
|
127
|
+
const style = view.getComputedStyle(node)
|
|
128
|
+
if (style.display === "none" || style.visibility === "hidden") return true
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return false
|
|
132
|
+
}
|
package/src/widget/widget.css
CHANGED
|
@@ -31,7 +31,12 @@
|
|
|
31
31
|
/* Four corners: `data-corner` is persisted and rewritten by dragging. Left
|
|
32
32
|
corners follow the sidebar so the pill never sits under an open panel. */
|
|
33
33
|
:host([data-corner="bl"]) { right: auto; left: calc(var(--docserve-inset-l) + var(--docserve-panel-left, 0px)); }
|
|
34
|
-
:host([data-corner="
|
|
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
|
+
}
|
|
35
40
|
:host([data-corner="tl"]) {
|
|
36
41
|
right: auto;
|
|
37
42
|
bottom: auto;
|
|
@@ -58,13 +63,21 @@
|
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
html[data-docserve-widget] body {
|
|
61
|
-
transition: padding-left 0.18s ease;
|
|
66
|
+
transition: padding-left 0.18s ease, padding-right 0.18s ease;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
html[data-docserve-panel-left] body {
|
|
65
70
|
padding-left: var(--docserve-panel-left) !important;
|
|
66
71
|
}
|
|
67
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
|
+
|
|
68
81
|
html[data-docserve-palette] {
|
|
69
82
|
overflow: hidden;
|
|
70
83
|
}
|