Git Product home page Git Product logo

eleventy-plugin-reading-time's Introduction

Reading time plugin for Eleventy

A non-dependency reading time plugin for Eleventy static site generator. This plugin provides a template tag which prints the number of minutes or seconds required to read the given text.

Install

npm install --save eleventy-plugin-reading-time

Usage

In your Eleventy config file (defaults to .eleventy.js):

const readingTime = require('eleventy-plugin-reading-time');

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(readingTime);
};

Now you can use the readingTime filter in your Nunjuck templates:

<span>About {{ someTextContent | readingTime } reading time}</span>

prints

<span>About 6 min reading time</span>

Example post.njk template:

<article role="article">
    <header>
        <h1>{{ title }}</h1>
        <p>
            <span class="meta__stats">About {{ content | readingTime }} reading time</span>
        </p>
    </header>

    <div>
        {{ content | safe }}
    </div>
</article>

If you're in a collection loop, this filter accepts a collection object too:

{% for post in posts %}
    <li>
        <h1>{{post.title}}</h1>
        <p>About {{ post | readingTime }} reading time.</p>
    </li>
{% endfor %}

To Do

  • Support more template engines.

License

MIT.

eleventy-plugin-reading-time's People

Contributors

johanbrook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eleventy-plugin-reading-time's Issues

Handle undefined postOrContent

During deployment of my own blog I figured out, that it failed with the following message:

Error writing templates: (more in DEBUG output)
> Having trouble writing template: $DOCUMENT_ROOT/hobbies/coding/demos/vr/map.vr/index.html (TemplateWriterWriteError)
> (unknown path)  
  TypeError: Cannot read property 'templateContent' of undefined (Template render error):
    Template render error: (unknown path)
      TypeError: Cannot read property 'templateContent' of undefined
        at Object._prettifyError ($PROJECT_ROOT/node_modules/nunjucks/src/lib.js:36:11)
        at $PROJECT_ROOT/node_modules/nunjucks/src/environment.js:567:19
        at Template.root [as rootRenderFunc] (eval at _compile ($PROJECT_ROOT/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:93:3)
        at Template.render ($PROJECT_ROOT/node_modules/nunjucks/src/environment.js:556:10)
        at $PROJECT_ROOT/node_modules/@11ty/eleventy/src/Engines/Nunjucks.js:136:14
        at new Promise (<anonymous>)
        at $PROJECT_ROOT/node_modules/@11ty/eleventy/src/Engines/Nunjucks.js:135:14
        at TemplateLayout.render ($PROJECT_ROOT/node_modules/@11ty/eleventy/src/TemplateLayout.js:135:31)
        at process._tickCallback (internal/process/next_tick.js:68:7)

Today I set out to dig the root cause:

: postOrContent.templateContent;

Would it make sense to log an error and render nothing?

How to override setting of this plugin?

How can I override setting of this plugin? I tried the following code but it doesn't work.

  eleventyConfig.addPlugin(readingTime, {
    printSeconds: false,
    raw: true,
    speed: 200
  });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.