kss-modern 1.2.2 → 1.3.0-beta.1

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.
@@ -16,11 +16,11 @@ import githubLightDefault from '@shikijs/themes/github-light-default';
16
16
  import MarkdownItAsync from 'markdown-it-async';
17
17
  import { createHighlighterCore } from 'shiki/core';
18
18
  import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
19
+ import { existsSync, readFileSync } from 'node:fs';
19
20
  import os from 'node:os';
20
21
  import { Worker } from 'node:worker_threads';
21
22
  import fs$1 from 'node:fs/promises';
22
23
  import { objectEntries } from '@antfu/utils';
23
- import { readFileSync } from 'node:fs';
24
24
  import chokidar from 'chokidar';
25
25
  import 'pug';
26
26
  import 'consola';
@@ -390,7 +390,7 @@ function shiftHeadingLevels(markdownContent, rootHeadingLevel) {
390
390
  }
391
391
  return markdownContent;
392
392
  }
393
- async function parseMarkdown(data) {
393
+ function getMarkdownRenderer() {
394
394
  if (!md) {
395
395
  md = MarkdownItAsync({ linkify: true, typographer: true });
396
396
  md.use(markdownItComponent, {
@@ -411,6 +411,29 @@ async function parseMarkdown(data) {
411
411
  )
412
412
  );
413
413
  }
414
+ return md;
415
+ }
416
+ const RENDER_CACHE_MAX_ENTRIES = 2e3;
417
+ const renderCache = /* @__PURE__ */ new Map();
418
+ async function renderMarkdownCached(source) {
419
+ const cached = renderCache.get(source);
420
+ if (cached !== void 0) {
421
+ return cached;
422
+ }
423
+ const promise = getMarkdownRenderer().renderAsync(source);
424
+ renderCache.set(source, promise);
425
+ promise.catch(() => {
426
+ if (renderCache.get(source) === promise)
427
+ renderCache.delete(source);
428
+ });
429
+ if (renderCache.size > RENDER_CACHE_MAX_ENTRIES) {
430
+ const oldest = renderCache.keys().next().value;
431
+ if (oldest !== void 0 && oldest !== source)
432
+ renderCache.delete(oldest);
433
+ }
434
+ return promise;
435
+ }
436
+ async function parseMarkdown(data) {
414
437
  if ("filePath" in data) {
415
438
  const doesFileExist = fs.existsSync(data.filePath);
416
439
  if (!doesFileExist) {
@@ -419,12 +442,12 @@ async function parseMarkdown(data) {
419
442
  }
420
443
  let fileContent = await fs.readFile(data.filePath, "utf8");
421
444
  fileContent = shiftHeadingLevels(fileContent, data.rootHeadingLevel);
422
- const parsedMarkdown = await md.renderAsync(fileContent);
445
+ const parsedMarkdown = await renderMarkdownCached(fileContent);
423
446
  return parsedMarkdown;
424
447
  } else {
425
448
  let fileContent = data.markdownContent.replace("Markdown:", "");
426
449
  fileContent = shiftHeadingLevels(fileContent, data.rootHeadingLevel);
427
- return await md.renderAsync(fileContent);
450
+ return await renderMarkdownCached(fileContent);
428
451
  }
429
452
  }
430
453
 
@@ -1038,8 +1061,17 @@ function getPugDependencyGraph() {
1038
1061
  }
1039
1062
  const __filename$1 = fileURLToPath(import.meta.url);
1040
1063
  const __dirname$1 = path.dirname(__filename$1);
1041
- const workerFilePath = __dirname$1.includes("dist/") ? "./pug/worker.mjs" : "./worker.ts";
1042
- const workerFilePathResolved = path.resolve(__dirname$1, workerFilePath);
1064
+ function resolveWorkerFile() {
1065
+ const candidates = ["./worker.ts", "./worker.mjs", "./pug/worker.mjs"];
1066
+ for (const candidate of candidates) {
1067
+ const resolved = path.resolve(__dirname$1, candidate);
1068
+ if (existsSync(resolved))
1069
+ return resolved;
1070
+ }
1071
+ return void 0;
1072
+ }
1073
+ const workerFilePathResolved = resolveWorkerFile();
1074
+ let warnedMissingWorker = false;
1043
1075
  const slots = Array.from({ length: MAX_POOL_SIZE }, () => ({ tail: Promise.resolve(), active: 0 }));
1044
1076
  const allWorkers = /* @__PURE__ */ new Set();
1045
1077
  function slotIndexFor(id) {
@@ -1051,6 +1083,8 @@ function slotIndexFor(id) {
1051
1083
  return (hash >>> 0) % MAX_POOL_SIZE;
1052
1084
  }
1053
1085
  function spawnWorker(index) {
1086
+ if (!workerFilePathResolved)
1087
+ throw new Error("pug compile worker file could not be located");
1054
1088
  const worker = new Worker(workerFilePathResolved, { name: `pug-worker-${index}` });
1055
1089
  allWorkers.add(worker);
1056
1090
  return worker;
@@ -1185,7 +1219,11 @@ async function compileIds(mode, contentDir, repository, ids) {
1185
1219
  if (misses.length === 0)
1186
1220
  return clonedRepository;
1187
1221
  const errors = [];
1188
- const useInline = mode === "development" && misses.length <= INLINE_THRESHOLD;
1222
+ if (!workerFilePathResolved && misses.length > INLINE_THRESHOLD && !warnedMissingWorker) {
1223
+ warnedMissingWorker = true;
1224
+ logger.warn("Pug compile worker not found; compiling markup on the main thread. Rebuilds stay correct but are slower.");
1225
+ }
1226
+ const useInline = !workerFilePathResolved || mode === "development" && misses.length <= INLINE_THRESHOLD;
1189
1227
  if (useInline) {
1190
1228
  await Promise.all(misses.map((id) => compileInline(mode, contentDir, clonedRepository, id, errors)));
1191
1229
  } else {
@@ -1230,6 +1268,7 @@ async function logicalWriteFile(filepath, content) {
1230
1268
  }
1231
1269
 
1232
1270
  async function generateFullPageFile(data) {
1271
+ const supportsDarkMode = !(data.deactivateDarkMode ?? false);
1233
1272
  const computedScriptTags = data.js.filter((entry) => entry.type !== "overwriteStyleguide").map((js) => {
1234
1273
  const additionalAttributes = js.additionalAttributes ? Object.entries(js.additionalAttributes).map(([key, value]) => `${key}="${value}"`).join(" ") : "";
1235
1274
  return `<script src="${js.src}" ${additionalAttributes}><\/script>`;
@@ -1239,7 +1278,7 @@ async function generateFullPageFile(data) {
1239
1278
  }).join("\n");
1240
1279
  const content = `
1241
1280
  <!DOCTYPE html>
1242
- <html lang="${data.page.lang}"${data.page.htmlclass ? ` class="scroll-smooth ${data.page.htmlclass}"` : ""}>
1281
+ <html lang="${data.page.lang}"${data.page.htmlclass ? ` class="scroll-smooth ${data.page.htmlclass}"` : ""} data-styleguide-dark-mode="${supportsDarkMode}">
1243
1282
  <head>
1244
1283
  <title>${sanitizeSpecialCharacters(data.page.title)}</title>
1245
1284
  ${data.page.description ? `<meta name="description" content="${sanitizeSpecialCharacters(data.page.description)}">` : ""}
@@ -1252,7 +1291,7 @@ async function generateFullPageFile(data) {
1252
1291
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="${data.theme.light}">
1253
1292
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="${data.theme.dark}">
1254
1293
  ` : `<meta name="theme-color" content="${data.theme}">`}
1255
- <script type="module" src="/styleguide-assets/fullpage-Ck0nyq2C.js"><\/script>
1294
+ <script type="module" src="/styleguide-assets/fullpage-B5QPff3p.js"><\/script>
1256
1295
  ${computedStyleTags}
1257
1296
  </head>
1258
1297
  <body${data.page.bodyclass ? ` class="${data.page.bodyclass}"` : ""}>
@@ -2059,7 +2098,7 @@ async function generatePreviewFile(data) {
2059
2098
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="${data.theme.light}">
2060
2099
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="${data.theme.dark}">
2061
2100
  ` : `<meta name="theme-color" content="${data.theme}">`}
2062
- <link rel="stylesheet" type="text/css" href="/styleguide-assets/kss-modern-DeXoMEvT.css">
2101
+ <link rel="stylesheet" type="text/css" href="/styleguide-assets/kss-modern-CTiCJXSr.css">
2063
2102
  <script>
2064
2103
  //#region client/lib/iframe-resize.ts
2065
2104
  var e = [
@@ -2170,8 +2209,16 @@ var u = async (e) => {
2170
2209
  normal: "theme-normal",
2171
2210
  light: "theme-light",
2172
2211
  dark: "theme-dark"
2212
+ }, f = {
2213
+ normal: "light dark",
2214
+ light: "only light",
2215
+ dark: "only dark"
2173
2216
  };
2174
- function f(e) {
2217
+ function p(e, t) {
2218
+ let n = e.contentDocument?.documentElement;
2219
+ n && (n.style.colorScheme = f[t]);
2220
+ }
2221
+ function m(e) {
2175
2222
  let t = window.matchMedia("(prefers-color-scheme: dark)");
2176
2223
  function n() {
2177
2224
  return localStorage.getItem("in2theme") || (localStorage.setItem("in2theme", "normal"), "normal");
@@ -2183,7 +2230,7 @@ function f(e) {
2183
2230
  Object.values(d).forEach((t) => e.classList.remove(t));
2184
2231
  };
2185
2232
  a(e), e.classList.add(i), a(document.body), document.body.classList.add(i), document.querySelectorAll("iframe").forEach((e) => {
2186
- a(e), e.classList.add(i);
2233
+ a(e), e.classList.add(i), p(e, r);
2187
2234
  }), Array.from(document.querySelectorAll("iframe")).filter((e) => e.src.includes("embed.figma.com")).forEach((e) => {
2188
2235
  let t = new URL(e.src);
2189
2236
  t.searchParams.set("theme", r === "normal" ? "system" : r), e.src = t.href;
@@ -2191,7 +2238,9 @@ function f(e) {
2191
2238
  document.body.classList.add("allow-transitions");
2192
2239
  }, 500);
2193
2240
  }
2194
- r();
2241
+ r(), document.querySelectorAll("iframe").forEach((e) => {
2242
+ e.addEventListener("load", () => p(e, n()));
2243
+ });
2195
2244
  let i = e.querySelector(\`input[value="\${n()}"]\`);
2196
2245
  i && (i.checked = !0), e.addEventListener("change", () => {
2197
2246
  let t = e.querySelector("input[name=\\"theme\\"]:checked");
@@ -2205,14 +2254,14 @@ function f(e) {
2205
2254
  //#region client/preview-inline.ts
2206
2255
  document.addEventListener("DOMContentLoaded", () => {
2207
2256
  let e = document.querySelector(".theme-select");
2208
- e && f(e);
2257
+ e && m(e);
2209
2258
  let t = Array.from(document.querySelectorAll(".preview-iframe"));
2210
2259
  t.length > 0 ? u(t).catch(console.error) : l(), a();
2211
2260
  });
2212
2261
  //#endregion
2213
2262
  ;
2214
2263
  <\/script>
2215
- <script type="module" src="/styleguide-assets/preview-CHOmMvuB.js"><\/script>
2264
+ <script type="module" src="/styleguide-assets/preview-D2-sk3lE.js"><\/script>
2216
2265
  <link rel="preload" href="/styleguide-assets/fonts/geist-mono-latin-400-normal.woff2" as="font" type="font/woff2" crossorigin="anonymous">
2217
2266
  <link rel="preload" href="/styleguide-assets/fonts/geist-mono-latin-600-normal.woff2" as="font" type="font/woff2" crossorigin="anonymous">
2218
2267
  <link rel="preload" href="/styleguide-assets/fonts/geist-mono-latin-300-normal.woff2" as="font" type="font/woff2" crossorigin="anonymous">
@@ -2366,6 +2415,10 @@ function watchStyleguideForChanges(watchPath, handlers) {
2366
2415
  handlers.onStructuralChange();
2367
2416
  else if (isMarkupFile(filePath))
2368
2417
  handlers.onMarkupChange(path.resolve(filePath));
2418
+ }).on("error", (error) => {
2419
+ if (isTransientFsError(error))
2420
+ return;
2421
+ logger.error("Styleguide watcher error (ignored to keep the dev server running):", error);
2369
2422
  });
2370
2423
  }
2371
2424
 
@@ -2441,6 +2494,7 @@ async function writeFullPageFile(config, baseDirectory, section) {
2441
2494
  js: config.html.assets.js,
2442
2495
  html: htmlMarkup,
2443
2496
  theme: config.theme,
2497
+ deactivateDarkMode: config.deactivateDarkMode,
2444
2498
  ogImageUrl: config.plugins?.ogImage ? config.plugins.ogImage(section) : void 0
2445
2499
  });
2446
2500
  } catch (error) {
@@ -24,7 +24,7 @@
24
24
  "name": "utils"
25
25
  },
26
26
  "client/fullpage.ts": {
27
- "file": "fullpage-Ck0nyq2C.js",
27
+ "file": "fullpage-B5QPff3p.js",
28
28
  "name": "fullpage",
29
29
  "src": "client/fullpage.ts",
30
30
  "isEntry": true,
@@ -55,7 +55,7 @@
55
55
  "isDynamicEntry": true
56
56
  },
57
57
  "client/lib/html-validator.ts": {
58
- "file": "html-validator-Cq21EQMK.js",
58
+ "file": "html-validator-BxGmbFa-.js",
59
59
  "name": "html-validator",
60
60
  "src": "client/lib/html-validator.ts",
61
61
  "isDynamicEntry": true,
@@ -76,13 +76,13 @@
76
76
  ]
77
77
  },
78
78
  "client/preview-inline.ts": {
79
- "file": "preview-inline-CW9Pw65W.js",
79
+ "file": "preview-inline-ByTkw_pt.js",
80
80
  "name": "preview-inline",
81
81
  "src": "client/preview-inline.ts",
82
82
  "isEntry": true
83
83
  },
84
84
  "client/preview.ts": {
85
- "file": "preview-CHOmMvuB.js",
85
+ "file": "preview-D2-sk3lE.js",
86
86
  "name": "preview",
87
87
  "src": "client/preview.ts",
88
88
  "isEntry": true,
@@ -119,7 +119,7 @@
119
119
  ]
120
120
  },
121
121
  "style.css": {
122
- "file": "kss-modern-DeXoMEvT.css",
122
+ "file": "kss-modern-CTiCJXSr.css",
123
123
  "src": "style.css"
124
124
  }
125
125
  }