reusable html layouts for awoo
- Powerful templating system
- Templates are just JavaScript functions!
- Ability to perform templating on a subset of files
# you will need frontmatter support:
npm install awoo-matter
npm install awoo-layouts
const awoo = require('awoo')
const matter = require('awoo-matter')
const layouts = require('awoo-layouts')
// a layout is just a js function that returns a string of
// html! it takes the current file and also the total set
// of files
const layout = (file, files) =>
`
<h1>
${file.contents}
</h1>
`
// enter our main function:
// the main function should be an async function so that
// it automatically returns a promise
awoo(async site => {
// first we need frontmatter support
site.use(matter)
// and now we can initialize layouts
// (the `layouts` options key takes an object of layouts)
site.use(layouts, { layouts: { layout } })
// ...and initiate the build process
return site
})
By default, layouts
only operates on HTML files (files that end with .html
).
This can be changed easily by using a custom filter. A filter is a function that
takes a file and returns a boolean that describes whether that file should have
layouts
enabled or not. A custom filter can look like this:
function myCustomFilter (file, options, files) {
return file.path.endsWith('.md')
}
This filter only matches for Markdown files. If you wanted to match on multiple file extensions at the same time, you could do something like this:
const fileExtension = file => file.path.split('.').pop()
function myOtherCustomFilter (file, options, files) {
return ['md', 'html'].some(e => fileExtension(file) === e)
}
To use the filter, just pass it into the plugin options:
awoo(async site => {
site.use(matter)
site.use(layouts, {layouts: {...}, filter: myCustomFilter})
return site
})
- Olivia Hugger <olivia@fastmail.com>
MIT (see LICENSE document)