From d6e2fffcaf0c9fd0f1cb8eb1f6703e4fbfc9d55f Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Tue, 4 Jan 2022 21:43:51 +0100 Subject: [PATCH] Add pretty print code selectors on load --- web/index.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/web/index.js b/web/index.js index e9cc0b7..2c02d70 100644 --- a/web/index.js +++ b/web/index.js @@ -1,8 +1,34 @@ -import "code-prettify/styles/desert.css" -import "bootstrap-icons/font/bootstrap-icons.css" +import "code-prettify/styles/desert.css"; +import "bootstrap-icons/font/bootstrap-icons.css"; + +import "code-prettify"; + +import "./style/index.scss"; + +const applyPrettyPrint = () => { + + const blogPosts = document.getElementsByClassName("blog-post-content"); + for (const blogPost of blogPosts) { + + const codeBlocks = blogPost.getElementsByTagName("code"); + for (const codeBlock of codeBlocks) { + codeBlock.parentElement.classList.add("prettyprint", "linenums", "kg-card", "kg-code-card"); + } + } + + PR.prettyPrint(); +} + +const init = () => { + applyPrettyPrint(); +} + +if (document.readyState === "complete" || document.readyState === "interactive") { + requestAnimationFrame(init); +} else { + document.addEventListener("DOMContentLoaded", init); +} + -import "code-prettify" -import "./style/index.scss" -PR.prettyPrint();