Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom extensions for data providers #736

Closed
cfjedimaster opened this issue Oct 18, 2019 · 11 comments
Closed

Support custom extensions for data providers #736

cfjedimaster opened this issue Oct 18, 2019 · 11 comments
Milestone

Comments

@cfjedimaster
Copy link

Is your feature request related to a problem? Please describe.
Note, I don't think this is the same as #117, but I could be wrong.

I do a lot of work with GeoJSON files (https://geojson.org/). They are JSON files but (typically) use the .geojson file extension. Putting one of these in _data will not work because 11ty will not recognize it as JSON.

Describe the solution you'd like
I'd like a configuration setting that would let me specify a file extension pattern, or list, to be treated as JSON.

Describe alternatives you've considered
Renaming the file. :) That works.

@Ryuno-Ki
Copy link
Contributor

Hm, could you workaround with collection (glob flavour) or data files until #117 is implemented?

@cfjedimaster
Copy link
Author

How would collections help? And to be clear, I meant data files. If it wasn't clear in the issue above let me know and I'll rewrite.

Basically, let me say that _data/cats.geojson is a valid data file by telling 11ty to look for *.json and *.geojson. The default would be *.json.

@saenglert
Copy link
Contributor

saenglert commented Oct 18, 2019

Had a similar problem. I wanted to create a collection out of json files in my data folder. Eleventy treated them as individual data files. So I created a directory data file in the JS format which means I can use Node to manually load, parse and return the files as JSON objects:

const fs = require("fs");
const path = require("path");

const jsons = fs.readdirSync(__dirname, {withFileTypes: true}) // Get all files in dir
.map(file => file.name) // Map all files names
.filter(name => /(.+)\.json$/.exec(name)) // Filter all .json files
.map(name => fs.readFileSync(path.resolve(__dirname, name))) // Read .json files
.map(buffer => JSON.parse(buffer.toString())) // Parse to json object

module.exports =  jsons;

Might be overkill but worked for me :)

Edit: Of course depending on the size of your files you might want to switch from sync to async file operations

@Ryuno-Ki
Copy link
Contributor

I had a similar approach like @saenglert in mind.

@Ryuno-Ki
Copy link
Contributor

@cfjedimaster
Copy link
Author

Glob collection would be https://www.11ty.io/docs/collections/#getfilteredbyglob(-glob-)

I don't get that how that would help for a data file though? Basically I want foo.geojson to be available as foo in my templates. It's not a collection, just data. I'd like Eleventy to basically let me configure: "In _data, what files do I consider JSON files."

Does that make sense?

@saenglert
Copy link
Contributor

Yes it does, @cfjedimaster.
However, as @Ryuno-Ki already pointed out this is not possible as #117 is not implemented yet. So you need a workaround.

Now my approach returns the data as unsorted array. You want to access your files by filename. This means some modifications are required.

@cfjedimaster
Copy link
Author

Ok, so when I wrote this bug, I thought maybe 117 wasn't going to cover it. If you think it will, I'll gladly close this as a dupe. :) Thanks!

@gloryofrobots
Copy link
Contributor

You probably can do it with new custom data files feature that just get merged #798
eleventyConfig.addDataExtension("geojson", contents => JSON.parse(contents));

@zachleat
Copy link
Member

#635 #798 aren’t quite aliasing but since it’s JSON I think that lovely addition by @gloryofrobots will solve this particular issue nicely.

Just for pedantic reasons I’ve filed #801 if people want fancier data file aliasing moving forward. #207 is the issue for template file aliasing.

@zachleat zachleat added this to the Next Minor Version milestone Nov 27, 2019
@zachleat
Copy link
Member

zachleat commented Nov 27, 2019

Closing as a duplicate of #630. Look for this feature in 0.10.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants