From 42af5ebcddc023cb10a9c42e1a8b3fd77793ace2 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 10 Aug 2023 15:15:06 +0100 Subject: [PATCH] refactor: delegate refresh to UI class --- src/ts/UI.ts | 32 ++++++++++++++++++++++++++++++++ src/ts/app.ts | 14 +++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/ts/UI.ts b/src/ts/UI.ts index f3cae5e..a4874f1 100644 --- a/src/ts/UI.ts +++ b/src/ts/UI.ts @@ -6,6 +6,38 @@ public static get blogPostTemplate(): HTMLDivElement { return document.querySelector("#blog-post-template"); } + + /** + * Forces all UI elements to update. + */ + public static updateUI() { + UI.addLineNumbers(); + UI.unescapeMarkTags(); + } + + /** + * Adds line numbers to all
  blocks that have more than one line.
+     */
+    public static addLineNumbers() {
+        document.querySelectorAll("pre code").forEach((block) => {
+            let content = block.textContent;
+            if (content.trim().split("\n").length > 1) {
+                block.parentElement.classList.add("line-numbers");
+            }
+        });
+    }
+
+    /**
+     * Unescapes all  tags in 
  blocks.
+     */
+    public static unescapeMarkTags() {
+        document.querySelectorAll("pre code").forEach((block) => {
+            let content = block.innerHTML;
+            content = content.replaceAll("<mark>", "");
+            content = content.replaceAll("</mark>", "");
+            block.innerHTML = content;
+        });
+    }
 }
 
 export default UI;
\ No newline at end of file
diff --git a/src/ts/app.ts b/src/ts/app.ts
index 8f51690..bd003ff 100644
--- a/src/ts/app.ts
+++ b/src/ts/app.ts
@@ -52,6 +52,8 @@ declare const Handlebars: any;
             disqusCounter.src = "https://oliverbooth-dev.disqus.com/count.js";
             disqusCounter.async = true;
 
+            UI.updateUI();
+
             const spinner = document.querySelector("#blog-loading-spinner");
             if (spinner) {
                 spinner.classList.add("removed");
@@ -60,17 +62,7 @@ declare const Handlebars: any;
         });
     }
 
-    document.querySelectorAll("pre code").forEach((block) => {
-        let content = block.textContent;
-        if (content.trim().split("\n").length > 1) {
-            block.parentElement.classList.add("line-numbers");
-        }
-
-        content = block.innerHTML;
-        content = content.replaceAll("<mark>", "");
-        content = content.replaceAll("</mark>", "");
-        block.innerHTML = content;
-    });
+    UI.updateUI();
 
     const tex = document.getElementsByClassName("math");
     Array.prototype.forEach.call(tex, function (el) {