www/.eleventy.js

150 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-06-17 17:02:30 -06:00
const { DateTime } = require('luxon')
const fs = require('fs')
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
2018-01-16 20:08:47 -07:00
module.exports = function(eleventyConfig) {
2021-03-17 09:29:29 -06:00
// https://www.11ty.dev/docs/data-deep-merge/
2021-06-17 17:02:30 -06:00
eleventyConfig.setDataDeepMerge(true)
2021-06-17 17:02:30 -06:00
eleventyConfig.addFilter('readableDate', dateObj => {
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('dd LLL yyyy')
})
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
2021-06-17 17:02:30 -06:00
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd')
})
// Get the first `n` elements of a collection.
2021-06-17 17:02:30 -06:00
eleventyConfig.addFilter('head', (array, n) => {
if( n < 0 ) {
2021-06-17 17:02:30 -06:00
return array.slice(n)
}
2021-06-17 17:02:30 -06:00
return array.slice(0, n)
})
2021-03-17 09:29:29 -06:00
// Return the smallest number argument
2021-06-17 17:02:30 -06:00
eleventyConfig.addFilter('min', (...numbers) => {
return Math.min.apply(null, numbers)
})
2020-10-16 12:06:30 -06:00
2021-06-17 17:02:30 -06:00
eleventyConfig.addFilter('filterTagList', tags => {
2021-03-17 15:29:07 -06:00
// should match the list in tags.njk
2021-06-17 17:02:30 -06:00
return (tags || []).filter(tag => ['all', 'nav', 'post', 'posts'].indexOf(tag) === -1)
2021-03-17 15:29:07 -06:00
})
// Convert text to hex
eleventyConfig.addFilter('toHex', (str) => {
2021-10-23 22:18:42 -06:00
let hex = ''
for(let i=0; i<str.length; i++){
let h = str.codePointAt(i).toString(16)
if(h.length===1) h='0'+h
hex += h + ' '
if( h.length>4 ) i++
} return hex.trim()
})
2022-10-20 09:53:02 -06:00
2021-10-23 22:18:42 -06:00
// Fold lines
eleventyConfig.addFilter('fold', (str, size) => {
// https://stackoverflow.com/a/29202760/3006854
const numChunks = Math.ceil(str.length/size)
const chunks = new Array(numChunks)
for (let i = 0, o = 0; i < numChunks; ++i, o += size)
chunks[i] = str.substr(o, size)
return chunks.join('\n')
})
2021-03-17 09:29:29 -06:00
// Create an array of all tags
2021-06-17 17:02:30 -06:00
eleventyConfig.addCollection('tagList', function(collection) {
let tagSet = new Set()
2021-03-17 15:29:07 -06:00
collection.getAll().forEach(item => {
2021-06-17 17:02:30 -06:00
(item.data.tags || []).forEach(tag => tagSet.add(tag))
2021-10-23 22:18:42 -06:00
}); return [...tagSet]
2021-06-17 17:02:30 -06:00
})
2018-08-29 07:46:06 -06:00
2021-03-17 09:29:29 -06:00
// Customize Markdown library and settings:
let markdownLibrary = markdownIt({
2018-06-18 08:00:00 -06:00
html: true,
breaks: true,
linkify: true
})//.use(markdownItAnchor, {
// permalink: true,
// permalinkClass: "direct-link",
// permalinkSymbol: "#"
// })
2021-06-17 17:02:30 -06:00
eleventyConfig.setLibrary("md", markdownLibrary)
2018-06-18 08:00:00 -06:00
2021-03-17 09:29:29 -06:00
// Override Browsersync defaults (used only with --serve)
eleventyConfig.setBrowserSyncConfig({
callbacks: {
ready: function(err, browserSync) {
2022-10-20 09:53:02 -06:00
const content_404 = fs.readFileSync('_site/404.html')
browserSync.addMiddleware("*", (req, res) => {
// Provides the 404 content without redirect.
2021-06-17 17:02:30 -06:00
res.writeHead(404, {"Content-Type": "text/html charset=UTF-8"})
res.write(content_404)
res.end()
})
},
},
2020-01-03 10:30:27 -07:00
ui: false,
ghostMode: false
2021-06-17 17:02:30 -06:00
})
return {
2021-03-17 09:29:29 -06:00
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: [
2018-01-16 20:08:47 -07:00
"md",
"njk",
2021-06-17 17:02:30 -06:00
'html',
'liquid',
'css',
'png',
2021-06-18 19:42:56 -06:00
'webp',
2021-06-17 17:02:30 -06:00
'jpg',
'gif',
'js',
2021-06-17 20:35:39 -06:00
'crt',
'pem',
2021-11-21 09:47:23 -07:00
'ttf',
2021-06-21 18:01:11 -06:00
'webmanifest',
2018-01-16 20:08:47 -07:00
],
2021-03-17 09:29:29 -06:00
// -----------------------------------------------------------------
// If your site deploys to a subdirectory, change `pathPrefix`.
// Dont worry about leading and trailing slashes, we normalize these.
2021-06-17 17:02:30 -06:00
// If you dont have a subdirectory, use "" or "/' (they do the same thing)
// This is only used for link URLs (it does not affect your file structure)
2020-07-17 08:36:28 -06:00
// Best paired with the `url` filter: https://www.11ty.dev/docs/filters/url/
// You can also pass this in on the command line using `--pathprefix`
2021-03-17 09:29:29 -06:00
// Optional (default is shown)
2021-06-17 17:02:30 -06:00
pathPrefix: '/',
2021-03-17 09:29:29 -06:00
// -----------------------------------------------------------------
// Pre-process *.md files with: (default: `liquid`)
2021-06-17 17:02:30 -06:00
markdownTemplateEngine: 'njk',
2021-03-17 09:29:29 -06:00
// Pre-process *.html files with: (default: `liquid`)
2021-06-17 17:02:30 -06:00
htmlTemplateEngine: 'njk',
2021-03-17 09:29:29 -06:00
// Opt-out of pre-processing global data JSON files: (default: `liquid`)
dataTemplateEngine: false,
// These are all optional (defaults are shown):
2018-01-16 20:08:47 -07:00
dir: {
2021-06-17 17:02:30 -06:00
input: '_src',
includes: '_includes',
data: '_data',
2022-10-20 09:53:02 -06:00
output: '_site'
}
2021-06-17 17:02:30 -06:00
}
}