Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add code block language display #208

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/components/misc/Markdown.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -43,8 +43,24 @@ const className = Astro.props.class
copyButton.innerHTML = `<div>${copyIcon} ${successIcon}</div>
`

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 = `
<div>${codeBlock.getAttribute("data-language")}</div>
`;

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<typeof setTimeout>;
copyButton.addEventListener("click", async () => {
Expand Down Expand Up @@ -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

Expand All @@ -163,6 +180,9 @@ const className = Astro.props.class
span.br::selection
background: var(--codeblock-selection)

.dataLang
color: var(--primary)

ul
li
&::marker
Expand Down