npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@web-alchemy/eleventy-plugin-content-dates

v1.0.4

Published

Static site generator [Eleventy](https://www.11ty.dev/docs/dates/) uses a special data field `date` with predefined values `Created`, `Last Modified`, `git Created`, `git Last Modified` to sort collections and show post date.

Downloads

7

Readme

Eleventy plugin for content dates

Static site generator Eleventy uses a special data field date with predefined values Created, Last Modified, git Created, git Last Modified to sort collections and show post date.

But there are two problems here:

  • it is not possible to use several fields at the same time, for example, for the creation date and for the modification date,
  • the date is calculated for one file, but you can have a whole folder for an article with pictures, fonts and other resources. Their update should also be considered as a modification of the article.

This plugin solves this problems.

Installation

npm install @web-alchemy/eleventy-plugin-content-dates

Configuration

const { eleventyPluginContentDates } = require('@web-alchemy/eleventy-plugin-content-dates');

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(eleventyPluginContentDates);
}

By default, dates are calculated for a file with the path data.page.inputPath. But you can change the path to the entity for which you need to calculate dates, for example, for a folder. Sometimes you need to calculate dates for folders, as they may contain other resources, for example, pictures. You can use the getContentFolderPath function from the plugin or write your own.

const { eleventyPluginContentDates, getContentFolderPath } = require('@web-alchemy/eleventy-plugin-content-dates');

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(eleventyPluginContentDates, {
    // this is default
    getContentPath: (data) => {
      return data.page.inputPath;
    }
    
    // you can compute dates for folders using library function `getContentFolderPath`
    getContentPath: getContentFolderPath
    
    // or write your own logic for getting path
    getContentPath: async (data) => {
      // `data` - is Eleventy data
      // function can be async
    }
  });
}

By default, library uses are async functions from modules node:fs and node:child_process. You can use sync version of this functions;

const { eleventyPluginContentDates, MODE } = require('@web-alchemy/eleventy-plugin-content-dates');

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(eleventyPluginContentDates, {
    mode: MODE.SYNC
  });
}

Usage

The plugin uses specials values for date fields:

  • Date. FS. Created - the date of creation file received from the file system
  • Date. FS. Last Modified - the date of modification file received from the file system
  • Date. Git. Created - the date of creation file received from the git
  • Date. Git. Last Modified - the date of modification file received from the git

Using in frontmatter:

---
createdAt: 'Date. Git. Created'
updatedAt: 'Date. Git. Last Modified'
---

<time datetime="{{ createdAt.toISOString()}}">
  {{ createdAt.toLocaleDateString() }}
</time>

<time datetime="{{ updatedAt.toISOString()}}">
  {{ updatedAt.toLocaleDateString() }}
</time>

Using in 11tydata-files:

const { TIMESTAMPS } = require('@web-alchemy/eleventy-plugin-content-dates');

module.exports = {
  createdAtWithFS: TIMESTAMPS.FS_CREATED,
  updatedAtWithFS: TIMESTAMPS.FS_LAST_MODIFIED,
  createdAtWithGit: TIMESTAMPS.GIT_CREATED,
  updatedAtWithGit: TIMESTAMPS.GIT_LAST_MODIFIED,
}

Low level usage example

You can use @web-alchemy/eleventy-plugin-content-dates as a library without registering the plugin:

// index.11tydata.js
const path = require('node:path');
const { TIMESTAMPS, MODE, computeDate } = require('@web-alchemy/eleventy-plugin-content-dates');

module.exports = {
  eleventyComputed: {
    yourCustomDateField: async function(data) {
      return await computeDate({
        strategy: TIMESTAMPS.GIT_LAST_MODIFIED,
        mode: MODE.ASYNC,
        contentPath: path.dirname(data.page.inputPath),
      })
    },
    
    yourAnotherCustomDateField: function(data) {      
      return computeDate({
        strategy: TIMESTAMPS.FS_CREATED,
        mode: MODE.SYNC,
        contentPath: data.page.inputPath,
      })
    },
  }
}

Caveats of use on CI/CD

Many CI/CD tools, such as Github Actions Checkout, by default does a shallow clone and dates may be incorrect.

Solutions:

  • Make deep clone with fetch-depth: '0':
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: '0'
    This is also helpful, if you use git creation dates. But with this method, all branches and tags are pulled out, not just the history of one branch.
  • Write your own git commands. Just example:
    - name: Checkout
      run: |
        git clone --depth 1 <REPO> .
        git fetch --unshallow