-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.eleventy.js
More file actions
174 lines (150 loc) · 5.16 KB
/
.eleventy.js
File metadata and controls
174 lines (150 loc) · 5.16 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const sass = require('sass');
const fs = require('node:fs');
const path = require('node:path');
const moment = require('moment');
const syntaxhighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const striptags = require('striptags');
// const upgradeHelper = require('@11ty/eleventy-upgrade-help');
// install prism plugins
require('prismjs/plugins/custom-class/prism-custom-class');
const REGEX_IMAGE_EXTENSION = '(jpg|png|gif)';
/**
* Restart program if you change configs!
*/
module.exports = function (eleventyConfig) {
// clear site on initial build
if (fs.existsSync('_site')) {
fs.rmSync('_site', { recursive: true });
}
console.log('Cleared _site folder');
const isServing = process.argv.includes('--serve');
if (isServing) {
// watch only files if we are serving
const watcher = fs.watch('src/styles', (event, filepath) => {
console.log('Styles file changed', filepath);
compileSass();
});
process.on('SIGINT', () => {
console.log('Stopping file watcher...');
watcher.close();
});
process.on('SIGTERM', () => {
console.log('Stopping file watcher...');
watcher.close();
});
}
compileSass();
eleventyConfig.addPassthroughCopy({
'./node_modules/@fortawesome/fontawesome-free': 'css/fontawesome',
});
eleventyConfig.addPassthroughCopy('src/CNAME');
eleventyConfig.addPassthroughCopy('src/images');
eleventyConfig.addPassthroughCopy(
`src/projects/**/*.${REGEX_IMAGE_EXTENSION}`
);
eleventyConfig.addPassthroughCopy({ webconfig: './' });
// Filter source file names using a glob
eleventyConfig.addCollection('posts', function (collectionApi) {
return collectionApi
.getFilteredByGlob('src/posts/*.md')
.filter(item => !item.data.unlisted)
.reverse();
});
eleventyConfig.addCollection('projects', function (collectionApi) {
return collectionApi.getFilteredByGlob('src/projects/**/*.md').reverse();
});
eleventyConfig.addPlugin(syntaxhighlight, {
init: function ({ Prism }) {
Prism.plugins.customClass.map({
// prefix tag and number to avoid conflicts with Bulma
tag: 'prism-tag',
number: 'prism-number',
});
},
});
eleventyConfig.addFilter('coverImage', filterCoverImage);
/**
* Generates a dynamic cover image
*/
eleventyConfig.addShortcode(
'dynamicImageOnHover',
shortcodeDynamicImageOnHover
);
/** Return year of today */
eleventyConfig.addShortcode('currentYear', function () {
return moment().format('YYYY');
});
eleventyConfig.addFilter('formatDate', function (date) {
return moment(date).format('MMMM YYYY');
});
eleventyConfig.addFilter('datetime', function (date) {
return moment(date).format('YYYY-MM-DD');
});
// generate anchor ids of headings
const markdownLib = markdownIt({ html: true }).use(markdownItAnchor);
eleventyConfig.setLibrary('md', markdownLib);
// generate Atom (RSS feed)
eleventyConfig.addPlugin(pluginRss);
// general config
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
exerpt_separator: '<!-- excerpt -->',
});
eleventyConfig.addWatchTarget('src/**/*.md');
// excerpts - use filter instead of shortcode to avoid templateContent timing issues
eleventyConfig.addFilter('excerpt', function (content) {
if (!content) return '';
const excerpt = striptags(content)
.substring(0, 200) // Cap at 200 characters
.replace(/^\s+|\s+$|\s+(?=\s)/g, '')
.trim();
return excerpt ? excerpt + '...' : '';
});
// configuration object
return {
dir: {
input: 'src',
},
jsDataFileSuffix: '.data',
};
};
async function compileSass() {
console.log('Compiling sass.');
const cssContent = sass.compile('src/styles/main.scss');
fs.mkdirSync('_site/css', { recursive: true });
fs.writeFileSync('_site/css/main.css', cssContent.css);
}
function shortcodeDynamicImageOnHover(
page,
staticCoverImage,
dynamicCoverImage
) {
const pageInputDir = path.dirname(page.inputPath);
const staticCoverImageInputPath = path.join(pageInputDir, staticCoverImage);
const dynamicCoverImageInputPath = path.join(pageInputDir, dynamicCoverImage);
const baseDir = page.url;
const staticCoverImagePath = path.join(baseDir, staticCoverImage);
const dynamicCoverImagePath = path.join(baseDir, dynamicCoverImage);
if (!fs.existsSync(staticCoverImageInputPath)) {
throw new Error(
`Static cover image not found. path=${staticCoverImageInputPath}`
);
}
if (fs.existsSync(dynamicCoverImageInputPath)) {
// dynamic cover image exists
return (
`<img class="is-absolute" src="${dynamicCoverImagePath}">` +
`<img class="is-hidden-on-hover" src="${staticCoverImagePath}">`
);
} else {
return `<img src="${staticCoverImagePath}">`;
}
}
function filterCoverImage(page) {
// take filePathStem or pageOptions directly if it is a string (and a path)
var pageOptions = typeof pageOptions === 'string' ? page : page.filePathStem;
return path.join(path.dirname(pageOptions), page.data.coverImageName);
}