跳到內容

程式碼區塊

所有程式碼區塊均使用 Expressive Code,並搭配定義於 styles/custom.css 的自訂樣式。

function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10)); // 55
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10)) # 55
#!/bin/bash
for i in $(seq 1 5); do
echo "Iteration $i"
done
site:
title: Documentation
base: /
integrations:
- starlight
- react
{
"name": "@f5-sales-demo/docs-theme",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Test Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
:root {
--sl-color-accent: #f06680;
--sl-font: "proximaNova", system-ui;
}
h1 {
font-family: var(--sl-font-heading);
font-weight: 700;
}
interface Config {
title: string;
base: string;
plugins: string[];
}
const config: Config = {
title: "Documentation",
base: "/",
plugins: ["starlight", "react"],
};

```bash```sh 圍欄的終端機風格程式碼區塊,將套用仿 iTerm 的視窗框架,並顯示 macOS 交通號誌圓點。

Terminal
npm install @f5-sales-demo/docs-theme
npx astro build
npx astro preview
/* 框架邊框 */
.expressive-code .frame.is-terminal {
border: 2px solid rgba(255, 255, 255, 0.15);
}
/* 深色標題列搭配交通號誌 SVG 圓點 */
.expressive-code .frame.is-terminal .header {
background: #323232 !important;
color: #ccc !important;
}
/* 深色模式終端機主體 — 靈感來自 Tokyo Night */
:root:not([data-theme="light"]) .expressive-code .frame.is-terminal pre {
background: #1a1b26 !important;
}
/* 淺色模式邊框覆寫 */
:root[data-theme="light"] .expressive-code .frame.is-terminal {
border-color: rgba(0, 0, 0, 0.2);
}
src/config.ts
export default {
site: "https://example.com",
title: "My Docs",
};
const name = "highlighted"; // 第 1 行高亮
const other = "normal";
const a = 1; // 第 3-5 行高亮
const b = 2;
const c = 3;
const d = "normal again";
const config = {
theme: "dark", // 插入
theme: "light", // 刪除
lang: "en",
};
const oldValue = "deprecated";
const newValue = "replacement";
const unchanged = "stays";
removeThis();
addThis();

所有程式碼區塊均具有圓角及多層陰影效果:

.expressive-code .frame {
--header-border-radius: 0.75rem;
border-radius: 0.75rem;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0,0,0,0.04), 0 8px 16px rgba(0,0,0,0.08), 0 24px 48px rgba(0,0,0,0.12);
}
/* 深色模式(預設) */
.expressive-code .frame.is-terminal {
border: 2px solid rgba(255, 255, 255, 0.15);
}
/* 淺色模式 */
:root[data-theme="light"] .expressive-code .frame.is-terminal {
border-color: rgba(0, 0, 0, 0.2);
}
  • 程式碼區塊背景色會隨主題自動調整
  • 終端機標題列背景為 #323232,並顯示 macOS 交通號誌圓點
  • 終端機邊框在深色模式使用 rgba(255, 255, 255, 0.15),淺色模式使用 rgba(0, 0, 0, 0.2)
  • 深色模式終端機主體使用 #1a1b26
  • 兩種主題下語法顏色均保持清晰可讀
  • 行高亮具有可見的背景色調
  • 插入行顯示綠色色調,刪除行顯示紅色色調
  • 程式碼區塊具有 0.75rem 圓角及盒陰影