feat: Add check for content

The `source/screens` directory is checked for any present file. This changes the permalink path for demo/index.liquid.

- Add fs module to eleventy config
- Add function to check for files
- Add demoPath to global data
- Change permalink for demo index to use demoPath
This commit is contained in:
sthag 2026-03-15 08:52:09 +01:00
parent 437c561847
commit d3ab17d5a1
2 changed files with 16 additions and 3 deletions

View file

@ -1,8 +1,19 @@
/* jshint strict: false */ /* jshint strict: false */
import fs from 'fs/promises';
import {HtmlBasePlugin} from "@11ty/eleventy"; import {HtmlBasePlugin} from "@11ty/eleventy";
import pluginWebc from "@11ty/eleventy-plugin-webc"; import pluginWebc from "@11ty/eleventy-plugin-webc";
async function hasFiles(dirPath) {
try {
const entries = await fs.readdir(dirPath, {withFileTypes: true});
return entries.some(entry => entry.isFile());
} catch (err) {
console.error('Error reading directory:', err);
return false;
}
}
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
export default async function (eleventyConfig) { export default async function (eleventyConfig) {
eleventyConfig.addPlugin(HtmlBasePlugin); eleventyConfig.addPlugin(HtmlBasePlugin);
@ -19,6 +30,9 @@ export default async function (eleventyConfig) {
eleventyConfig.addGlobalData('permalink', () => { eleventyConfig.addGlobalData('permalink', () => {
return (data) => `${data.page.filePathStem}.${data.page.outputFileExtension}`; return (data) => `${data.page.filePathStem}.${data.page.outputFileExtension}`;
}); });
const demoPath = await hasFiles('source/screens') ? '/demo/' : '/';
eleventyConfig.addGlobalData('hippie', { eleventyConfig.addGlobalData('hippie', {
brand: 'hippie', brand: 'hippie',
titlePrefix: '', titlePrefix: '',
@ -30,6 +44,7 @@ export default async function (eleventyConfig) {
mail: 'name@domain.tld', mail: 'name@domain.tld',
domain: 'https://domain.tld' domain: 'https://domain.tld'
}, },
demoPath: demoPath,
debugMode: true, debugMode: true,
legacyMode: false legacyMode: false
}); });
@ -85,7 +100,6 @@ export default async function (eleventyConfig) {
</g> </g>
</g> </g>
</svg> </svg>
`; `;
let output = ''; let output = '';
@ -124,6 +138,5 @@ export default async function (eleventyConfig) {
data: '../data' data: '../data'
}, },
templateFormats: ['html', 'liquid', 'md'] templateFormats: ['html', 'liquid', 'md']
// pathPrefix: './demo/'
}; };
} }

View file

@ -1,5 +1,5 @@
--- ---
permalink: "/" permalink: "{{ hippie.demoPath }}"
title: Index title: Index
--- ---
{% assign pageId = page.fileSlug -%} {% assign pageId = page.fileSlug -%}