Skip to content

feat: introduce Expressive Code for code block rendering #476

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

Merged
merged 12 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: improve styles of Expressive Code
  • Loading branch information
saicaca committed Jun 1, 2025
commit 3fbbb9df2e25429886e2d329149e2db8c165613c
2 changes: 2 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { GithubCardComponent } from "./src/plugins/rehype-component-github-card.
import { parseDirectiveNode } from "./src/plugins/remark-directive-rehype.js";
import { remarkExcerpt } from "./src/plugins/remark-excerpt.js";
import { remarkReadingTime } from "./src/plugins/remark-reading-time.mjs";
import { pluginCustomCopyButton } from "./src/plugins/expressive-code/custom-copy-button.js";

// https://astro.build/config
export default defineConfig({
Expand Down Expand Up @@ -60,6 +61,7 @@ export default defineConfig({
pluginCollapsibleSections(),
pluginLineNumbers(),
pluginLanguageBadge(),
pluginCustomCopyButton(),
],
defaultProps: {
wrap: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@astrojs/ts-plugin": "^1.10.4",
"@biomejs/biome": "1.9.4",
"@rollup/plugin-yaml": "^4.1.2",
"@types/hast": "^3.0.4",
"@types/markdown-it": "^14.1.2",
"@types/mdast": "^4.0.4",
"@types/sanitize-html": "^2.16.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/components/misc/Markdown.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,24 @@ const className = Astro.props.class;
<!--<div class="max-w-none custom-md">-->
<slot/>
</div>

<script>
let timeout: ReturnType<typeof setTimeout>;
document.addEventListener("click", function (e) {
if (e.target && e.target?.classList.contains("copy-btn")) {
const preEle = e.target.closest("pre");
const codeEle = preEle?.querySelector("code");
const code = Array.from(codeEle.querySelectorAll(".code:not(summary *)"))
.map(e => e.textContent)
.map(t => t === "\n" ? "" : t)
.join("\n");
navigator.clipboard.writeText(code);
e.target.classList.add("success");
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
e.target.classList.remove("success");
}, 1000);}
});
</script>
82 changes: 82 additions & 0 deletions src/plugins/expressive-code/custom-copy-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { definePlugin } from "@expressive-code/core"
import type { Element, ElementContent } from 'hast';

export function pluginCustomCopyButton() {
return definePlugin({
name: "Custom Copy Button",
hooks: {
postprocessRenderedBlock: context => {
function traverse(node: Element) {
if (node.type === "element" && node.tagName === "pre") {
processCodeBlock(node)
return
}
if (node.children) {
for (const child of node.children) {
if (child.type === "element")
traverse(child)
}
}
}

function processCodeBlock(node: Element) {
const copyButton = {
type: "element" as const,
tagName: "button",
properties: {
className: ["copy-btn"],
"aria-label": "Copy code",
},
children: [{
type: "element" as const,
tagName: "div",
properties: {
className: ["copy-btn-icon"],
},
children: [{
type: "element" as const,
tagName: "svg",
properties: {
viewBox: "0 -960 960 960",
xmlns: "http://www.w3.org/2000/svg",
className: ["copy-btn-icon", "copy-icon"]
},
children: [{
type: "element" as const,
tagName: "path",
properties: {
d: "M368.37-237.37q-34.48 0-58.74-24.26-24.26-24.26-24.26-58.74v-474.26q0-34.48 24.26-58.74 24.26-24.26 58.74-24.26h378.26q34.48 0 58.74 24.26 24.26 24.26 24.26 58.74v474.26q0 34.48-24.26 58.74-24.26 24.26-58.74 24.26H368.37Zm0-83h378.26v-474.26H368.37v474.26Zm-155 238q-34.48 0-58.74-24.26-24.26-24.26-24.26-58.74v-515.76q0-17.45 11.96-29.48 11.97-12.02 29.33-12.02t29.54 12.02q12.17 12.03 12.17 29.48v515.76h419.76q17.45 0 29.48 11.96 12.02 11.97 12.02 29.33t-12.02 29.54q-12.03 12.17-29.48 12.17H213.37Zm155-238v-474.26 474.26Z"
},
children: []
}]
}, {
type: "element" as const,
tagName: "svg",
properties: {
viewBox: "0 -960 960 960",
xmlns: "http://www.w3.org/2000/svg",
className: ["copy-btn-icon", "success-icon"]
},
children: [{
type: "element" as const,
tagName: "path",
properties: {
d: "m389-377.13 294.7-294.7q12.58-12.67 29.52-12.67 16.93 0 29.61 12.67 12.67 12.68 12.67 29.53 0 16.86-12.28 29.14L419.07-288.41q-12.59 12.67-29.52 12.67-16.94 0-29.62-12.67L217.41-430.93q-12.67-12.68-12.79-29.45-.12-16.77 12.55-29.45 12.68-12.67 29.62-12.67 16.93 0 29.28 12.67L389-377.13Z"
},
children: []
}]
}]
}],
} as Element;

if (!node.children) {
node.children = [];
}
node.children.push(copyButton);
}

traverse(context.renderData.blockAst)
}
}
})
}
8 changes: 4 additions & 4 deletions src/plugins/expressive-code/language-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export function pluginLanguageBadge() {
right: calc(${cssVar('borderWidth')} + ${cssVar('uiPaddingInline')} / 2);
top: calc(${cssVar('borderWidth')} + 0.35rem);
padding: 0.1rem 0.5rem;
box-shadow: 0 0 1px 1px ${cssVar('codeBackground')};
content: attr(data-language);
font-size: 0.75rem;
font-weight: bold;
text-transform: uppercase;
color: var(--btn-content);
background: var(--btn-regular-bg);
border-radius: ${cssVar('borderRadius')};
color: oklch(0.75 0.1 var(--hue));
background: oklch(0.33 0.035 var(--hue));
border-radius: 0.5rem;
pointer-events: none;
transition: opacity 0.2s;
opacity: 0;
Expand Down
32 changes: 32 additions & 0 deletions src/styles/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@
}
}

.copy-btn {
all: initial;
@apply btn-regular-dark opacity-0 shadow-lg shadow-black/50 absolute active:scale-90 h-8 w-8 top-3 right-3 text-sm rounded-lg transition-all ease-in-out z-20 cursor-pointer;
}
.frame:hover .copy-btn {
opacity: 1;
}

.copy-btn-icon {
@apply absolute top-1/2 left-1/2 transition -translate-x-1/2 -translate-y-1/2 w-4 h-4 fill-white pointer-events-none;
}
.copy-btn .copy-icon {
@apply opacity-100 fill-white dark:fill-white/75;
}
.copy-btn.success .copy-icon {
@apply opacity-0 fill-[var(--deep-text)]
}
.copy-btn .success-icon {
@apply opacity-0 fill-white;
}
.copy-btn.success .success-icon {
@apply opacity-100
}

.expressive-code {
@apply my-4;
::selection {
@apply bg-[var(--codeblock-selection)];
}
}


ul, ol {
li::marker {
@apply text-[var(--primary)];
Expand Down
3 changes: 2 additions & 1 deletion src/styles/variables.styl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ define({
--inline-code-color: var(--btn-content)
--selection-bg: oklch(0.90 0.05 var(--hue)) oklch(0.40 0.08 var(--hue))
--codeblock-selection: oklch(0.40 0.08 var(--hue))
--codeblock-bg: oklch(0.2 0.015 var(--hue)) oklch(0.17 0.015 var(--hue))
--codeblock-bg: oklch(0.17 0.015 var(--hue)) oklch(0.17 0.015 var(--hue))
--codeblock-topbar-bg: oklch(0.3 0.02 var(--hue)) oklch(0.12 0.015 var(--hue))

--license-block-bg: black(0.03) var(--codeblock-bg)

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy