basecampjs 0.0.11 → 0.0.12

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 (2) hide show
  1. package/index.js +36 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -388,6 +388,26 @@ function createLiquidEnv(layoutsDir, pagesDir, srcDir, partialsDir) {
388
388
  });
389
389
  }
390
390
 
391
+ async function loadMustachePartials(partialsDir) {
392
+ const partials = {};
393
+ if (!existsSync(partialsDir)) return partials;
394
+
395
+ try {
396
+ const files = await walkFiles(partialsDir);
397
+ await Promise.all(files.map(async (file) => {
398
+ if (extname(file).toLowerCase() === ".mustache") {
399
+ const content = await readFile(file, "utf8");
400
+ const partialName = basename(file, ".mustache");
401
+ partials[partialName] = content;
402
+ }
403
+ }));
404
+ } catch (err) {
405
+ console.error(kolor.yellow(`Warning: Failed to load Mustache partials: ${err.message}`));
406
+ }
407
+
408
+ return partials;
409
+ }
410
+
391
411
  function toUrlPath(outRel) {
392
412
  const normalized = outRel.replace(/\\/g, "/");
393
413
  let path = `/${normalized}`;
@@ -416,7 +436,7 @@ function shouldRenderMarkdown(frontmatter, config, defaultValue) {
416
436
  return defaultValue;
417
437
  }
418
438
 
419
- async function renderWithLayout(layoutName, html, ctx, env, liquidEnv, layoutsDir) {
439
+ async function renderWithLayout(layoutName, html, ctx, env, liquidEnv, layoutsDir, partials = {}) {
420
440
  if (!layoutName) return html;
421
441
  const ext = extname(layoutName).toLowerCase();
422
442
  const layoutCtx = {
@@ -438,7 +458,7 @@ async function renderWithLayout(layoutName, html, ctx, env, liquidEnv, layoutsDi
438
458
  const layoutPath = join(layoutsDir, layoutName);
439
459
  if (existsSync(layoutPath)) {
440
460
  const layoutTemplate = await readFile(layoutPath, "utf8");
441
- return Mustache.render(layoutTemplate, layoutCtx);
461
+ return Mustache.render(layoutTemplate, layoutCtx, partials);
442
462
  }
443
463
  }
444
464
 
@@ -446,20 +466,25 @@ async function renderWithLayout(layoutName, html, ctx, env, liquidEnv, layoutsDi
446
466
  return html;
447
467
  }
448
468
 
449
- async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data }) {
469
+ async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data, partialsDir }) {
450
470
  const rel = relative(pagesDir, filePath);
451
471
  const ext = extname(filePath).toLowerCase();
452
472
  const outRel = rel.replace(/\.liquid(\.html)?$/i, ".html").replace(ext, ".html");
453
473
  const outPath = join(outDir, outRel);
454
474
  const path = toUrlPath(outRel);
455
475
  await ensureDir(dirname(outPath));
476
+
477
+ // Load Mustache partials if needed
478
+ const partials = (ext === ".mustache" || (await readFile(filePath, "utf8")).match(/layout:.*\.mustache/))
479
+ ? await loadMustachePartials(partialsDir)
480
+ : {};
456
481
 
457
482
  if (ext === ".md") {
458
483
  const raw = await readFile(filePath, "utf8");
459
484
  const parsed = matter(raw);
460
485
  const html = md.render(parsed.content);
461
486
  const ctx = pageContext(parsed.data, html, config, rel, data, path);
462
- const rendered = await renderWithLayout(parsed.data.layout, html, ctx, env, liquidEnv, layoutsDir);
487
+ const rendered = await renderWithLayout(parsed.data.layout, html, ctx, env, liquidEnv, layoutsDir, partials);
463
488
  await writeFile(outPath, rendered, "utf8");
464
489
  return;
465
490
  }
@@ -473,7 +498,7 @@ async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidE
473
498
  if (shouldRenderMarkdown(parsed.data, config, false)) {
474
499
  pageHtml = md.render(pageHtml);
475
500
  }
476
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir);
501
+ const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
477
502
  await writeFile(outPath, rendered, "utf8");
478
503
  return;
479
504
  }
@@ -486,7 +511,7 @@ async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidE
486
511
  if (shouldRenderMarkdown(parsed.data, config, false)) {
487
512
  pageHtml = md.render(pageHtml);
488
513
  }
489
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir);
514
+ const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
490
515
  await writeFile(outPath, rendered, "utf8");
491
516
  return;
492
517
  }
@@ -495,11 +520,11 @@ async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidE
495
520
  const raw = await readFile(filePath, "utf8");
496
521
  const parsed = matter(raw);
497
522
  const ctx = pageContext(parsed.data, parsed.content, config, rel, data, path);
498
- let pageHtml = Mustache.render(parsed.content, ctx);
523
+ let pageHtml = Mustache.render(parsed.content, ctx, partials);
499
524
  if (shouldRenderMarkdown(parsed.data, config, false)) {
500
525
  pageHtml = md.render(pageHtml);
501
526
  }
502
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir);
527
+ const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
503
528
  await writeFile(outPath, rendered, "utf8");
504
529
  return;
505
530
  }
@@ -512,7 +537,7 @@ async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidE
512
537
  if (shouldRenderMarkdown(parsed.data, config, false)) {
513
538
  pageHtml = md.render(pageHtml);
514
539
  }
515
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir);
540
+ const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
516
541
  await writeFile(outPath, rendered, "utf8");
517
542
  return;
518
543
  }
@@ -645,7 +670,7 @@ async function build(cwdArg = cwd) {
645
670
  return;
646
671
  }
647
672
 
648
- await Promise.all(files.map((file) => renderPage(file, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data })));
673
+ await Promise.all(files.map((file) => renderPage(file, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data, partialsDir })));
649
674
 
650
675
  if (config.minifyCSS) {
651
676
  await minifyCSSFiles(outDir);
@@ -1466,6 +1491,7 @@ async function main() {
1466
1491
  await preview();
1467
1492
  break;
1468
1493
  case "clean":
1494
+ case "cleanup":
1469
1495
  await clean();
1470
1496
  break;
1471
1497
  case "check":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basecampjs",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "description": "BasecampJS engine for CampsiteJS static site generator.",
6
6
  "bin": {