eleventy.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module.exports = function(eleventyConfig) {
  2. const fs = require('fs');
  3. const path = require('path');
  4. // add my css file from the root folder
  5. eleventyConfig.addPassthroughCopy("bundle.css");
  6. eleventyConfig.addPassthroughCopy("img");
  7. eleventyConfig.addPassthroughCopy({ "_data/images": "photos" });
  8. // This line was to make sure my post.md weren't processed as Liquid,
  9. // permitting this post here
  10. eleventyConfig.setTemplateFormats(["md", "njk", "html"]);
  11. eleventyConfig.addGlobalData("site.author", "unakt");
  12. // Double with the .eleventyignore file, but just in case
  13. eleventyConfig.ignores.add("/_drafts/**");
  14. eleventyConfig.ignores.add("/README.md");
  15. eleventyConfig.addLayoutAlias("post", "post.njk");
  16. eleventyConfig.addCollection("postlist", function(collectionApi) {
  17. return collectionApi.getFilteredByGlob("./posts/*.md");
  18. });
  19. // Custom collection for mini images
  20. // It scans the ./src/_data/images/ folder for jpg files
  21. // and groups them by their parent folder name
  22. // Each image object contains its path, album name, and last modified date
  23. // ========================================================
  24. eleventyConfig.addCollection("miniImages", function(collectionApi) {
  25. const files = [];
  26. const baseDir = "./_data/images/";
  27. // Recursive function to find all jpgs
  28. const getFiles = (dir) => {
  29. fs.readdirSync(dir).forEach(file => {
  30. const fullPath = path.join(dir, file);
  31. if (fs.statSync(fullPath).isDirectory()) {
  32. getFiles(fullPath);
  33. } else if (file.endsWith(".jpg") ||
  34. file.endsWith(".jpeg") ||
  35. file.endsWith(".png")) {
  36. // BEFORE =============
  37. const relativePath = path.relative("./_data/images", fullPath).replace(/\\/g, "/");
  38. //const pathParts = relativePath.split(path.sep).slice(0, -1); // Make Breadcrumb
  39. // ====================
  40. files.push({
  41. // AFTER ==============
  42. fullPath: fullPath, // We need this for the calculation below
  43. //webPath: fullPath.replace("src/", "/"),
  44. date: fs.statSync(fullPath).mtime,
  45. // BEFORE =============
  46. path: "/photos/" + relativePath,
  47. //album: dir.split(path.sep).pop(), // Gets the folder name
  48. //breadcrumbs: pathParts,
  49. //date: fs.statSync(fullPath).mtime
  50. // ====================
  51. });
  52. }
  53. });
  54. };
  55. getFiles(baseDir);
  56. // Group them by album name
  57. const grouped = {};
  58. files.forEach(f => {
  59. // AFTER ==============
  60. const relFolder = path.relative(baseDir, path.dirname(f.fullPath));
  61. // BEFORE =============
  62. // Use the directory path as the unique ID (e.g., "wargaming/40k/orks")
  63. //const folderPath = path.dirname(path.relative("./_data/images", f.path));
  64. if (!grouped[relFolder]) {
  65. grouped[relFolder] = {
  66. images: [],
  67. subfolders: [],
  68. breadcrumbArray: relFolder === "" ? [] : relFolder.split(path.sep)
  69. //path: folderPath.split(path.sep) // Normalize to forward slashes
  70. };
  71. }
  72. grouped[relFolder].images.push(f);
  73. });
  74. // Second pass to link parents to children subfolders
  75. for (const parentPath in grouped) {
  76. for (const potentialChild in grouped) {
  77. const parentParts = parentPath === "" ? [] : parentPath.split(path.sep);
  78. const childParts = potentialChild.split(path.sep);
  79. // Is the child exactly one level deeper than the parent?
  80. if (potentialChild.startsWith(parentPath) &&
  81. childParts.length === parentParts.length + 1 &&
  82. parentPath !== potentialChild) {
  83. grouped[parentPath].subfolders.push(potentialChild);
  84. }
  85. }
  86. }
  87. return grouped;
  88. });
  89. // ========================================================
  90. // Date filter to format dates as YYYY-MM-DD
  91. eleventyConfig.addFilter("date", function(dateObj, format = "YYYY-MM-DD") {
  92. const d = new Date(dateObj);
  93. return d.toISOString().split('T')[0]; // Returns YYYY-MM-DD
  94. });
  95. return {
  96. // Ok idk this prevent markdown from being processed?
  97. markdownTemplateEngine: false,
  98. dir: {
  99. input: "./",
  100. output: "_site",
  101. layouts: "layouts",
  102. includes: "includes"
  103. }
  104. };
  105. };