lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.85k stars 85 forks source link

Question: how to support both rendering .html from a file and also copying the raw file #592

Closed johanbrook closed 6 months ago

johanbrook commented 6 months ago

Version

2.1.3

Platform

macOS

What steps will reproduce the bug?

Let's say I have a bunch of .cook files in a recipes folder. I'd like to render these as HTML with a layout, but also serve these raw .cook files in the built site.

It's the same scenario as wanting to keep the source .md files in a blog and serve along side built .html files, or keep raw .json files and serve as a JSON API.

I have a hard time finding my way around the docs to find an elegant solution for this.

What I landed on now:

// _config.ts
import { copy, expandGlob } from 'std/fs/mod.ts';

const SRC =  'src';
const DEST = 'build';

const site = lume({
    src: SRC,
    dest: DEST,
});

site
    .data('layout', 'layouts/reci.njk', '/recipes')
     // Makes src/recipes/*.cook → build/recipes/*/index.html
    .loadPages(['.cook'], async (path) => {
        const text = await Deno.readTextFile(path);
        return parseToRecipe(text); // irrelevant
    })
    // Makes src/recipes/*.cook → build/recipes/*.cook
    .addEventListener('afterBuild', async () => {
        // Fiddly, since we don't want to overwrite the existing build/recipes directory from above
        for await (const entry of expandGlob(`./src/recipes/*.cook`)) {
            await copy(entry.path, `./${DEST}/recipes/${entry.name}`);
        }
    })

I've tried various combinations of:

I've read https://lume.land/docs/core/loaders top to bottom. The key paragraph for my issue is:

Note: you can't use the same extension to generate pages and assets, so a way to have support for both is adding a sub-extension (like .page) for pages


Is my solution above the best one so far?

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

-

Additional information

Related as input to:

johanbrook commented 6 months ago

Sorry, saw there are Discussions here too (https://github.com/lumeland/lume/discussions/593).