-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-framework.js
More file actions
279 lines (242 loc) · 10.1 KB
/
docs-framework.js
File metadata and controls
279 lines (242 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* docs-framework.js — Shared documentation site engine
Source of truth: streamdeck-dev/docs-framework/
Reads JSON config from #docs-config, builds sidebar + content area,
loads markdown via marked.js with hash-based routing.
Supports file:// via XMLHttpRequest fallback. */
(function () {
'use strict';
// ---- Read config ----
const configEl = document.getElementById('docs-config');
if (!configEl) {
document.body.innerHTML = '<p style="color:#f48771;padding:24px">docs-framework: missing <code><script id="docs-config"></code> block.</p>';
return;
}
let config;
try {
config = JSON.parse(configEl.textContent);
} catch (e) {
document.body.innerHTML = `<p style="color:#f48771;padding:24px">docs-framework: invalid JSON in #docs-config — ${e.message}</p>`;
return;
}
const title = config.title || 'Docs';
const basePath = config.basePath ? config.basePath.replace(/\/$/, '') + '/' : '';
const defaultPage = config.defaultPage || config.sections?.[0]?.items?.[0]?.id || 'readme';
const sections = config.sections || [];
const headerLinks = config.headerLinks || null;
const multiSection = sections.length > 1;
// Build a lookup: id → file
const pageMap = {};
for (const section of sections) {
for (const item of section.items || []) {
pageMap[item.id] = item.file;
}
}
// ---- Build DOM ----
// Optional header
if (headerLinks) {
document.body.classList.add('docs-has-header');
const header = document.createElement('header');
header.className = 'docs-header';
header.innerHTML = `<a class="docs-header-title" href="${headerLinks[0]?.href || '#'}">${escapeHtml(title)}</a>`;
const nav = document.createElement('nav');
nav.className = 'docs-header-nav';
nav.setAttribute('aria-label', 'Main navigation');
for (const link of headerLinks) {
const a = document.createElement('a');
a.href = link.href;
a.textContent = link.label;
if (link.active) a.classList.add('active');
if (link.external) {
a.target = '_blank';
a.rel = 'noopener noreferrer';
}
nav.appendChild(a);
}
header.appendChild(nav);
document.body.appendChild(header);
}
// Layout container
const layout = document.createElement('div');
layout.className = 'docs-layout';
// Sidebar
const sidebar = document.createElement('nav');
sidebar.className = 'docs-sidebar';
sidebar.setAttribute('aria-label', 'Documentation navigation');
if (multiSection) {
// Collapsible <details> per section
for (const section of sections) {
const details = document.createElement('details');
details.className = 'docs-nav-section';
details.dataset.section = section.label.toLowerCase().replace(/\s+/g, '-');
const summary = document.createElement('summary');
summary.innerHTML = `<h3>${escapeHtml(section.label)}</h3>`;
details.appendChild(summary);
const linksDiv = document.createElement('div');
linksDiv.className = 'nav-links';
for (const item of section.items || []) {
linksDiv.appendChild(createNavLink(item));
}
details.appendChild(linksDiv);
sidebar.appendChild(details);
// Animate open/close
setupDetailsAnimation(details, linksDiv);
}
} else {
// Flat list with heading
const section = sections[0];
if (section) {
const h2 = document.createElement('h2');
h2.textContent = section.label;
sidebar.appendChild(h2);
for (const item of section.items || []) {
sidebar.appendChild(createNavLink(item));
}
}
}
// Main content
const main = document.createElement('main');
main.className = 'docs-content';
main.id = 'docs-content';
main.setAttribute('aria-live', 'polite');
main.innerHTML = '<span class="docs-loading">Loading\u2026</span>';
layout.appendChild(sidebar);
layout.appendChild(main);
document.body.appendChild(layout);
// ---- Configure marked ----
if (typeof marked === 'undefined') {
main.innerHTML = '<p class="docs-error">docs-framework: <code>marked.js</code> not loaded. Add <code><script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></code> before docs-framework.js.</p>';
return;
}
marked.setOptions({ gfm: true, breaks: false });
marked.use({
renderer: {
code({ text, lang }) {
return `<pre tabindex="0"><code class="language-${lang || 'text'}">${escapeHtml(text)}</code></pre>`;
},
image({ href, title, text }) {
const alt = text || title || 'Image';
const titleAttr = title ? ` title="${escapeHtml(title)}"` : '';
return `<img src="${href}" alt="${escapeHtml(alt)}"${titleAttr} loading="lazy">`;
},
link({ href, title, tokens }) {
const text = this.parser.parseInline(tokens);
const titleAttr = title ? ` title="${escapeHtml(title)}"` : '';
const isExternal = href && href.startsWith('http') && !href.includes(location.hostname);
if (isExternal) {
return `<a href="${href}"${titleAttr} target="_blank" rel="noopener noreferrer">${text}<span class="visually-hidden"> (opens in new tab)</span></a>`;
}
return `<a href="${href || ''}"${titleAttr}>${text}</a>`;
}
}
});
// ---- Navigation ----
function getPageId() {
const hash = (location.hash || '').slice(1);
return hash || defaultPage;
}
function setActiveLink(id) {
sidebar.querySelectorAll('a').forEach(a => {
a.classList.toggle('active', a.dataset.page === id);
});
// Auto-expand parent details
if (multiSection) {
sidebar.querySelectorAll('details').forEach(d => d.open = false);
const activeLink = sidebar.querySelector(`a[data-page="${id}"]`);
if (activeLink) {
const details = activeLink.closest('details');
if (details) details.open = true;
}
}
}
async function loadPage(id) {
const file = pageMap[id];
if (!file) {
main.innerHTML = `<p class="docs-error">Unknown page: ${escapeHtml(id)}</p>`;
return;
}
main.innerHTML = '<span class="docs-loading">Loading\u2026</span>';
setActiveLink(id);
try {
const md = await loadMarkdown(basePath + file);
main.innerHTML = marked.parse(md);
// Wrap tables for horizontal scrolling
main.querySelectorAll('table').forEach(table => {
const wrapper = document.createElement('div');
wrapper.className = 'table-wrap';
wrapper.setAttribute('tabindex', '0');
table.parentNode.insertBefore(wrapper, table);
wrapper.appendChild(table);
});
// Update document title
const titleMatch = md.match(/^#\s+(.+)$/m);
document.title = titleMatch ? `${titleMatch[1]} \u2014 ${title}` : title;
// Scroll to top
window.scrollTo(0, 0);
} catch (err) {
main.innerHTML = `<p class="docs-error">Failed to load ${escapeHtml(basePath + file)}: ${escapeHtml(err.message)}</p>
<p style="margin-top:8px;color:var(--docs-text-muted);font-size:13px">If using <code>file://</code>, try: <code>npx http-server -p 3456 -c-1</code></p>`;
}
}
// Sidebar link clicks
sidebar.addEventListener('click', (e) => {
const link = e.target.closest('a[data-page]');
if (!link) return;
e.preventDefault();
const id = link.dataset.page;
location.hash = id;
});
window.addEventListener('hashchange', () => loadPage(getPageId()));
// Initial load
loadPage(getPageId());
// ---- Helpers ----
function createNavLink(item) {
const a = document.createElement('a');
a.href = '#' + item.id;
a.textContent = item.label;
a.dataset.page = item.id;
if (item.sub) a.classList.add('sub');
return a;
}
function setupDetailsAnimation(details, content) {
details.addEventListener('click', (e) => {
if (!e.target.closest('summary')) return;
e.preventDefault();
if (details.open) {
content.style.animation = 'docsSlideUp 0.2s ease-out forwards';
content.addEventListener('animationend', () => {
details.open = false;
content.style.animation = '';
}, { once: true });
} else {
details.open = true;
content.style.animation = 'none';
content.offsetHeight; // reflow
content.style.animation = 'docsSlideDown 0.2s ease-out';
}
});
}
async function loadMarkdown(path) {
try {
const url = new URL(path, location.href);
const res = await fetch(url);
if (!res.ok) throw new Error(res.statusText);
return await res.text();
} catch (fetchErr) {
// file:// fallback via XMLHttpRequest
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.onload = () => (xhr.status === 200 || xhr.status === 0)
? resolve(xhr.responseText)
: reject(new Error(xhr.statusText || 'Load failed'));
xhr.onerror = () => reject(fetchErr);
xhr.send();
});
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
})();