Created
November 11, 2021 17:21
-
-
Save pechorin/9853d4e5ab574413434ba8a008fe28cb to your computer and use it in GitHub Desktop.
Eleventy example config (not finished)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { DateTime } = require("luxon"); | |
const fs = require("fs"); | |
const pluginRss = require("@11ty/eleventy-plugin-rss"); | |
const pluginNavigation = require("@11ty/eleventy-navigation"); | |
const markdownIt = require("markdown-it"); | |
const markdownItAnchor = require("markdown-it-anchor"); | |
const markdownItAttrs = require("markdown-it-attrs"); | |
const markdownItFootnotes = require('markdown-it-footnote'); | |
const markdownItPlantuml = require('markdown-it-plantuml'); | |
const { execSync } = require("child_process"); | |
module.exports = function(eleventyConfig) { | |
eleventyConfig.addPlugin(pluginRss); | |
eleventyConfig.addPlugin(pluginNavigation); | |
// Customize Markdown library and settings: | |
let markdownLibrary = markdownIt({ | |
html: true, | |
breaks: false, | |
linkify: true, | |
highlight: function(str, lang, extra) { | |
var result = "empty"; | |
var cmd = `chroma -f html -l ${lang} \ | |
--style="autumn" \ | |
--html-only \ | |
--html-prevent-surrounding-pre \ | |
--html-highlight=${extra} | |
`; | |
result = execSync(cmd, { input: str }).toString(); | |
// console.log('result -> ', result.toString()) | |
// remove \n at start and end of code blocks | |
result = result.replace("\n<table class=\"lntable\">", '<table class="lntable">') | |
result = result.replace("<table class=\"lntable\"><tr><td class=\"lntd\">\n(\s)*", "<table class=\"lntable\"><tr><td class=\"lntd\">") | |
result = result.replace("</td></tr></table>\n", "</td></tr></table>") | |
// console.log('result after-> ', result.toString()) | |
return result; | |
}, | |
}).use(markdownItAnchor, { | |
permalink: markdownItAnchor.permalink.ariaHidden({ | |
placement: "after", | |
class: "direct-link", | |
symbol: "#", | |
level: [1,2,3,4,5], | |
}), | |
slugify: eleventyConfig.getFilter("slug") | |
}).use(markdownItAttrs, { | |
}).use(markdownItFootnotes, { | |
}).use(markdownItPlantuml, { | |
openMarker: '```plantuml', | |
closeMarker: '```', | |
render: function(tokens, idx) { | |
var imageUrl = tokens[idx].attrGet('src') | |
return `<div class="uml-wrapper"><img class="uml" src="${imageUrl}"></img></div>` | |
} | |
}); | |
eleventyConfig.setLibrary("md", markdownLibrary); | |
// Override Browsersync defaults (used only with --serve) | |
eleventyConfig.setBrowserSyncConfig({ | |
callbacks: { | |
ready: function(err, browserSync) { | |
const content_404 = fs.readFileSync('build/404.html'); | |
browserSync.addMiddleware("*", (req, res) => { | |
// Provides the 404 content without redirect. | |
res.writeHead(404, {"Content-Type": "text/html; charset=UTF-8"}); | |
res.write(content_404); | |
res.end(); | |
}); | |
}, | |
}, | |
ui: false, | |
ghostMode: false | |
}); | |
// https://www.11ty.dev/docs/data-deep-merge/ | |
eleventyConfig.setDataDeepMerge(true); | |
eleventyConfig.addLayoutAlias("post", "post.njk"); | |
eleventyConfig.addLayoutAlias("application", "application.njk"); | |
eleventyConfig.addCollection("posts", function(collectionApi) { | |
return collectionApi.getFilteredByGlob("site/posts/*.md"); | |
}); | |
eleventyConfig.addWatchTarget('./assets/') | |
// eleventyConfig.addPassthroughCopy("img"); | |
// eleventyConfig.addPassthroughCopy("assets"); | |
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) => { | |
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd'); | |
}); | |
// Return the smallest number argument | |
eleventyConfig.addFilter("min", (...numbers) => { | |
return Math.min.apply(null, numbers); | |
}); | |
// Get the first `n` elements of a collection. | |
eleventyConfig.addFilter("head", (array, n) => { | |
if(!Array.isArray(array) || array.length === 0) { | |
return []; | |
} | |
if( n < 0 ) { | |
return array.slice(n); | |
} | |
return array.slice(0, n); | |
}); | |
function withoutBlacklistedTags(tags) { | |
return (tags || []).filter(tag => ["all", "nav", "post", "posts"].indexOf(tag) === -1); | |
} | |
eleventyConfig.addFilter("withoutBlacklistedTags", withoutBlacklistedTags) | |
// Create an array of all tags | |
eleventyConfig.addCollection("tags", function(collection) { | |
let tagSet = new Set(); | |
collection.getAll().forEach(item => { | |
(item.data.tags || []).forEach(tag => tagSet.add(tag)); | |
}); | |
return withoutBlacklistedTags([...tagSet]); | |
}); | |
eleventyConfig.on("afterBuild", function() { | |
fs.readFile(".revision", "utf-8", (err, data) => { | |
if (err) { | |
console.log(".revision file read error:", err) | |
return | |
} else { | |
var last_revision = parseInt(data) | |
var new_revision = last_revision + 1 | |
fs.writeFile('.revision', String(new_revision), err => { | |
if (err) { | |
console.log(".revision file write error:", err) | |
return | |
} | |
}) | |
} | |
}) | |
}) | |
return { | |
templateFormats: [ | |
"md", | |
"njk", | |
"html", | |
], | |
markdownTemplateEngine: "njk", | |
htmlTemplateEngine: "njk", | |
dataTemplateEngine: false, | |
// passthroughFileCopy: true, | |
dir: { | |
input: "./site/", | |
output: "./build/", | |
includes: "includes", | |
layouts: "layouts", | |
data: "data", | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment