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

metalsmith-mdn

v1.0.0

Published

MDN is a Metalsmith plugin that lets you use Nunjucks components in markdown content

Downloads

142

Readme

MDN is a Metalsmith plugin that lets you use Nunjucks in your markdown content. It enables the re-use of section components, the same components that you use in your page section templates can now be used in long text pages. Simply add the component props to the frontmatter of your markdown file and use the mdn tag to include the component in your markdown content.

If you are new to the concept of section components, you can read more about it on the Metalsmith Components Website and in this blog post.

metalsmith:plugin npm: version license: MIT

Installation

NPM:

npm install metalsmith-mdn

Yarn:

yarn add metalsmith-mdn

Usage

Pass metalsmith-mdn to metalsmith.use :

import Metalsmith from 'metalsmith';
import markdown from '@metalsmith/markdown';
import MDN from 'metalsmith-mdn'

Metalsmith( __dirname )
...
  .use( MDN( {
        templatesDir: "layouts",
        customFilters: "nunjucks-filters.js",
      } ) )

      .use( markdown() )
}))

Note: must be run immediately before the markdown plugin.

Options

templatesDir

templatesDir allows you to specify the directory where your Nunjucks templates are stored. The directory should be relative to the Metalsmith root. If not specified, the default directory is layouts.

customFilters

customFilters allows you to specify the filename of a custom Nunjucks filter file. This file should be located in the Metalsmith root directory. If not specified, the default filename is nunjucks-filters.js.

Example

To add a section component to your markdown content, use the mdn tag and pass unique tag name and props as arguments.

Here is an example of a markdown file with a section component. This example uses the setup that is shown in the Usage section above.

index.md

---
layout: simple.njk
bodyClass: "home"

seo:
  title: My Awesome Metalsmith Website
  description: "Fusce Aenean Ridiculus Tristique"
  
mySectionComponent:
  layout: sections/intro.njk
  text:
    title: Important Information
    header: "h2"
    subTitle: ""
    prose: |-
      Morbi leo risus, porta ac consectetur ac, vestibulum at eros. 
---

# Home page title
Donec id elit non mi porta gravida at eget metus. Donec sed odio dui. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.

{#mdn "mySectionComponent" #}

Curabitur blandit tempus porttitor. Nullam id dolor id nibh ultricies vehicula ut id elit. Vestibulum id ligula porta felis euismod semper.

In addition to page related props, the mySectionComponent tag is used to include the intro section component. The intro section component is located in the layouts/sections directory and is defined in the intro.njk file. Note that the intro section component imports the text macro.

layouts/sections/intro.njk

{% from "../partials/text.njk" import text %}

<section class="section-intro>
  <div class="content">
    {% set info = params %}
    {{ text(info.text) }}
  </div>
</section>

layouts/partials/text.njk

{% macro text(info) %}

  {% if info.title %}
    {% if info.header === "h1" %}
      <h1>{{ info.title }}</h1>
    {% elif info.header === "h2" %}
      <h2>{{ info.title }}</h2>
    {% else %}
      <h3>{{ info.title }}</h3>
    {% endif %}
  {% endif %}

  {% if info.subTitle %}
    <p class="sub-title">{{ info.subTitle }}</p>
  {% endif %}

  {% if info.prose %}
    <div>{{ info.prose | mdToHTML | safe }}</div>
  {% endif %}

{% endmacro %}

index.html

index.html below shows the transformed end result of the file. The intro section component, populated with the props from the frontmatter is included in the markdown content.

<h1>Home page title</h1>
<p>Donec id elit non mi porta gravida at eget metus. Donec sed odio dui. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>

<section class="section-intro  ">
  <div class="content">
    <h2>Important Information</h2>
    <div>
      <p>Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
    </div>
  </div>    
</section>

<p>Curabitur blandit tempus porttitor. Nullam id dolor id nibh ultricies vehicula ut id elit. Vestibulum id ligula porta felis euismod semper.</p>

Debug

To enable debug logs, set the DEBUG environment variable to metalsmith-mdn*:

metalsmith.env('DEBUG', 'metalsmith-mdn*')

CLI usage

To use this plugin with the Metalsmith CLI, add metalsmith-mdn to the plugins key in your metalsmith.json file:

{
  "plugins": [
    {
      "metalsmith-mdn": {}
    }
  ]
}

License

MIT