11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
16.84k stars 487 forks source link

Unable to use custom base file name for data files unless `11tydata` also included as extension #2872

Open paulrobertlloyd opened 1 year ago

paulrobertlloyd commented 1 year ago

Operating system

macOS Ventura 13.2.1

Eleventy

2.0.0

Describe the bug

I’m trying to use a custom base file name so that I can use the following directory data files[^1]:

.
├── articles
│   ├── 2023-03-18-example.md
│   └── _data.json
└── notes
    ├── 2023-03-18-1.md
    └── _data.json

However, the custom base file name only appears to work if the .11tydata suffix is provided:

Using default config (data files share same name as directory):

Using eleventyConfig.setDataFileBaseName("_data"):

Setting eleventyConfig.setDataFileSuffixes([""]) has no effect, either.

[^1]: I actually want to use a custom data format, YAML, so that I can use _data.yaml files, but given the natively supported JSON format doesn’t work as expected either, using this as my example.

zachleat commented 1 year ago

Yeah, I believe this was an intentional defensive design decision. I wonder if there is some middle ground we can get to here, I’d be most worried about .js data files but it seems like .json is risky too.

https://github.com/11ty/eleventy/blame/da10b5c6ed1f7646e24f3e7576434e079c64e064/src/TemplateData.js#L608

Commit: https://github.com/11ty/eleventy/commit/d0ab03811e87735ac3d79f03bdbff493358f13df

Specifically from the original request (#1699) was targeted towards index.* data files and my worry was that index.js and index.json would match files that were not supposed to be data files.

(Consider what I said me to be thinking out loud)

That said, it does seem like this should work (but does not):

eleventyConfig.setDataFileBaseName("_data");
eleventyConfig.setDataFileSuffixes([""]);
valtlai commented 1 year ago

I would like to configure this so that _data.json, _data.js, etc. work as directory data files but, at the same time, template data files still require the .data file extension. For example, lorem-ipsum.data.js would work as a data file for the lorem-ipsum.md template but lorem-ipsum.js wouldn’t be a data file but a template.