代码块
三个反引号后面写语言名,就有颜色。认不出的语言按普通文字显示,不会报错。每段代码都能一键复制。
Go
// Rel 给出两页之间的相对地址。
func Rel(from, to string) (string, error) {
n := strings.Count(from, "/")
if n > 8 {
return "", fmt.Errorf("目录太深:%d 层", n)
}
return strings.Repeat("../", n) + to, nil
}
JavaScript 和 TypeScript
const hits = mdsiteSearch.query("段", 20);
for (const { title, path } of hits) console.log(`${title} → ${path}`);
interface Hit { path: string; title: string; anchor?: string }
export const first = (hits: readonly Hit[]): Hit | undefined => hits[0];
export function Badge({ count }: { count: number }) {
return <span className="badge">{count > 99 ? "99+" : count}</span>;
}
Python
from pathlib import Path
def pages(root: Path) -> list[str]:
"""列出全部 Markdown 文件。"""
return sorted(str(p) for p in root.rglob("*.md") if not p.name.startswith("."))
Rust
fn fold(c: char) -> char {
match c {
'\u{FF01}'..='\u{FF5E}' => char::from_u32(c as u32 - 0xFEE0).unwrap_or(c),
_ => c.to_ascii_lowercase(), // 全角转半角、大写转小写
}
}
Java、C、C++
public record Page(String path, String title) {
public boolean isIndex() { return path.endsWith("index.html"); }
}
#include <stdio.h>
int main(void) {
printf("你好,%s\n", "mdsite"); /* 一行输出 */
return 0;
}
#include <vector>
#include <algorithm>
auto longest(const std::vector<std::string>& v) {
return *std::max_element(v.begin(), v.end(),
[](auto& a, auto& b) { return a.size() < b.size(); });
}
Swift 和 Kotlin
struct Page: Identifiable {
let id = UUID()
var title: String
var isDraft = false
}
data class Page(val path: String, val title: String = "未命名")
fun List<Page>.titles() = joinToString("、") { it.title }
数据与配置
SELECT path, count(*) AS hits
FROM views
WHERE day >= '2026-11-01' -- 只看本月
GROUP BY path
ORDER BY hits DESC
LIMIT 10;
{ "title": "安装", "order": 1, "draft": false, "tags": ["入门", null] }
# GitHub Actions 的一步
- name: 生成网站
run: mdsite build docs -o public -strict
[server]
addr = "127.0.0.1:8421"
retries = 20
HTML、XML、CSS
<!-- 搜索弹窗 -->
<dialog id="search"><input type="search" placeholder="搜索"></dialog>
<?xml version="1.0" encoding="UTF-8"?>
<urlset><url><loc>https://example.com/</loc></url></urlset>
.tab[aria-current] {
writing-mode: vertical-rl;
background: var(--shilv); /* 当前章 */
}
改动对比
## 安装
-需要 Node 18。
+不需要 Node。
命令行
for f in *.markdown; do
mv "$f" "${f%.markdown}.md" # 统一扩展名
done
没写语言
原样显示 <b>尖括号</b> 和 & 符号。
很长的一行
太长的代码在框里横向滚动,页面不会被撑宽:
mdsite build ~/Documents/项目资料/2026-年度/产品手册 -o ~/Sites/产品手册 -title "产品手册(第三版,含安装、使用、维护与常见问题)" -lang zh -strict