treeSitter()
Tạo plugin Tree-sitter để bật syntax highlighting cho các khối mã fenced bằng thư viện parsing Tree-sitter.
import { treeSitter } from "@unifast/node";Chữ ký
function treeSitter(options?: TreeSitterPluginOptions): UnifastPluginTham số
options?
Cấu hình engine highlighting
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
engine | "none" | "treeSitter" | "treeSitter" | “treeSitter” sử dụng highlighter Tree-sitter; “none” sẽ tắt highlighting |
Cách dùng
import { compile, treeSitter } from "@unifast/node";
const md = `
# Code Example
\`\`\`rust
fn main() {
println!("Hello, world!");
}
\`\`\`
`;
const result = compile(md, {
plugins: [
treeSitter({
engine: "treeSitter",
}),
],
});
console.log(result.output);
// Code blocks are syntax highlighted with <span> elements and CSS classesVí dụ
Bật syntax highlighting
import { compile, treeSitter } from "@unifast/node";
const md = `
\`\`\`typescript
const greeting: string = "Hello";
console.log(greeting);
\`\`\`
`;
const result = compile(md, { plugins: [treeSitter()] });
console.log(result.output);
// <pre><code class="language-typescript">
// <span class="...">const</span> ...
// </code></pre>Tắt highlighting
import { compile, treeSitter } from "@unifast/node";
const result = compile(md, {
plugins: [treeSitter({ engine: "none" })],
});
console.log(result.output);
// Code blocks are rendered without syntax highlighting