-
Notifications
You must be signed in to change notification settings - Fork 23
/
.eleventy.js
136 lines (116 loc) · 3.49 KB
/
.eleventy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const pluginTailwindCSS = require('eleventy-plugin-tailwindcss')
const htmlmin = require('html-minifier')
const markdownIt = require('markdown-it')
const Image = require('@11ty/eleventy-img')
const path = require('path')
const pluginTOC = require('eleventy-plugin-toc')
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight')
const markdownOptions = {
html: true,
breaks: false,
linkify: true,
}
module.exports = function (eleventyConfig) {
const isProduction = process.env.NODE_ENV == 'production'
// Copy assets
eleventyConfig.addPassthroughCopy({ 'src/assets': '.' })
// Tailwind
eleventyConfig.addPlugin(pluginTailwindCSS, {
src: 'src/styles/main.css',
dest: '.',
keepFolderStructure: false,
minify: isProduction,
watchEleventyWatchTargets: true,
})
// Syntax highlighting
eleventyConfig.addPlugin(syntaxHighlight)
// Markdown
setupMarkdown(eleventyConfig)
// Table of contents
eleventyConfig.addPlugin(pluginTOC)
// Optimize images
eleventyConfig.addAsyncShortcode(
'optimizedImage',
async function (src, alt, attrs = '') {
if (!path.isAbsolute(src)) {
src = path.join(path.dirname(this.page.inputPath), src)
} else {
src = path.join('.', src)
}
if (alt === undefined) {
throw new Error(`Missing \`alt\` on image shortcode from: ${src}`)
}
let stats = await Image(src, {
widths: [640, null],
formats: ['webp', 'jpeg'],
outputDir: './_site/images',
urlPath: '/images/',
})
let lowestSrc = stats['jpeg'][0]
let sizes = '100vw' // Make sure you customize this!
// Iterate over formats and widths
return `<picture ${attrs} >
${Object.values(stats)
.map(imageFormat => {
return ` <source type="image/${
imageFormat[0].format
}" srcset="${imageFormat
.map(entry => entry.srcset)
.join(', ')}" sizes="${sizes}">`
})
.join('\n')}
<img
src="${lowestSrc.url}"
alt="${alt}">
</picture>`
}
)
// Minify HTML in production
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
if (isProduction && outputPath.endsWith('.html')) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
})
return minified
}
return content
})
// Custom filters
eleventyConfig.addShortcode('paragraph', (text, classes) => {
return text
.split('\n\n')
.map(par => `<p class="${classes}">${par}</p>`)
.join('\n')
})
eleventyConfig.addFilter('markdown', text => {
const md = markdownIt(markdownOptions)
return md.render(text)
})
eleventyConfig.addFilter('debug', obj => {
console.log('DEBUG', obj)
})
eleventyConfig.addFilter('docsNavGroupItems', (group, collection) => {
return collection
.filter(x => x.data.group == group)
.sort((a, b) => a.data.priority - b.data.priority)
})
}
const setupMarkdown = eleventyConfig => {
const linkAnchorOptions = {
permalink: true,
permalinkAttrs: () => ({ 'aria-label': 'Anchor' }),
permalinkClass: 'header-anchor',
permalinkSymbol: '',
permalinkBefore: true,
}
const md = markdownIt(markdownOptions).use(
require('markdown-it-anchor'),
linkAnchorOptions
)
eleventyConfig.setLibrary('md', md)
eleventyConfig.addFilter('markdown', markdown => {
return md.render(markdown)
})
}