@kenjura/ursa 0.52.0 → 0.53.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 0.53.0
2
+ 2025-01-01
3
+
4
+ ### Menu Size Optimization
5
+ - **External Menu JSON**: Menu data is now stored in `/public/menu-data.json` instead of being embedded in every HTML file. This dramatically reduces HTML file sizes for sites with large folder structures (e.g., from 2-3MB per file down to ~50KB).
6
+ - **Async Menu Loading**: Menu data is fetched asynchronously after page render, showing a "Loading menu..." indicator until ready.
7
+ - **Debug Fields Removed**: Menu JSON no longer includes debug/inactive fields, reducing JSON size further.
8
+ - **Gzip Compression**: Development server now uses gzip compression for all responses, significantly reducing transfer size for JSON and HTML files.
9
+
1
10
  # 0.52.0
2
11
  2025-12-21
3
12
 
package/meta/menu.js CHANGED
@@ -2,19 +2,12 @@ document.addEventListener('DOMContentLoaded', () => {
2
2
  const navMain = document.querySelector('nav#nav-main');
3
3
  if (!navMain) return;
4
4
 
5
- // Load menu data from embedded JSON
6
- const menuDataScript = document.getElementById('menu-data');
7
- if (!menuDataScript) return;
5
+ // State - menu data will be loaded asynchronously
6
+ let menuData = null;
7
+ let menuDataLoaded = false;
8
+ let menuDataLoading = false;
8
9
 
9
- let menuData;
10
- try {
11
- menuData = JSON.parse(menuDataScript.textContent);
12
- } catch (e) {
13
- console.error('Failed to parse menu data:', e);
14
- return;
15
- }
16
-
17
- // Load menu config from embedded JSON (contains openMenuItems)
10
+ // Load menu config from embedded JSON (contains openMenuItems) - this is small, so it's embedded
18
11
  const menuConfigScript = document.getElementById('menu-config');
19
12
  let menuConfig = { openMenuItems: [] };
20
13
  if (menuConfigScript) {
@@ -39,9 +32,39 @@ document.addEventListener('DOMContentLoaded', () => {
39
32
 
40
33
  // Helper to check if we're on mobile
41
34
  const isMobile = () => window.matchMedia('(max-width: 800px)').matches;
35
+
36
+ /**
37
+ * Load menu data from external JSON file
38
+ * This is done asynchronously to avoid blocking page render
39
+ */
40
+ async function loadMenuData() {
41
+ if (menuDataLoaded || menuDataLoading) return;
42
+ menuDataLoading = true;
43
+
44
+ try {
45
+ const response = await fetch('/public/menu-data.json');
46
+ if (!response.ok) {
47
+ throw new Error(`HTTP ${response.status}`);
48
+ }
49
+ menuData = await response.json();
50
+ menuDataLoaded = true;
51
+
52
+ // Re-render menu now that we have full data
53
+ initializeFromCurrentPage();
54
+ } catch (error) {
55
+ console.error('Failed to load menu data:', error);
56
+ menuDataLoaded = true; // Mark as loaded to prevent retries
57
+ } finally {
58
+ menuDataLoading = false;
59
+ }
60
+ }
61
+
62
+ // Start loading menu data immediately
63
+ loadMenuData();
42
64
 
43
65
  // Get items at a specific path
44
66
  function getItemsAtPath(path) {
67
+ if (!menuData) return [];
45
68
  let items = menuData;
46
69
  for (const segment of path) {
47
70
  const folder = items.find(item => item.path === (path.slice(0, path.indexOf(segment) + 1).join('/') || segment));
@@ -56,6 +79,7 @@ document.addEventListener('DOMContentLoaded', () => {
56
79
 
57
80
  // Find item by path
58
81
  function findItemByPath(pathString) {
82
+ if (!menuData) return null;
59
83
  const segments = pathString.split('/').filter(Boolean);
60
84
  let items = menuData;
61
85
  let item = null;
@@ -98,6 +122,12 @@ document.addEventListener('DOMContentLoaded', () => {
98
122
 
99
123
  // Render menu at current path
100
124
  function renderMenu() {
125
+ // Wait for menu data to load
126
+ if (!menuData) {
127
+ menuContainer.innerHTML = '<li class="menu-loading">Loading menu...</li>';
128
+ return;
129
+ }
130
+
101
131
  // Get items for current level (level 1)
102
132
  let level1Items;
103
133
  if (currentPath.length === 0) {
@@ -421,5 +451,6 @@ document.addEventListener('DOMContentLoaded', () => {
421
451
  renderMenu();
422
452
  }
423
453
 
424
- initializeFromCurrentPage();
454
+ // Initial render shows loading state, then loadMenuData() will call initializeFromCurrentPage() when ready
455
+ renderMenu();
425
456
  });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@kenjura/ursa",
3
3
  "author": "Andrew London <andrew@kenjura.com>",
4
4
  "type": "module",
5
- "version": "0.52.0",
5
+ "version": "0.53.0",
6
6
  "description": "static site generator from MD/wikitext/YML",
7
7
  "main": "lib/index.js",
8
8
  "bin": {
@@ -24,6 +24,7 @@
24
24
  "url": "git+https://github.com/kenjura/ursa.git"
25
25
  },
26
26
  "dependencies": {
27
+ "compression": "^1.7.4",
27
28
  "directory-tree": "^3.3.2",
28
29
  "express": "^4.18.2",
29
30
  "fs-extra": "^10.1.0",
@@ -132,7 +132,8 @@ function resolveHref(rawHref, validPaths) {
132
132
  }
133
133
 
134
134
  // Build a flat tree structure with path info for JS navigation
135
- function buildMenuData(tree, source, validPaths, parentPath = '') {
135
+ // Set includeDebug=false to exclude debug fields and reduce JSON size
136
+ function buildMenuData(tree, source, validPaths, parentPath = '', includeDebug = true) {
136
137
  const items = [];
137
138
 
138
139
  // Files to hide from menu by default
@@ -192,14 +193,21 @@ function buildMenuData(tree, source, validPaths, parentPath = '') {
192
193
  label,
193
194
  path: folderPath,
194
195
  href,
195
- inactive,
196
- debug,
197
196
  hasChildren,
198
197
  icon,
199
198
  };
200
199
 
200
+ // Only include debug and inactive fields if requested (for smaller JSON)
201
+ if (includeDebug) {
202
+ menuItem.inactive = inactive;
203
+ menuItem.debug = debug;
204
+ } else if (inactive) {
205
+ // Only include inactive if true (to save space)
206
+ menuItem.inactive = true;
207
+ }
208
+
201
209
  if (hasChildren) {
202
- menuItem.children = buildMenuData(item, source, validPaths, folderPath);
210
+ menuItem.children = buildMenuData(item, source, validPaths, folderPath, includeDebug);
203
211
  }
204
212
 
205
213
  items.push(menuItem);
@@ -218,7 +226,9 @@ export async function getAutomenu(source, validPaths) {
218
226
  const tree = dirTree(source, {
219
227
  exclude: /[\/\\]\.|node_modules/, // Exclude hidden folders (starting with .) and node_modules
220
228
  });
221
- const menuData = buildMenuData(tree, source, validPaths);
229
+
230
+ // Build menu data WITHOUT debug fields for smaller JSON
231
+ const menuData = buildMenuData(tree, source, validPaths, '', false);
222
232
 
223
233
  // Get root config for openMenuItems setting
224
234
  const rootConfig = getRootConfig(source);
@@ -227,14 +237,11 @@ export async function getAutomenu(source, validPaths) {
227
237
  // Add home item with resolved href
228
238
  const homeResolved = resolveHref('/', validPaths);
229
239
  const fullMenuData = [
230
- { label: 'Home', path: '', href: homeResolved.href, inactive: homeResolved.inactive, debug: homeResolved.debug, hasChildren: false, icon: `<span class="menu-icon">${HOME_ICON}</span>` },
240
+ { label: 'Home', path: '', href: homeResolved.href, hasChildren: false, icon: `<span class="menu-icon">${HOME_ICON}</span>` },
231
241
  ...menuData
232
242
  ];
233
243
 
234
- // Embed the menu data as JSON for JavaScript to use
235
- const menuDataScript = `<script type="application/json" id="menu-data">${JSON.stringify(fullMenuData)}</script>`;
236
-
237
- // Embed the openMenuItems config as separate JSON
244
+ // Embed the openMenuItems config as JSON (small, safe to embed)
238
245
  const menuConfigScript = `<script type="application/json" id="menu-config">${JSON.stringify({ openMenuItems })}</script>`;
239
246
 
240
247
  // Render the breadcrumb header (hidden by default, shown when navigating)
@@ -245,10 +252,14 @@ export async function getAutomenu(source, validPaths) {
245
252
  <span class="menu-current-path"></span>
246
253
  </div>`;
247
254
 
248
- // Render the initial menu (root level)
255
+ // Render the initial menu (root level only - children loaded from external JSON)
249
256
  const menuHtml = renderMenuLevel(fullMenuData, 0);
250
257
 
251
- return `${menuDataScript}${menuConfigScript}${breadcrumbHtml}<ul class="menu-level" data-level="0">${menuHtml}</ul>`;
258
+ // Return both the HTML for embedding and the full menu data for the static JSON file
259
+ return {
260
+ html: `${menuConfigScript}${breadcrumbHtml}<ul class="menu-level" data-level="0">${menuHtml}</ul>`,
261
+ menuData: fullMenuData
262
+ };
252
263
  }
253
264
 
254
265
  function renderMenuLevel(items, level) {
@@ -323,7 +323,9 @@ export async function generate({
323
323
  const validPaths = buildValidPaths(allSourceFilenamesThatAreArticles, source, allSourceFilenamesThatAreDirectories);
324
324
  progress.log(`Built ${validPaths.size} valid paths for link validation`);
325
325
 
326
- const menu = await getMenu(allSourceFilenames, source, validPaths);
326
+ const menuResult = await getMenu(allSourceFilenames, source, validPaths);
327
+ const menu = menuResult.html;
328
+ const menuData = menuResult.menuData;
327
329
 
328
330
  // Get and increment build ID from .ursa.json
329
331
  const buildId = getAndIncrementBuildId(resolve(_source));
@@ -549,6 +551,13 @@ export async function generate({
549
551
  progress.log(`Writing search index with ${searchIndex.length} entries`);
550
552
  await outputFile(searchIndexPath, JSON.stringify(searchIndex));
551
553
 
554
+ // Write menu data as a separate JSON file (not embedded in each page)
555
+ // This dramatically reduces HTML file sizes for large sites
556
+ const menuDataPath = join(output, 'public', 'menu-data.json');
557
+ const menuDataJson = JSON.stringify(menuData);
558
+ progress.log(`Writing menu data (${(menuDataJson.length / 1024).toFixed(1)} KB)`);
559
+ await outputFile(menuDataPath, menuDataJson);
560
+
552
561
  // Process directory indices with batched concurrency
553
562
  const totalDirs = allSourceFilenamesThatAreDirectories.length;
554
563
  let processedDirs = 0;
@@ -1084,23 +1093,12 @@ async function getTemplates(meta) {
1084
1093
  async function getMenu(allSourceFilenames, source, validPaths) {
1085
1094
  // todo: handle various incarnations of menu filename
1086
1095
 
1087
- const rawMenu = await getAutomenu(source, validPaths);
1088
- const menuBody = renderFile({ fileContents: rawMenu, type: ".md" });
1089
- return menuBody;
1090
-
1091
- // const allMenus = allSourceFilenames.filter((filename) =>
1092
- // filename.match(/_?menu\.(html|yml|md|txt)/)
1093
- // );
1094
- // console.log({ allMenus });
1095
- // if (allMenus.length === 0) return "";
1096
-
1097
- // // pick best menu...TODO: actually apply logic here
1098
- // const bestMenu = allMenus[0];
1099
- // const rawBody = await readFile(bestMenu, "utf8");
1100
- // const type = parse(bestMenu).ext;
1101
- // const menuBody = renderFile({ fileContents: rawBody, type });
1102
-
1103
- // return menuBody;
1096
+ const menuResult = await getAutomenu(source, validPaths);
1097
+ const menuBody = renderFile({ fileContents: menuResult.html, type: ".md" });
1098
+ return {
1099
+ html: menuBody,
1100
+ menuData: menuResult.menuData
1101
+ };
1104
1102
  }
1105
1103
 
1106
1104
  async function getTransformedMetadata(dirname, metadata) {
package/src/serve.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import express from "express";
2
+ import compression from "compression";
2
3
  import watch from "node-watch";
3
4
  import { generate, regenerateSingleFile, clearWatchCache } from "./jobs/generate.js";
4
5
  import { join, resolve, dirname } from "path";
@@ -252,6 +253,15 @@ export async function serve({
252
253
  function serveFiles(outputDir, port = 8080) {
253
254
  const app = express();
254
255
 
256
+ // Enable gzip compression for all responses
257
+ // This significantly reduces transfer size for JSON and HTML files
258
+ app.use(compression({
259
+ // Compress everything over 1KB
260
+ threshold: 1024,
261
+ // Use default compression level (good balance of speed vs size)
262
+ level: 6
263
+ }));
264
+
255
265
  app.use(
256
266
  express.static(outputDir, { extensions: ["html"], index: "index.html" })
257
267
  );