eleventy.config.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { readFileSync } from 'node:fs';
  2. import { EleventyRenderPlugin } from "@11ty/eleventy";
  3. import { join } from 'node:path';
  4. import { sync } from 'glob';
  5. import { appFilters } from "../shared/e11ty/filters.mjs";
  6. import { appData } from "../shared/e11ty/data.mjs"
  7. import { appConfig } from "../shared/e11ty/config.mjs"
  8. /** @type {import('@11ty/eleventy').LocalConfig} */
  9. export default function (eleventyConfig) {
  10. const environment = process.env.NODE_ENV || "production";
  11. const currentDir = process.cwd();
  12. appConfig(eleventyConfig);
  13. appFilters(eleventyConfig);
  14. appData(eleventyConfig);
  15. eleventyConfig.addPassthroughCopy({
  16. "node_modules/@tabler/core/dist": "dist",
  17. "pages/favicon.ico": "favicon.ico",
  18. "pages/favicon-dev.ico": "favicon-dev.ico",
  19. "static": "static",
  20. });
  21. eleventyConfig.addPlugin(EleventyRenderPlugin, {
  22. accessGlobalData: true,
  23. });
  24. /**
  25. * Data
  26. */
  27. eleventyConfig.addGlobalData("environment", environment);
  28. eleventyConfig.addGlobalData("readme", readFileSync(join("..", "README.md"), "utf-8"));
  29. eleventyConfig.addGlobalData("license", readFileSync(join("..", "LICENSE"), "utf-8"));
  30. // PostHog Analytics Environment Variables
  31. eleventyConfig.addGlobalData("posthogApiKey", process.env.NEXT_PUBLIC_POSTHOG_KEY);
  32. eleventyConfig.addGlobalData("posthogHost", process.env.NEXT_PUBLIC_POSTHOG_HOST);
  33. eleventyConfig.addGlobalData("pages", () => {
  34. return sync('pages/**/*.html').filter((file) => {
  35. return !file.includes('pages/_') && !file.includes('pages/docs/index.html');
  36. }).map((file) => {
  37. return {
  38. url: file.replace(/^pages\//, '/')
  39. }
  40. });
  41. });
  42. /**
  43. * Shortcodes
  44. */
  45. const tags = ["highlight", "endhighlight"];
  46. tags.forEach(tag => {
  47. eleventyConfig.addLiquidTag(tag, function (liquidEngine) {
  48. return {
  49. parse: function (tagToken, remainingTokens) {
  50. this.str = tagToken.args;
  51. },
  52. render: function (scope, hash) {
  53. return "";
  54. },
  55. };
  56. });
  57. });
  58. let _CAPTURES = {};
  59. eleventyConfig.on('beforeBuild', () => {
  60. _CAPTURES = {};
  61. });
  62. ['script', 'modal'].forEach((tag) => {
  63. eleventyConfig.addPairedShortcode(`capture_${tag}`, function (content, inline) {
  64. if (inline) {
  65. return content;
  66. }
  67. if (!_CAPTURES[tag]) {
  68. _CAPTURES[tag] = []
  69. }
  70. if (!_CAPTURES[tag][this.page.inputPath]) {
  71. _CAPTURES[tag][this.page.inputPath] = [];
  72. }
  73. _CAPTURES[tag][this.page.inputPath].push(content);
  74. return ''
  75. })
  76. eleventyConfig.addShortcode(`${tag}s`, function () {
  77. if (_CAPTURES[tag] && _CAPTURES[tag][this.page.inputPath]) {
  78. return _CAPTURES[tag][this.page.inputPath] ? `<!-- BEGIN PAGE ${tag.toUpperCase()}S -->\n${_CAPTURES[tag][this.page.inputPath].join('\n').trim()}\n<!-- END PAGE ${tag.toUpperCase()}S -->` : '';
  79. }
  80. return ''
  81. });
  82. });
  83. /**
  84. * Transforms
  85. */
  86. if (environment !== "development") {
  87. function prettifyHTML(content, outputPath) {
  88. return outputPath.endsWith('.html')
  89. ? content
  90. .replace(/\/\/ @formatter:(on|off)\n+/gm, '')
  91. // remove empty lines
  92. .replace(/^\s*[\r\n]/gm, '')
  93. : content
  94. }
  95. eleventyConfig.addTransform('htmlformat', prettifyHTML)
  96. }
  97. };