官方提供的 demo 版本默认不支持高亮 md 代码块扩展语法,比如设置标题,高亮删减行、特定行、特定代码段等。
我们需要通过 astro 提供的 astro-expressive-code 插件来配置我们的主题。
修改配置
步骤 1,安装依赖
yarn add astro-expressive-code
步骤 2,新建 src/utils/expressive-code.ts。
这里是用到了两个 css 变量:--theme-code-bg
和 --theme-code-tabs
。
import { astroExpressiveCode } from "astro-expressive-code";
export const astroDocsExpressiveCode = () =>
astroExpressiveCode({
theme: "one-dark-pro",
styleOverrides: {
codeBackground: "var(--theme-code-bg)",
scrollbarThumbColor: "hsl(269deg 20% 90% / 0.25)",
scrollbarThumbHoverColor: "hsl(269deg 20% 90% / 0.5)",
},
frames: {
styleOverrides: {
editorTabBarBackground: "var(--theme-code-tabs)",
editorActiveTabBackground: "hsl(24.77deg 40% 65% / 15%)",
editorActiveTabBorderBottom: "hsl(25.36deg 95.22% 49.22%)",
editorTabBarBorderBottom: "var(--theme-code-tabs)",
terminalTitlebarBackground: "var(--theme-code-tabs)",
terminalTitlebarBorderBottom: "transparent",
terminalBackground: "var(--theme-code-bg)",
},
},
textMarkers: {
styleOverrides: { defaultChroma: "55" },
},
});
步骤 3,进入 src/styles/base.css,添加 css 变量
@layer base {
:root,
html[data-theme="light"] {
--theme-code-tabs: #22272f;
--theme-code-bg: #282c34;
}
html[data-theme="dark"] {
--theme-code-tabs: #22272f;
--theme-code-bg: #282c34;
}
}
步骤 4,修改 astro 配置
import { astroDocsExpressiveCode } from "./src/utils/expressive-code";
export default defineConfig({
integrations: [
astroDocsExpressiveCode(), //添加
react(),
sitemap(),
// ... others
],
markdown: {
// shikiConfig: {
// theme: "one-dark-pro",
// wrap: true,
// },
extendDefaultPlugins: true,
},
});