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-section

v1.0.3

Published

A metalsmith plugin which parses html content into named sections.

Downloads

3

Readme

metalsmith-section

A metalsmith plugin which will section your html content into metadata properties. Its ideal for single page sites with multiple content blocks. It can strip out sections of html from metadata.contents and make them accessible in your templates via their name. Additionally it can produce arrays of sections for iteration.

Installation

$ npm install metalsmith-section

Usage

Use section after markdown.

var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var section = require('metalsmith-section');

Metalsmith(__dirname)
    .use(markdown())
    .use(section())
    .build();

Make a named section

You can make sections by adding a line with the name of your section e.g. section:::mySection. You will find you section in the metaData under sections.mySection if you haven't changed any of the options. See example below or example folder for more details.

Make a list/array of sections

You can make arrays of sections by simply giving 1 or more sections the same name. If metalsmith-section encounters multiple sections with the same name it will compile them in to a list. A list is simply an array of sections which can later be iterated. See example below or example folder for more details.

Options

Pass an options object

const options = {
    pattern: "*",
    prefix: "section:::",
    removeFromContents: false,
    metaDataKey: 'list'
}

pattern

The file path pattern to match agains

default is **/*.html

prefix

The section name prefix used to split the sections, this will be removed from the html

default is "section:::"

removeFromContents

Should the sections be removed from the metaData.contents

default is true

metaDataKey

The name of the metaData property that the sections will be stored in

default is "sections"

Example

Javascript

var Metalsmith = require('metalsmith');
var section = require('metalsmith-section');
var layouts = require('metalsmith-layouts');
var markdown = require('metalsmith-markdown');

Metalsmith(__dirname)
    .source('./resources')
    .destination('./build')
    .use(markdown())
    .use(section())
    .use(layouts({
        engine: 'handlebars',
        pattern: "**/*.html"
    }))

Markdown

You can make sections by adding a line with the name of your section e.g. section:::mySection. You will find you section in the metaData under sections.mySection if you haven't changed any of the options. If metalsmith-section encounters multiple sections with the same name it will compile them in to a list. A list is simply an array of sections which can later be iterated.

---
layout: index.hbs
---

# Hello World!

This is my page contents


section:::about
---

## About me

I am human

section:::list
---

red

section:::list
---

green

section:::list
---

blue

Handlebars template

<!doctype html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    {{{contents}}}

    <hr/>

    {{{sections.about}}}
    <h2>Colors I like</h2>

    <ul>
    {{#each sections.list}}
        <li>{{{this}}}</li>
    {{/each}}
    </ul>

</body>
</html>

Built html

<!doctype html>
<html>
<head>
  <title>metalsmith-section example</title>
</head>
<body>

<h1>Hello World!</h1>
<p>This is my page contents</p>


<hr/>

<h2>About me</h2>
<p>I am human</p>
<h2>Colors I like</h2>

<ul>
    <li><p>red</p></li>
    <li><p>green</p></li>
    <li><p>blue</p></li>
</ul>

</body>
</html>