Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use github-slugger for markdown anchors #16067

Merged
merged 3 commits into from Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions docs/.eleventy.js
Expand Up @@ -4,11 +4,11 @@ const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginTOC = require("eleventy-plugin-nesting-toc");
const slugify = require("slugify");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItContainer = require("markdown-it-container");
const Image = require("@11ty/eleventy-img");
const path = require("path");
const { slug } = require("github-slugger");
const yaml = require("js-yaml");

const {
Expand Down Expand Up @@ -60,16 +60,23 @@ module.exports = function(eleventyConfig) {

eleventyConfig.addFilter("jsonify", variable => JSON.stringify(variable));

/**
* Takes in a string and converts to a slug
* @param {string} text text to be converted into slug
* @returns {string} slug to be used as anchors
*/
function slugify(text) {
return slug(text.replace(/[<>()[\]{}]/gu, ""))
// eslint-disable-next-line no-control-regex -- used regex from https://github.com/eslint/archive-website/blob/master/_11ty/plugins/markdown-plugins.js#L37
.replace(/[^\u{00}-\u{FF}]/gu, "");
}

eleventyConfig.addFilter("slugify", str => {
if (!str) {
return "";
}

return slugify(str, {
lower: true,
strict: true,
remove: /["]/gu
});
return slugify(str);
});

eleventyConfig.addFilter("URIencode", str => {
Expand Down Expand Up @@ -154,7 +161,9 @@ module.exports = function(eleventyConfig) {

eleventyConfig.setLibrary("md",
markdownIt({ html: true, linkify: true, typographer: true })
.use(markdownItAnchor, {})
.use(markdownItAnchor, {
slugify
})
.use(markdownItContainer, "correct", {})
.use(markdownItContainer, "incorrect", {})
.disable("code"));
Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Expand Up @@ -28,6 +28,7 @@
"eleventy-plugin-nesting-toc": "^1.3.0",
"eleventy-plugin-page-assets": "^0.3.0",
"eleventy-plugin-reading-time": "^0.0.1",
"github-slugger": "^1.4.0",
"imagemin": "^8.0.1",
"imagemin-cli": "^7.0.0",
"js-yaml": "^3.14.1",
Expand All @@ -38,8 +39,7 @@
"netlify-cli": "^10.3.1",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"sass": "^1.52.1",
"slugify": "^1.6.3"
"sass": "^1.52.1"
},
"engines": {
"node": ">=14.0.0"
Expand Down