From c48422c39e774da29141e43ffe9d6a2ceeb42565 Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Sat, 15 Mar 2025 13:54:13 +0530 Subject: [PATCH] feat: add syntax highlighting for code blocks on plugin page --- src/pages/plugin/plugin.js | 27 ++++++++++++++++++++++++++- src/pages/plugin/plugin.scss | 8 ++++++-- www/index.html | 3 ++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/pages/plugin/plugin.js b/src/pages/plugin/plugin.js index 6c3598b5..716f86bb 100644 --- a/src/pages/plugin/plugin.js +++ b/src/pages/plugin/plugin.js @@ -308,7 +308,10 @@ export default async function PluginInclude( const pluginSettings = settings.uiSettings[`plugin-${plugin.id}`]; $page.body = view({ ...plugin, - body: markdownIt({ html: true, xhtmlOut: true }) + body: markdownIt({ + html: true, + xhtmlOut: true, + }) .use(MarkdownItGitHubAlerts) .use(anchor, { slugify: (s) => @@ -394,6 +397,28 @@ export default async function PluginInclude( copyButton.className = "copy-button"; copyButton.textContent = "Copy"; + const codeElement = pre.querySelector("code"); + if (codeElement) { + const langMatch = codeElement.className.match( + /language-(\w+)|(javascript)/, + ); + if (langMatch) { + const langMap = { + bash: "sh", + shell: "sh", + }; + const lang = langMatch[1] || langMatch[2]; + const mappedLang = langMap[lang] || lang; + const highlight = ace.require("ace/ext/static_highlight"); + highlight(codeElement, { + mode: `ace/mode/${mappedLang}`, + theme: settings.value.editorTheme.startsWith("ace/theme/") + ? settings.value.editorTheme + : "ace/theme/" + settings.value.editorTheme, + }); + } + } + copyButton.addEventListener("click", async () => { const code = pre.querySelector("code")?.textContent || pre.textContent; try { diff --git a/src/pages/plugin/plugin.scss b/src/pages/plugin/plugin.scss index e32c6387..fdb45b57 100644 --- a/src/pages/plugin/plugin.scss +++ b/src/pages/plugin/plugin.scss @@ -191,15 +191,19 @@ word-wrap: normal; white-space: pre; font-size: 0.8rem; - padding: 5px; + border-radius: 4px; + padding: 1em; margin: 5px 0; background: var(--primary-color); color: var(--primary-text-color); - border-left: solid 5px var(--active-color); + /* border-left: solid 5px var(--active-color); */ overflow: auto; width: calc(100% - 10px); display: block; user-select: text; + div { + background: var(--primary-color); + } } } } diff --git a/www/index.html b/www/index.html index aa1ce8ce..6ceca12d 100644 --- a/www/index.html +++ b/www/index.html @@ -47,7 +47,7 @@ type, listener, useCapture, - allowed + allowed, ) { if (typeof useCapture === "boolean") { allowed = useCapture; @@ -101,6 +101,7 @@ +