silexlabs / eleventy-plugin-directus Goto Github PK
View Code? Open in Web Editor NEWExpose Directus collections as global data in 11ty
Home Page: https://www.npmjs.com/package/@silexlabs/eleventy-plugin-directus
License: MIT License
Expose Directus collections as global data in 11ty
Home Page: https://www.npmjs.com/package/@silexlabs/eleventy-plugin-directus
License: MIT License
I was wondering the best way of downloading directus' assets and storing them locally instead of hotlinking to my directus instance using directus_asseturl
internet2000.net already seems to do this (e.g. https://internet2000.net/optimized/8a7c9b06-0d80-4b3b-9624-ebcb359e8ea3-235.webp and https://internet2000.net/uploads/93c3b71f-16fc-4a88-b883-b7be02a1e919.svg).
After a few hours of tinkering (JS is not my strong suit), I came up with a hacky solution using Eleventy-Fetch that seems to work, although it could be greatly improved:
filters/directus_asset.js
const fs = require('fs');
const EleventyFetch = require("@11ty/eleventy-fetch");
module.exports = async function (img) {
const directus_c = this.ctx.directus // from https://github.com/11ty/eleventy/issues/813
let url = `${directus_c.url}/assets/${img.filename_disk}?download&access_token=${directus_c.token}`;
let imageBuffer = await EleventyFetch(url, {
duration: "1d",
type: "buffer"
});
let folder_path = `./public/img/${img.folder.name}/`
let path = `${folder_path}${img.filename_download}`
fs.mkdirSync(folder_path, { recursive: true })
fs.writeFile(path, imageBuffer, "binary", (err) => err && console.error(err))
let final_path = `/img/${img.folder.name}/${img.filename_download}`
return final_path;
};
.eleventy.js
...
eleventyConfig.addAsyncFilter('directus_asset', require('./filters/directus_download.js'));
...
template.njk
{% for cliente in directus.collections.Clientes %}
<div><img src="{{ cliente.Logo | directus_asset }}" alt="{{ cliente.Nome }}"></div>
{% endfor %}
I'm not sure if this is the best way of doing this, but I was wondering if it would be possible to share how Internet2000 does it or implement a native function for doing this within the plugin.
Here is what I am working on:
{% assign page = item | directus_relation: 'link' %}
{% assign translated = item | directus_translate: 'fr' %}
<img src="{{ item.image | directus_asseturl: true }}">
#3cc @eitchtee
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.