extract-marpit-css.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Build-time script to extract Marp base CSS constants.
  3. *
  4. * Replicates the Marp configuration from growi-marpit.ts and generates
  5. * pre-extracted CSS so that GrowiSlides can apply Marp container styling
  6. * without a runtime dependency on @marp-team/marp-core or @marp-team/marpit.
  7. *
  8. * Regenerate with: node scripts/extract-marpit-css.ts
  9. */
  10. import { writeFileSync } from 'node:fs';
  11. import { dirname, resolve } from 'node:path';
  12. import { fileURLToPath } from 'node:url';
  13. import type { MarpOptions } from '@marp-team/marp-core';
  14. import { Marp } from '@marp-team/marp-core';
  15. import { Element } from '@marp-team/marpit';
  16. const __dirname = dirname(fileURLToPath(import.meta.url));
  17. const MARP_CONTAINER_CLASS_NAME = 'marpit';
  18. const marpitOption: MarpOptions = {
  19. container: [
  20. new Element('div', { class: `slides ${MARP_CONTAINER_CLASS_NAME}` }),
  21. ],
  22. inlineSVG: true,
  23. emoji: undefined,
  24. html: false,
  25. math: false,
  26. };
  27. // Slide mode: with shadow/rounded slide containers
  28. const slideMarpitOption: MarpOptions = { ...marpitOption };
  29. slideMarpitOption.slideContainer = [
  30. new Element('section', { class: 'shadow rounded m-2' }),
  31. ];
  32. const slideMarpit = new Marp(slideMarpitOption);
  33. // Presentation mode: minimal slide containers
  34. const presentationMarpitOption: MarpOptions = { ...marpitOption };
  35. presentationMarpitOption.slideContainer = [
  36. new Element('section', { class: 'm-2' }),
  37. ];
  38. const presentationMarpit = new Marp(presentationMarpitOption);
  39. const { css: slideCss } = slideMarpit.render('');
  40. const { css: presentationCss } = presentationMarpit.render('');
  41. if (!slideCss || !presentationCss) {
  42. // biome-ignore lint/suspicious/noConsole: Allows console output for script
  43. console.error('ERROR: CSS extraction produced empty output');
  44. process.exit(1);
  45. }
  46. const output = `// Generated file — do not edit manually
  47. // Regenerate with: node scripts/extract-marpit-css.ts
  48. export const SLIDE_MARPIT_CSS = ${JSON.stringify(slideCss)};
  49. export const PRESENTATION_MARPIT_CSS = ${JSON.stringify(presentationCss)};
  50. `;
  51. const outPath = resolve(
  52. __dirname,
  53. '../src/client/consts/marpit-base-css.vendor-styles.prebuilt.ts',
  54. );
  55. writeFileSync(outPath, output, 'utf-8');
  56. // biome-ignore lint/suspicious/noConsole: Allows console output for script
  57. console.log(`Extracted Marp base CSS to ${outPath}`);