-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize the 11ty utilities and bigger font
- Loading branch information
Showing
18 changed files
with
145 additions
and
118 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,34 @@ | ||
const markdownIt = require("markdown-it"); | ||
const eleventyPluginFilesMinifier = require("@sherby/eleventy-plugin-files-minifier"); | ||
|
||
function slugify(text) { | ||
return text.toLowerCase().trim().replace(/\s+/g, "-").replace(/[^\w\-]+/g, ""); | ||
} | ||
|
||
module.exports = function(eleventyConfig) { | ||
|
||
eleventyConfig.addPlugin(eleventyPluginFilesMinifier); | ||
|
||
eleventyConfig.addFilter("urlEncode", function(value) { | ||
return encodeURIComponent(value); | ||
}); | ||
|
||
const md = markdownIt({ html: true }).use((md) => { | ||
const defaultRender = md.renderer.rules.heading_close || function (tokens, idx) { | ||
return md.renderer.renderToken(tokens, idx); | ||
}; | ||
|
||
md.renderer.rules.heading_open = (tokens, idx) => { | ||
const content = tokens[idx + 1] && tokens[idx + 1].content ? tokens[idx + 1].content : "heading"; | ||
const slug = slugify(content); | ||
tokens[idx].attrSet("id", slug); | ||
return `<${tokens[idx].tag} id="${slug}">`; | ||
}; | ||
|
||
md.renderer.rules.heading_close = (tokens, idx) => { | ||
const content = tokens[idx - 1] && tokens[idx - 1].content ? tokens[idx - 1].content : "heading"; | ||
const slug = slugify(content); | ||
return `<a href="#${slug}" class="header-anchor" aria-label="Permalink to ${content}"><div class="tooltip">Permanent link</div></a></${tokens[idx].tag}>`; | ||
}; | ||
}); | ||
|
||
eleventyConfig.setLibrary("md", md); | ||
|
||
eleventyConfig.addFilter("date", (dateObj) => { | ||
const year = dateObj.getFullYear(); | ||
const month = String(dateObj.getMonth() + 1).padStart(2, "0"); | ||
const day = String(dateObj.getDate()).padStart(2, "0"); | ||
return `${year}-${month}-${day}`; | ||
}); | ||
|
||
eleventyConfig.addCollection("posts", (collectionApi) => | ||
collectionApi.getFilteredByGlob("src/posts/*.md") | ||
); | ||
|
||
eleventyConfig.addFilter("filterByTag", (posts, tag) => | ||
posts.filter(post => post.data.tags?.includes(tag)) | ||
); | ||
|
||
eleventyConfig.addCollection("tagsList", (collectionApi) => { | ||
const tagsSet = new Set(); | ||
collectionApi.getAll().forEach(item => { | ||
item.data.tags?.forEach(tag => tagsSet.add(tag)); | ||
}); | ||
return [...tagsSet]; | ||
}); | ||
|
||
eleventyConfig.addLiquidShortcode('image', (filename, alt, size, loading) => { | ||
const [width, height] = size.split('x'); | ||
return `<img src="${filename}" alt="${alt}" width="${width}" height="${height}" loading="${loading}" />`; | ||
}); | ||
const slugify = require("slugify"); | ||
const configureMarkdown = require("./_utilities/markdown"); | ||
const filesMinifier = require("./_utilities/filesMinifier"); | ||
const dateFilter = require("./_utilities/dateFilter"); | ||
const urlEncode = require("./_utilities/urlEncode"); | ||
const postsCollection = require("./_utilities/postsCollection"); | ||
const tagsListCollection = require("./_utilities/tagsListCollection"); | ||
const filterByTag = require("./_utilities/filterByTag"); | ||
const imageShortcode = require("./_utilities/imageShortcode"); | ||
|
||
module.exports = function (eleventyConfig) { | ||
|
||
configureMarkdown(eleventyConfig, slugify); | ||
filesMinifier(eleventyConfig); | ||
eleventyConfig.addFilter("date", dateFilter); | ||
eleventyConfig.addFilter("urlEncode", urlEncode); | ||
eleventyConfig.addCollection("posts", postsCollection); | ||
eleventyConfig.addCollection("tagsList", tagsListCollection); | ||
eleventyConfig.addFilter("filterByTag", filterByTag); | ||
eleventyConfig.addLiquidShortcode("image", imageShortcode); | ||
|
||
eleventyConfig.addPassthroughCopy({ | ||
"src/public": "/", | ||
"node_modules/beercss/dist/cdn": "/modules/beercss", | ||
"node_modules/material-dynamic-colors/dist/cdn": "/modules/material-dynamic-colors", | ||
"node_modules/simple-icons-font/font": "/modules/simple-icons-font" | ||
}); | ||
|
||
eleventyConfig.addPassthroughCopy({ "src/public": "/" }); | ||
|
||
return { | ||
dir: { | ||
input: "src", | ||
output: "_site", | ||
}, | ||
}; | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function dateFilter(dateObj) { | ||
const year = dateObj.getFullYear(); | ||
const month = String(dateObj.getMonth() + 1).padStart(2, "0"); | ||
const day = String(dateObj.getDate()).padStart(2, "0"); | ||
return `${year}-${month}-${day}`; | ||
} | ||
|
||
module.exports = dateFilter; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const filesMinifier = require("@sherby/eleventy-plugin-files-minifier"); | ||
|
||
function configureFilesMinifier(eleventyConfig) { | ||
eleventyConfig.addPlugin(filesMinifier); | ||
} | ||
|
||
module.exports = configureFilesMinifier; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function filterByTag(posts, tag) { | ||
return posts.filter((post) => post.data.tags?.includes(tag)); | ||
} | ||
|
||
module.exports = filterByTag; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function imageShortcode(filename, alt, size, loading = "lazy") { | ||
const [width, height] = size.split("x"); | ||
return `<img src="${filename}" alt="${alt}" width="${width}" height="${height}" loading="${loading}" />`; | ||
} | ||
|
||
module.exports = imageShortcode; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const markdownIt = require("markdown-it"); | ||
|
||
function configureMarkdown(eleventyConfig, slugify) { | ||
const md = markdownIt({ html: true }).use((md) => { | ||
md.renderer.rules.heading_open = (tokens, idx) => { | ||
const content = tokens[idx + 1]?.content || "heading"; | ||
const slug = slugify(content); | ||
tokens[idx].attrSet("id", slug); | ||
return `<${tokens[idx].tag} id="${slug}">`; | ||
}; | ||
|
||
md.renderer.rules.heading_close = (tokens, idx) => { | ||
const content = tokens[idx - 1]?.content || "heading"; | ||
const slug = slugify(content); | ||
return `<a href="#${slug}" class="header-anchor" aria-label="Permalink to ${content}"><div class="tooltip">Permanent link</div></a></${tokens[idx].tag}>`; | ||
}; | ||
}); | ||
|
||
eleventyConfig.setLibrary("md", md); | ||
} | ||
|
||
module.exports = configureMarkdown; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function postsCollection(collectionApi) { | ||
return collectionApi.getFilteredByGlob("src/posts/*.md"); | ||
} | ||
|
||
module.exports = postsCollection; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function slugify(text) { | ||
return text.toLowerCase().trim().replace(/\s+/g, "-").replace(/[^\w\-]+/g, ""); | ||
} | ||
|
||
module.exports = slugify; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function tagsListCollection(collectionApi) { | ||
const tagsSet = new Set(); | ||
collectionApi.getAll().forEach((item) => { | ||
item.data.tags?.forEach((tag) => tagsSet.add(tag)); | ||
}); | ||
return [...tagsSet]; | ||
} | ||
|
||
module.exports = tagsListCollection; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function urlEncode(value) { | ||
return encodeURIComponent(value); | ||
} | ||
|
||
module.exports = urlEncode; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
Oops, something went wrong.