惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

Bryan Robinson's Blog

Does our technology still work for us? Product pricing, dev bad habits, and the role of the pit of success Astro Server Island for latest Bluesky post (heavily cached!) Type-safe environment variables in Astro 5.0 New Website, but really is it? Netlify Durable Cache: Caching for a third-party world Introducing the Hygraph Astro Content Loader Integrating Astro.js Starlight Documentation into a Next.js Project Using Proxies Jamstack is meaningless 😱 Book Release: Eleventy by Example – Learn 11ty with 5 in-depth projects 11ty Second 11ty: Creating Template Filters 11ty Second 11ty: Global Data files (JS and JSON) 11ty second 11ty: The Render Plugin Part 1 Help needed: Netlify Frontend environment variables with Astro.js Quick experiment with the Slinkity 11ty plugin Creating a dynamic color converter with 11ty Serverless Using 11ty JavaScript Data files to mix Markdown and CMS content into one collection How to show your template code in 11ty blog posts New City, New Job, New Content Using Nunjucks Climbing the 11ty Performance leaderboard with Cloudinary, critical CSS and more Three JAMstack movements to watch in 2020 Create a Codepen promo watermark with no additional HTML, CSS or JS 3 underused CSS features to learn for 2020 Use CSS Subgrid to layout full-width content stripes in an article template Adapt client-side JavaScript for use in 11ty (Eleventy) data files CSS Gap creates a bright future for margins in Flex as well as Grid Create your first CSS Custom Properties (Variables) Use CSS Grid to create a self-centering full-width element Now offering design and code reviews at PeerReviews.dev Routing contact-form emails to different addresses with Netlify, Zapier and SendGrid Create an Eleventy (11ty) theme based on a free HTML template Client work and the JAMstack Grid vs. Flex: A Tale of a "Simple" Promo Space Using Eleventy The Tech Barrier to Entry What Can We Learn from CERN Let Practical CSS Grid - Launching My First Course Build Trust on the Web incorporating User Worries with your User Stories 2019 The Year of Markup-First Development Refactoring CSS into a Sass mixin Starting a new journey with Code Contemporary Dynamic Static Sites with Netlify and iOS Shortcuts Top 3 uses for the ::before and ::after CSS pseudo elements How To: Use CSS Grid to Mix and Match Design Patterns Use CSS ::before and ::after for simple, spicy image overlays Modern CSS: Four Things Every Developer and Designer Should Know About CSS 3 Strategies for Getting Started with CSS Grid CSS Tip: Use rotate() and skew() together to introduce some clean punk rock to your CSS The 5 Stages of Grid Love How To: A CSS-Only Mobile Off Canvas Navigation How To: Use CSS Grid Layout to Make a Simple, Fluid Card Grid Make a More Flexible Cover Screen with CSS Grid Can CSS Grid open up interesting CMS Layout options? Firefox 52 to Introduce New Box-Alignment Values Falling Forward — Rethinking Progressive Enhancement, Graceful Degradation and Developer Morality Start Exploring the Magic of CSS Grid Layout I Converted My Blog to CSS Grid Layout and Regret Nothing Feature Queries are on the Rise CSS Shapes — Let the Text Flow Around You Flexbox -- Let Memorializing Prince and Print vs. The Web I went to Italy and noticed UX fails How to Get Designers to Contribute in Open Source The True Gift of Your Former Code
Creating an 11ty Plugin - SVG Embed Tool
2019-06-21 · via Bryan Robinson's Blog

11ty Plugin banner image where a plug is coming out of the 11ty logo

It’s well documented that I love the JAMstack. It’s also well documented, that I’m a fan of the JavaScript static site generator 11ty (Eleventy). In the sites I’ve built with it recently, I’ve found myself reusing a couple filters. This has involved me copying and pasting the code a lot. The solution? Create an 11ty Plugin.

I honestly thought this was a relatively new feature, but it turns out, it’s been around since v0.2.13! Look at that pie on my face!

Don’t care about creating the filter or plugin and just want to embed an SVG in your template? Run npm install --save eleventy-plugin-svg-contents and read the usage here.

What is an 11ty plugin?

An 11ty plugin is an NPM package that exports additions to your 11ty configuration. So all you have to do to add one to your site is to npm install the package and then add the plugin to your configuration. 

At that point you have access to anything the plugin offers. Most plugins are adding custom filters, tags or shortcodes. This can be handy for a lot of uses. Some of the plugins offered on the official documentation give you handy filters for RSS feeds, give estimated reading times for your blog posts, and even lint your blog posts based on inclusive language!

New Plugin: Embed an SVG from a file

In my projects, I like to have SVG files embedded in the HTML whenever possible to allow for using CSS to control various aspects of them. I could potentially use a template include to put the the code into my template. The drawback for that would be if I want to use a CMS to upload the image or use an SVG optimizer in my build process.

This allows me to upload SVGs to my images directory and still embed them easily in my template.

Let’s start with how the filter itself works.

The svgContents filter

The logic behind the filter is as basic as I could make it.

1. Accept a string of a file path

2. Find that file in the project.

3. Get the contents of the file

4. Return the contents encoded to utf-8

In 11ty, we add our custom filters in our .eleventy.js config file, but we can house those filters elsewhere.

My config file looks like this:

module.exports = function(config) {  
   config.addFilter("svgContents", require("./filters/svgContents.js"));
   // The rest of my config down here
}

Then, I put the filter itself in the contents.js file as a module.export.

const fs = require('fs');  

module.exports = function(file) {  
  let relativeFilePath = `.${file}`  

  let data = fs.readFileSync(relativeFilePath, function(err, contents) {  
    if (err) return err  
    return contents  
  });

  return data.toString('utf8')  
}

To use our filter in the template, we’ll use the following syntax (in Nunjucks and Liquid):

{{ "/string/to/svg.svg" | svgContents }}

By default, 11ty will accept the value of our variable in the template as the first argument of our function.

We then pass that string to the Node file-system readFile method. This will return the contents of the file. We’ll then return out of our filter method with a value of our contents encoded to UTF-8 for our template.

That’s it for the filter. It’s pretty lightweight. If you wanted, you could copy and paste that code into your project and embed any SVG you want. Like I said above, though, I got tired of copying and pasting. Let’s take a look at what it takes to make this a plugin!

Creating a plugin for 11ty

A plugin for 11ty is a JavaScript module at its heart. In the case of making it shareable, it’s specifically an NPM package.

There are better people than me to explain the intricacies of a proper setup for a package.json for an NPM module, but the important thing here, is that the main file for our module is its own .eleventy.js config file.

The contents of our 11ty config file should look very similar:

const svgContents = require("./src/getSvgContents");  

module.exports = function(eleventyConfig) {  
    eleventyConfig.addFilter("svgContents", svgContents);  
};

The file paths have changed, but the theory is still the same. We add our filter to the eleventyConfig object. The contents of our our module will be injected into the main 11ty config file in the final use.

While we’re making small changes, let’s add a few protections for the code: 

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

module.exports = function(file) {  
    let relativeFilePath = `.${file}`  
    if (path.extname(file) != '.svg') {  
        throw new Error("eleventy-plugin-svg-contents requires a filetype of svg");  
    }  
    let data = fs.readFileSync(relativeFilePath, function(err, contents) {    
        if (err) {  
            throw new Error(err)  
        }  
        
        return contents  
    });  
    return data.toString('utf8')  
}

We’ve added an error check early in the process. If the file extension isn’t .svg, we’ll exit with an error that will show up in the console. It’s always helpful to know why your site build fails. 

Later in the code, we use the fs.readFile API’s error condition like we did in the initial filter, but convert it over to a full-fledged error.

That’s it. We publish this to NPM and we can now install it in our project.

Installation of the plugin

1. Install the package

npm install --save eleventy-plugin-svg-contents

2. Require the package in our config file

const svgContents = require("eleventy-plugin-svg-contents");

3. Register the plugin

module.exports = function(config) {  
   config.addPlugin(svgContents);  

// Everything else
}

4. Use the filter in our templates

In any Nunjucks or Liquid template, use a variable notation:

{{ "/string/to/svg.svg" | svgContents }}

11ty makes this super easy and fun

If you’ve made an 11ty filter or custom tag, you can abstract them out to package to bring your favorite configuration options with you between projects.

You don’t need an SVG embed filter anymore, I’ve got you covered on that. What other filters would you like to create? If you’re not up to creating a custom filter or tag, what are your most used filters and plugins and could you create your own package to bundle them up and give yourself a great headstart on any new project?

Let me know in the comments!

{% include ad-space.html %}