|
| 1 | +const fs = require('fs'); |
| 2 | +const pluginRss = require('@11ty/eleventy-plugin-rss'); |
| 3 | +const sassWatch = require('./_includes/sass-watch'); |
| 4 | +const filterFullDate = require('./_includes/filters/full-date'); |
| 5 | +const filterLimitTo = require('./_includes/filters/limit-to'); |
| 6 | +const filterMarkdown = require('./_includes/filters/markdown'); |
| 7 | +const filterRegexReplace = require('./_includes/filters/regex-replace'); |
| 8 | +const scMeetupDetails = require('./_includes/shortcodes/meetup-details'); |
| 9 | +const scVideoPlayer = require('./_includes/shortcodes/video-player'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Add date properties to collections. |
| 13 | + * |
| 14 | + * @param {object} posts Collection to add date properties to. |
| 15 | + * @return {object} Augmented posts collection. |
| 16 | + */ |
| 17 | +const addFileDates = (posts) => posts.map((post) => { |
| 18 | + if (!(post && post.inputPath)) return post; |
| 19 | + |
| 20 | + const stat = fs.statSync(post.inputPath) || {}; |
| 21 | + const newPost = post; |
| 22 | + |
| 23 | + newPost.dateCreated = stat.birthtime; |
| 24 | + newPost.dateModified = stat.mtime; |
| 25 | + |
| 26 | + return newPost; |
| 27 | +}); |
| 28 | + |
| 29 | +module.exports = (eleventyConfig) => { |
| 30 | + // Watch Sass directory for styling changes. |
| 31 | + // Works only in dev mode. Though it throws and error and then continues on. |
| 32 | + if (process.env.ELEVENTY_ENV === 'dev') { |
| 33 | + sassWatch('./_sass/_main.scss', './_site/assets/css/main.css'); |
| 34 | + } |
| 35 | + |
| 36 | + // PASSTHRU: Copy the `assets` directory to the compiled site folder |
| 37 | + eleventyConfig.addPassthroughCopy('assets'); |
| 38 | + eleventyConfig.addPassthroughCopy('robots.txt'); |
| 39 | + eleventyConfig.addPassthroughCopy('CNAME'); |
| 40 | + |
| 41 | + // COLLECTION: Create meetup collection. |
| 42 | + eleventyConfig.addCollection('meetups', (collection) => { |
| 43 | + const posts = collection.getFilteredByGlob('./_meetups/**'); |
| 44 | + |
| 45 | + return addFileDates(posts); |
| 46 | + }); |
| 47 | + |
| 48 | + // COLLECTION: Create post collection. |
| 49 | + eleventyConfig.addCollection('posts', (collection) => { |
| 50 | + const posts = collection.getFilteredByGlob(['./_meetups/**', './_posts/**']); |
| 51 | + |
| 52 | + return addFileDates(posts); |
| 53 | + }); |
| 54 | + |
| 55 | + // FILTER: Convert dates to MMMM D, YYYY format. |
| 56 | + eleventyConfig.addFilter('fullDate', filterFullDate); |
| 57 | + |
| 58 | + // FILTER: Limit collection length. |
| 59 | + eleventyConfig.addFilter('limitTo', filterLimitTo); |
| 60 | + |
| 61 | + // FILTER: Run content thru Markdown-it. |
| 62 | + eleventyConfig.addFilter('markdown', filterMarkdown); |
| 63 | + |
| 64 | + // FILTER: Replace text with regex capabilities. |
| 65 | + eleventyConfig.addFilter('regexReplace', filterRegexReplace); |
| 66 | + |
| 67 | + // SHORTCODE: Format meeting details message block. |
| 68 | + eleventyConfig.addShortcode('meetupDetails', scMeetupDetails); |
| 69 | + |
| 70 | + // SHORTCODE: Embed video players for event replay. |
| 71 | + eleventyConfig.addShortcode('videoPlayer', scVideoPlayer); |
| 72 | + |
| 73 | + // PLUGIN: RSS feed |
| 74 | + eleventyConfig.addPlugin(pluginRss); |
| 75 | + |
| 76 | + // BROWSERSYNC: add ability to see 404.html in dev mode |
| 77 | + eleventyConfig.setBrowserSyncConfig({ |
| 78 | + callbacks: { |
| 79 | + ready(err, bs) { |
| 80 | + bs.addMiddleware('*', (req, res) => { |
| 81 | + const content_404 = fs.readFileSync('_site/404.html'); |
| 82 | + // Provides the 404 content without redirect. |
| 83 | + |
| 84 | + res.write(content_404); |
| 85 | + // Add 404 http status code in request header. |
| 86 | + // res.writeHead(404, { "Content-Type": "text/html" }); |
| 87 | + res.writeHead(404); |
| 88 | + res.end(); |
| 89 | + }); |
| 90 | + }, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + return { |
| 95 | + dir: { |
| 96 | + input: './', |
| 97 | + output: './_site', |
| 98 | + layouts: './_layouts', |
| 99 | + }, |
| 100 | + templateFormats: [ |
| 101 | + 'njk', |
| 102 | + 'liquid', |
| 103 | + 'md', |
| 104 | + 'html', |
| 105 | + ], |
| 106 | + htmlTemplateEngine: 'njk', |
| 107 | + dataTemplateEngine: 'njk', |
| 108 | + }; |
| 109 | +}; |
0 commit comments