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 = ;var markdown = ;var section = ; ;
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 = ;var section = ;var layouts = ;var markdown = ; source'./resources'
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
{{title}} {{{contents}}} {{{sections.about}}} Colors I like {{#each sections.list}} {{{this}}} {{/each}}
Built html
metalsmith-section example Hello World!This is my page contents About meI am humanColors I like red green blue