Skip to content

Commit

Permalink
add tree-sitter code
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Aug 4, 2024
1 parent b219f32 commit 260caec
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 10 deletions.
15 changes: 8 additions & 7 deletions astro.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'astro/config';
import tailwind from "@astrojs/tailwind";
import remarkLesetid from "remark-lesetid/astro";
import { rehypeTitles, rehypeCodeCopy, rehypePreClass } from './src/rehype';
import { rehypeTitles, rehypeCodeCopy, rehypePreClass, rehypeHighlight } from './src/rehype';
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import { remarkAlert } from 'remark-github-blockquote-alert';
import react from "@astrojs/react";
Expand Down Expand Up @@ -38,13 +38,14 @@ export default defineConfig({
rehypeHeadingIds,
rehypeTitles,
rehypeCodeCopy,
[rehypeShiki, {
themes: {
dark: "vesper",
light: 'github-light',
}
}],
// [rehypeShiki, {
// themes: {
// dark: "vesper",
// light: 'github-light',
// }
// }],
rehypePreClass,
rehypeHighlight,
],
},
build: {
Expand Down
23 changes: 22 additions & 1 deletion flake.lock

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

9 changes: 8 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
tree-sitter = {
url = "github:viperML/tree-sitter";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = {
self,
nixpkgs,
tree-sitter,
}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
Expand Down Expand Up @@ -64,6 +69,7 @@
env =
{
ASTRO_TELEMETRY_DISABLED = true;
TS_GRAMMAR_PATH = tree-sitter.packages.${system}.bundle;
}
// (lib.optionalAttrs (builtins.hasAttr "rev" self) {
GIT_COMMIT = self.rev;
Expand All @@ -89,7 +95,8 @@

ltex-ls
];
env.RUST_SRC_PATH = "${rustPlatform.rustLibSrc}";
env.RUST_SRC_PATH = "${rustPlatform.rustLibSrc}";
env.TS_GRAMMAR_PATH = tree-sitter.packages.${system}.bundle;
};
};
}
91 changes: 91 additions & 0 deletions neohome-rs/Cargo.lock

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

2 changes: 2 additions & 0 deletions neohome-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ crate-type = ["cdylib"]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
tree-sitter-dynamic = { git = "https://github.com/viperML/tree-sitter", version = "0.1.0" }
tree-sitter-highlight = "0.22.6"

[build-dependencies]
napi-build = "2.0.1"
Expand Down
34 changes: 33 additions & 1 deletion neohome-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@
#[macro_use]
extern crate napi_derive;

use tree_sitter_dynamic::{DynTS, STANDARD_CAPTURE_NAMES};
use tree_sitter_highlight::{Highlight, HighlightEvent};

#[napi]
pub fn sum(a: i32, b: i32) -> i32 {
a + b
a + b
}

#[napi]
pub fn highlight(text: String, lang: String) -> Option<String> {
let mut lang = DynTS::new(lang, STANDARD_CAPTURE_NAMES).ok()?;

let mut res: Vec<u8> = Vec::new();

let bytes = text.as_bytes();

for event in lang.highlight(bytes) {
match event.ok()? {
HighlightEvent::Source { start, end } => res.extend(&bytes[start..end]),
HighlightEvent::HighlightStart(Highlight(n)) => {
let capture = STANDARD_CAPTURE_NAMES[n];
let classes = capture.replace(".", " ");
let text = format!(r#"<span class="{}">"#, classes);
res.extend(text.as_bytes());
}
HighlightEvent::HighlightEnd => {
let text = format!("</span>");
res.extend(text.as_bytes());
}
};
}

let res_text = String::from_utf8(res).ok()?;

Some(res_text)
}
4 changes: 4 additions & 0 deletions src/pages/experiments/index.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
import Base from "../../layouts/Base.astro";
import Centered from "../../components/Centered.astro";
import { highlight } from "neohome-rs";
---

<Base
Expand All @@ -11,4 +13,6 @@ import Centered from "../../components/Centered.astro";
<Centered>
<h2>Coming soon...</h2>
</Centered>

{highlight("let x = 1", "javascript")}
</Base>
14 changes: 14 additions & 0 deletions src/rehype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { fromHtml } from 'hast-util-from-html'
import IconCopy from "@tabler/icons/outline/copy.svg?raw";
import IconCopyCheckFilled from "@tabler/icons/filled/copy-check.svg?raw";

import { highlight } from "neohome-rs";

function icon2node(raw: string): Element {
return fromHtml(raw, {
fragment: true,
Expand All @@ -28,6 +30,18 @@ export function rehypePreClass(): (tree: Root) => void {
}
}

export function rehypeHighlight(): (tree: Root) => void {
return function (tree: Root) {
visit(tree, "element", node => {
if (node.tagName === "pre") {
console.log(node);

}
});
}
}


export function rehypeTitles(): (tree: Root) => void {
return function (tree: Root) {
visit(tree, 'element', function (node) {
Expand Down

0 comments on commit 260caec

Please sign in to comment.