From 6494ead6676b9cf4f32d9245cd78bfe1a013b1dc Mon Sep 17 00:00:00 2001 From: M Sarmad Qadeer Date: Tue, 6 Sep 2022 22:54:37 +0500 Subject: [PATCH] use markdown-it-replace-link to convert relative .md extension to .html extension --- website/.eleventy.js | 32 ++++++++++++++++++++-- website/package.json | 3 ++ website/src/_includes/layouts/article.html | 2 -- website/src/js/article.js | 22 --------------- 4 files changed, 33 insertions(+), 26 deletions(-) delete mode 100644 website/src/js/article.js diff --git a/website/.eleventy.js b/website/.eleventy.js index 411ef958d5..ddbed4bc9c 100644 --- a/website/.eleventy.js +++ b/website/.eleventy.js @@ -18,19 +18,47 @@ module.exports = function (eleventyConfig) { const markdownIt = require("markdown-it"); const markdownItAnchor = require("markdown-it-anchor"); + const markdownItReplaceLink = require('markdown-it-replace-link'); const slugify = require("slugify"); const markdownLib = markdownIt({ html: true, breaks: true, - linkify: true + linkify: true, + replaceLink: function (link, env) { + const dotSepList = link.split("."); + if (dotSepList[0] == "") { + const hashSepList = dotSepList[dotSepList.length - 1].split("#") + if (hashSepList[0] == "md") { + let str = `.${dotSepList[1]}`; + for (let i = 2; i < dotSepList.length; i++) { + if (dotSepList[i].substring(0, 2) != "md") { + str += "." + dotSepList[i]; + } else { + str += ".html"; + break; + }; + } + if (hashSepList[1]) { + str += "#" + hashSepList[1]; + } + return str; + } + else { + return link; + } + } + else { + return link; + } + } }).use(markdownItAnchor, { slugify: (str) => slugify(str, { lower: true, strict: true, }) - }); + }).use(markdownItReplaceLink); // replace the default markdown-it instance eleventyConfig.setLibrary("md", markdownLib); diff --git a/website/package.json b/website/package.json index 38b531cdff..ef8bf89b4d 100644 --- a/website/package.json +++ b/website/package.json @@ -19,5 +19,8 @@ "markdown-it-anchor": "^8.6.4", "slugify": "^1.6.5", "tailwindcss": "^3.0.24" + }, + "dependencies": { + "markdown-it-replace-link": "^1.1.0" } } diff --git a/website/src/_includes/layouts/article.html b/website/src/_includes/layouts/article.html index ab67b29a81..2982be2912 100644 --- a/website/src/_includes/layouts/article.html +++ b/website/src/_includes/layouts/article.html @@ -19,8 +19,6 @@
{{ content | safe }}
{% include "footer.html" %} - - diff --git a/website/src/js/article.js b/website/src/js/article.js deleted file mode 100644 index e38f489e5a..0000000000 --- a/website/src/js/article.js +++ /dev/null @@ -1,22 +0,0 @@ -// Convert relative blog extension (from .md to .html) -document.querySelectorAll("a").forEach((a) => { - const dotSepList = a.getAttribute("href").split("."); - if (dotSepList[0] == "") { - const hashSepList = dotSepList[dotSepList.length - 1].split("#") - if (hashSepList[0] == "md") { - let str = `.${dotSepList[1]}`; - for (let i = 2; i < dotSepList.length; i++) { - if (dotSepList[i].substring(0, 2) != "md") { - str += "." + dotSepList[i]; - } else { - str += ".html"; - break; - }; - } - if (hashSepList[1]) { - str += "#" + hashSepList[1]; - } - a.setAttribute("href", str); - } - } -}); \ No newline at end of file