Astro 搭建博客系列:高亮 markdwon 代码块扩展语法

本文介绍了如何在官方demo不支持的高亮功能下,通过astro-expressive-code插件在Astro项目中实现代码块的自定义样式,包括安装依赖、创建并使用CSS变量以及修改astro配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方提供的 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,
  },
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值