Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import icon from "astro-icon";
import { pluginLanguageLogo } from "ec-lang-logo"; /* Language Logo */
import { pluginCollapsible } from "expressive-code-collapsible"; /* Collapsible */
import { pluginLanguageBadge } from "expressive-code-language-badge"; /* Language Badge */
import katex from "katex";
import "katex/dist/contrib/mhchem.mjs"; // 加载 mhchem 扩展
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeCallouts from "rehype-callouts";
Expand Down Expand Up @@ -302,7 +301,7 @@ export default defineConfig({
[remarkPlantuml, plantumlConfig],
],
rehypePlugins: [
[rehypeKatex, { katex }],
rehypeKatex,
[rehypeCallouts, { theme: siteConfig.post.rehypeCallouts.theme }],
rehypeSlug,
rehypeCodeGroup,
Expand Down
4 changes: 1 addition & 3 deletions docs/AGENT_CONTENT_PROMPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- 文章 Frontmatter 只必填 `title` 和 `published`。发布时建议按需填写 `description`、`tags`、`category`、`draft`、`author` 和 `comment`。其他字段见[字段规范](WRITING_GUIDE.md#43-字段规范)。
- 文章语言与站点默认语言不同时填写 `lang`,例如英文文章用 `en`。
- 提醒框使用 GitHub 语法,例如 `> [!NOTE]`。本站关闭了 Python-Markdown 的 `!!!`、`???` 和 `???+` 语法。折叠写法见[写作指南](WRITING_GUIDE.md#53-提醒框)。
- 化学式和反应方程式可使用 KaTeX 的 `\ce{}`,示例见[化学公式](WRITING_GUIDE.md#188-katex-化学公式)。
- 日期按 `Asia/Shanghai` 时区。
- 已发布文章的 `published` 和 `slug` 不要修改;需要更新时设置 `updated`。
- 标签和分类优先沿用现有站点的写法,不要随意发明同义标签。
Expand Down Expand Up @@ -92,9 +93,6 @@

## 已知限制

- 当前版本的 KaTeX 化学公式不要使用 `\ce{}`,会显示红色未知命令。
- 使用普通 KaTeX 写法,例如:
`\mathrm{CH_4 + 2O_2 \rightarrow CO_2 + 2H_2O}`
- `draft: true` 在生产构建中隐藏,开发环境仍可见。
- Pagefind 搜索必须在 `pnpm build && pnpm preview` 后验证,开发服务器不能完整代表搜索结果。
- ESA Pages 没有与 Cloudflare Pages 完全相同的 PR Preview 流程,不要假设每个 PR 都有公开预览地址。
Expand Down
6 changes: 2 additions & 4 deletions docs/WRITING_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1226,13 +1226,11 @@ console.log("这一行标记为新增");

### 18.8 KaTeX 化学公式

官方演示页展示了 `mhchem` 的 `\ce{}` 语法,但当前仓库 `Firefly 6.16.9` 实测会把 `\ce` 渲染成红色未知命令。**不要直接依赖 `\ce{}`。**

当前可靠写法是使用普通 KaTeX:
化学式和反应方程式可以用 KaTeX 的 `mhchem` 扩展书写。用 `\ce{}` 包住方程式,反应箭头写成 `->`。

```markdown
$$
\mathrm{CH_4 + 2O_2 \rightarrow CO_2 + 2H_2O}
\ce{CH4 + 2O2 -> CO2 + 2H2O}
$$
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"@iconify/svelte": "^5.2.3",
"@iconify/types": "^2.0.0",
"@iconify/utils": "^3.1.7",
"@mermanjs/web": "0.8.0-alpha.3",
"@mermanjs/node": "0.9.3",
"@mermanjs/web": "0.9.3",
"@napi-rs/wasm-runtime": "^1.2.6",
"@swup/astro": "^1.8.0",
"@tailwindcss/typography": "^0.5.20",
Expand Down
93 changes: 72 additions & 21 deletions pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ packages:
allowBuilds:
esbuild: true
workerd: true
overrides:
katex: "^0.19.1"
52 changes: 26 additions & 26 deletions src/plugins/rehype-mermaid.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFile } from "node:fs/promises";
import { assertSafeSvgForDom, initMerman, renderSvg } from "@mermanjs/web";
import { createNodeEngine } from "@mermanjs/node";
import { assertSelfContainedSvgForDom } from "@mermanjs/web";
import { fromHtml } from "hast-util-from-html";
import { h } from "hastscript";
import { visit } from "unist-util-visit";
Expand All @@ -15,14 +15,9 @@ import {
} from "./utils/diagramConstants.js";
import { extractText } from "./utils/extractText.js";

const mermanWasmUrl = import.meta.resolve(
"@mermanjs/web/pkg/merman_wasm_bg.wasm",
);
await initMerman({
wasm: {
module_or_path: await readFile(new URL(mermanWasmUrl)),
},
});
// @mermanjs/web 自 0.9 起仅支持浏览器运行时,构建期渲染改用 Node 原生引擎;
// @mermanjs/web 仅用于校验输出的 SVG 可以安全插入 DOM
const mermanEngine = await createNodeEngine();

/**
* 在构建时将 Mermaid 源码渲染为浅色和深色两套静态 SVG
Expand All @@ -40,24 +35,29 @@ function removeSvgMaxWidth(svg) {
return svg.replace(/(<svg[^>]*style="[^"]*?)max-width:\s*[^;]+;?/, "$1");
}

function buildMermaidSvgs(mermaidCode, themeConfig, diagramIndex) {
const lightSvg = renderSvg(mermaidCode, {
host_theme: { preset: themeConfig.lightTheme },
svg: {
diagram_id: `mermaid-${diagramIndex}-light`,
pipeline: "parity",
},
});
const darkSvg = renderSvg(mermaidCode, {
host_theme: { preset: themeConfig.darkTheme },
svg: {
diagram_id: `mermaid-${diagramIndex}-dark`,
pipeline: "parity",
},
function renderMermaidSvg(mermaidCode, preset, diagramId) {
return mermanEngine.renderSvgSync(mermaidCode, {
optionsJson: JSON.stringify({
presentation: { theme: { preset } },
svg: { diagram_id: diagramId, pipeline: "parity" },
}),
});
}

function buildMermaidSvgs(mermaidCode, themeConfig, diagramIndex) {
const lightSvg = renderMermaidSvg(
mermaidCode,
themeConfig.lightTheme,
`mermaid-${diagramIndex}-light`,
);
const darkSvg = renderMermaidSvg(
mermaidCode,
themeConfig.darkTheme,
`mermaid-${diagramIndex}-dark`,
);

assertSafeSvgForDom(lightSvg);
assertSafeSvgForDom(darkSvg);
assertSelfContainedSvgForDom(lightSvg);
assertSelfContainedSvgForDom(darkSvg);

return {
lightSvg: removeSvgMaxWidth(lightSvg),
Expand Down
5 changes: 5 additions & 0 deletions src/styles/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
}
}

/* 行内代码保留连续空格;代码块(pre > code)不受影响 */
:not(pre) > code {
white-space: break-spaces;
}

.expressive-code {
@apply my-4;
}
Expand Down
10 changes: 5 additions & 5 deletions src/types/mermaidConfig.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { HostThemePresetName } from "@mermanjs/web";
import type { BUNDLED_THEME_PRESETS } from "@mermanjs/web";

/**
* merman 内置宿主主题预设名
* merman 内置主题预设名
*/
export type MermaidThemeName = HostThemePresetName;
export type MermaidThemeName = (typeof BUNDLED_THEME_PRESETS)[number];

/**
* Mermaid 图表渲染配置
*
* 控制 markdown 文章中 ` ```mermaid ` 代码块在构建时的服务端 SVG 渲染行为。
*/
export type MermaidConfig = {
/** 亮色模式下使用的 merman 宿主主题预设名 */
/** 亮色模式下使用的 merman 内置主题预设名 */
lightTheme: MermaidThemeName;
/** 暗色模式下使用的 merman 宿主主题预设名 */
/** 暗色模式下使用的 merman 内置主题预设名 */
darkTheme: MermaidThemeName;
};
25 changes: 22 additions & 3 deletions src/utils/responsive-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,34 @@ export function computeGridColumns(input: GridColumnsInput): GridColumnVars {
const rightIn =
sidebarActive && input.hasRightWidgets && input.position !== "left";

// 平板端:position 为 both 时只显示 tabletSidebar 指定的那一侧
// 平板端:position 为 both 时只显示 tabletSidebar 指定的那一侧。
// 若指定的一侧没有可用组件(hasLeft/hasRightWidgets 为 false),则回退到另一侧,
// 避免平板宽度下整列侧边栏直接消失。
let tabletSide = input.tabletSidebar;
if (
input.position === "both" &&
tabletSide === "left" &&
!input.hasLeftWidgets &&
input.hasRightWidgets
) {
tabletSide = "right";
} else if (
input.position === "both" &&
tabletSide === "right" &&
!input.hasRightWidgets &&
input.hasLeftWidgets
) {
tabletSide = "left";
}

const mdLeft =
leftIn &&
(input.position === "left" ||
(input.position === "both" && input.tabletSidebar === "left"));
(input.position === "both" && tabletSide === "left"));
const mdRight =
rightIn &&
(input.position === "right" ||
(input.position === "both" && input.tabletSidebar === "right"));
(input.position === "both" && tabletSide === "right"));

// 侧栏已启用但本断点无侧栏列 → 内容栏按比例收窄;比例钳到 [0,1],1 即不收窄
// 闸门用 enabled 而非 sidebarActive:enable: false 时仍铺满。md 与 xl 需各自判定
Expand Down