From d6a4b28c574aaac4dc586689cc80133dbc6f2274 Mon Sep 17 00:00:00 2001 From: "FAIRY\\CosmoLau" Date: Mon, 28 Oct 2024 13:51:12 +0800 Subject: [PATCH] feat: add code block language display --- src/components/misc/Markdown.astro | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/components/misc/Markdown.astro b/src/components/misc/Markdown.astro index 68eff8ec3..b02ee1fd9 100644 --- a/src/components/misc/Markdown.astro +++ b/src/components/misc/Markdown.astro @@ -31,7 +31,7 @@ const className = Astro.props.class wrapper.className = "relative code-block"; let copyButton = document.createElement("button"); - copyButton.className = "copy-btn btn-regular-dark absolute active:scale-90 h-8 w-8 top-2 right-2 opacity-75 text-sm p-1.5 rounded-lg transition-all ease-in-out"; + copyButton.className = "copy-btn btn-regular-dark absolute active:scale-90 h-8 w-8 top-2 right-2 opacity-75 text-sm p-1.5 rounded-lg transition-all ease-in-out hidden"; codeBlock.setAttribute("tabindex", "0"); if (codeBlock.parentNode) { @@ -43,8 +43,24 @@ const className = Astro.props.class copyButton.innerHTML = `
${copyIcon} ${successIcon}
` + let dataLang = document.createElement("div"); + dataLang.className = "dataLang absolute h-8 w-auto top-0.5 right-2 text-xs text-right p-1.5 rounded-lg transition-all ease-in-out"; + dataLang.innerHTML = ` +
${codeBlock.getAttribute("data-language")}
+ `; + wrapper.appendChild(codeBlock); wrapper.appendChild(copyButton); + wrapper.appendChild(dataLang); + + wrapper.onmouseover = (event) => { + copyButton.style.display = "block"; + dataLang.style.display = "none"; + } + wrapper.onmouseout = (event) => { + copyButton.style.display = "none"; + dataLang.style.display = "block"; + } let timeout: ReturnType; copyButton.addEventListener("click", async () => { @@ -148,6 +164,7 @@ const className = Astro.props.class pre background: var(--codeblock-bg) !important border-radius: 0.75rem + padding-top: 1.25rem padding-left: 1.25rem padding-right: 1.25rem @@ -163,6 +180,9 @@ const className = Astro.props.class span.br::selection background: var(--codeblock-selection) + .dataLang + color: var(--primary) + ul li &::marker