0.3.0 ā€¢ Published 3 years ago

eleventy-plugin-page-assets v0.3.0

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

eleventy-plugin-page-assets

Copy local page assets to permalink folder

Instalation

Available on npm

npm install eleventy-plugin-page-assets --save-dev

Open up your Eleventy config file (probably .eleventy.js) and use addPlugin:

FILENAME .eleventy.js

const pageAssetsPlugin = require('eleventy-plugin-page-assets');

module.exports = function(eleventyConfig) {
    eleventyConfig.addPlugin(pageAssetsPlugin, {
        mode: "parse",
        postsMatching: "src/posts/*/*.md",
    });
};

How it works

This folder structure

šŸ“ src/posts/
  šŸ“ some-title/
    šŸ“„ index.md <-- when a template file is processed
    šŸ–¼ cover.png    assets relative to it are automatically
    šŸ–¼ image.jpg    copied to the permalink folder
  šŸ“ good-title/
    šŸ“„ index.md 
    šŸ–¼ cover.png
  šŸ“ bar-title/
    šŸ“„ index.md
    šŸ“ icons/ 
      šŸ–¼ icon.png
  šŸ“„ my-post.md
  šŸ–¼ img.png

Will generate this output

šŸ“ dist/
  šŸ“ perma-some-title/
    šŸ“„ index.html 
    šŸ–¼ 89509eae15a24c2276d54d4b7b28194a1391ee48.png 
    šŸ–¼ 63d8ddb9ffadd92e3d9a95f0e49ae76e7201a672.jpg 
  šŸ“ perma-good-title/
    šŸ“„ index.html 
    šŸ–¼ d0017352f4da463a61a83a1bc8baf539a4c921c1.png
  šŸ“ perma-bar-title/
    šŸ“„ index.md
    šŸ–¼ faa22a543b2dcb21fdd9b7795095e364ef00d540.png
  šŸ“ perma-my-post/
    šŸ“„ index.md
    šŸ–¼ faa22a543b2dcb21fdd9b7795095e364ef00d540.png

Directory mode

On directory mode the template is not parsed, assets on the same level as template are copied to the permalink folder, even if not used.

Note: Paths are not rewritten and folder structure is kept inside the perma folder.

This mode is cheaper as it does not parses the html or transforms it.

Options

OptionRequiredTypeDefaultDescription
modefalsestringparseParse mode will resolve assets referenced inside the template. Directory mode blindly copies files on the folder as the template.
postsMatchingfalsestring"*.md"Pattern (glob) filtering which templates to process
assetsMatchingfalsestring".png|.jpg|*.gif"Specify a pattern (glob) that matches which assets are going to be resolved
recursivefalsebooleanfalseRecursively scan assets under subdirectories (example src/posts/foo/bar/baz/img.jpg) (directory mode only)
hashAssetsfalsebooleantrueRewrite filenames to hashes. This will flatten the paths to always be next to the post .html file. (parse mode only)
hashingAlgfalsestringsha1Hashing algorithm sha1|md5|sha256|sha512 https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_optionsetc (parse mode only)
hashingDigestfalsestringhexDigest of the hash hex|base64 (parse mode only)
addIntegrityAttributefalsebooleanfalseAdd a integrity attribute to the tag (parse mode only)

TO-DO:

  • Parse the rendered html files looking for assets, and only used imported assets (similat to how what webpack loaders work)
  • Rewrite paths on the output files, possibly renaming files to md5 hashes, so images also have permalinks.
  • Write tests
0.3.0

3 years ago

0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago