Skip to content

Commit

Permalink
fix: re-add syntect (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Oct 16, 2024
1 parent 3337daa commit 3f41a99
Show file tree
Hide file tree
Showing 25 changed files with 312 additions and 161 deletions.
81 changes: 79 additions & 2 deletions Cargo.lock

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

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ termcolor = "1.4.1"
html-escape = { version = "0.2.13", optional = true }
comrak = { version = "0.28.0", optional = true, default-features = false }
handlebars = { version = "6.1", optional = true, features = ["string_helpers"] }
syntect = { version = "5.2.0", optional = true, default-features = false, features = [
"parsing",
"default-syntaxes",
"default-themes",
"html",
"dump-load",
"regex-onig",
] }
ammonia = { version = "4.0.0", optional = true }

tree-sitter-highlight = { version = "0.22.6", optional = true }
Expand All @@ -67,13 +75,10 @@ pretty_assertions = "1.4.0"
insta = { version = "1.39.0", features = ["json"] }

[features]
default = ["html", "rust"]
default = ["html", "rust", "ammonia", "tree-sitter"]
rust = []
ammonia = ["dep:ammonia"]
html = [
"html-escape",
"comrak",
"handlebars",
html = ["html-escape", "comrak", "handlebars"]
tree-sitter = [
"tree-sitter-highlight",
"tree-sitter-javascript",
"tree-sitter-typescript",
Expand Down
66 changes: 66 additions & 0 deletions src/html/comrak_adapters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Copied and modified from https://github.com/kivikakk/comrak/blob/main/src/plugins/syntect.rs

//! Adapter for the Syntect syntax highlighter plugin.

#![allow(clippy::print_stderr)]

#[cfg(any(
not(any(feature = "syntect", feature = "tree-sitter")),
all(feature = "syntect", feature = "tree-sitter")
))]
compile_error!(
"Either feature \"syntect\" or \"tree-sitter\" must be enabled, not both or neither."
);

use comrak::adapters::HeadingAdapter;
use comrak::adapters::HeadingMeta;
use comrak::adapters::SyntaxHighlighterAdapter;
Expand All @@ -11,7 +23,13 @@ use std::sync::Arc;
use std::sync::Mutex;

#[derive(Debug)]
/// Syntect syntax highlighter plugin.
pub struct HighlightAdapter {
#[cfg(feature = "syntect")]
pub syntax_set: syntect::parsing::SyntaxSet,
#[cfg(feature = "syntect")]
pub theme_set: syntect::highlighting::ThemeSet,
#[cfg(feature = "tree-sitter")]
pub language_cb:
fn(&str) -> Option<&'static tree_sitter_highlight::HighlightConfiguration>,
pub show_line_numbers: bool,
Expand Down Expand Up @@ -76,6 +94,54 @@ impl HighlightAdapter {
}

impl SyntaxHighlighterAdapter for HighlightAdapter {
#[cfg(all(feature = "syntect", not(feature = "tree-sitter")))]
fn write_highlighted(
&self,
output: &mut dyn Write,
lang: Option<&str>,
code: &str,
) -> std::io::Result<()> {
let lang = match lang {
Some(l) if !l.is_empty() => l,
_ => "Plain Text",
};

let syntax =
self
.syntax_set
.find_syntax_by_token(lang)
.unwrap_or_else(|| {
self
.syntax_set
.find_syntax_by_first_line(code)
.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
});

let theme = &self.theme_set.themes["InspiredGitHub"];
let mut highlighter = syntect::easy::HighlightLines::new(syntax, theme);

match self.highlight_html(
syntect::util::LinesWithEndings::from(code),
|lines, line| {
let regions = highlighter.highlight_line(line, &self.syntax_set)?;

syntect::html::append_highlighted_html_for_styled_line(
&regions,
syntect::html::IncludeBackground::No,
lines,
)?;

Ok(())
},
) {
Ok(highlighted_code) => output.write_all(highlighted_code.as_bytes())?,
Err(_) => output.write_all(code.as_bytes())?,
}

self.write_button(output, code)
}

#[cfg(all(feature = "tree-sitter", not(feature = "syntect")))]
fn write_highlighted(
&self,
output: &mut dyn Write,
Expand Down
4 changes: 4 additions & 0 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ lazy_static! {
AmmoniaRelativeUrlEvaluator(),
)));

#[cfg(feature = "syntect")]
ammonia_builder.add_tag_attributes("span", ["style"]);

#[cfg(feature = "tree-sitter")]
ammonia_builder.add_allowed_classes("span", super::tree_sitter::CLASSES);

ammonia_builder
Expand Down
9 changes: 9 additions & 0 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod partition;
mod render_context;
mod search;
mod symbols;
#[cfg(feature = "tree-sitter")]
pub mod tree_sitter;
mod types;
mod usage;
Expand Down Expand Up @@ -794,6 +795,14 @@ pub fn setup_highlighter(
show_line_numbers: bool,
) -> comrak_adapters::HighlightAdapter {
comrak_adapters::HighlightAdapter {
#[cfg(feature = "syntect")]
syntax_set: syntect::dumps::from_uncompressed_data(include_bytes!(
"./default_newlines.packdump"
))
.unwrap(),
#[cfg(feature = "syntect")]
theme_set: syntect::highlighting::ThemeSet::load_defaults(),
#[cfg(feature = "tree-sitter")]
language_cb: tree_sitter::tree_sitter_language_cb,
show_line_numbers,
}
Expand Down
4 changes: 4 additions & 0 deletions tests/html_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async fn html_doc_files() {
]
);

#[cfg(feature = "tree-sitter")]
{
insta::assert_snapshot!(files.get("./all_symbols.html").unwrap());
insta::assert_snapshot!(files.get("./index.html").unwrap());
Expand Down Expand Up @@ -272,6 +273,7 @@ async fn html_doc_files_rewrite() {
]
);

#[cfg(feature = "tree-sitter")]
{
insta::assert_snapshot!(files.get("./all_symbols.html").unwrap());
insta::assert_snapshot!(files.get("./index.html").unwrap());
Expand Down Expand Up @@ -385,6 +387,7 @@ async fn symbol_group() {
}
}

#[cfg(feature = "tree-sitter")]
insta::assert_json_snapshot!(files);
}

Expand Down Expand Up @@ -483,5 +486,6 @@ async fn module_doc() {
module_docs.push(module_doc);
}

#[cfg(feature = "tree-sitter")]
insta::assert_json_snapshot!(module_docs);
}
8 changes: 4 additions & 4 deletions tests/snapshots/html_test__html_doc_files-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ expression: "files.get(\"./~/Bar.html\").unwrap()"
</article></main>
<div class="toc">
<div><div class="usageContent">
<h3>Usage</h3><div class="markdown"><pre class=" highlight"><code class="language-typescript"><span class="pl-k">import</span> { <span class="pl-smi">Bar</span> } <span class="pl-k">from</span> <span class="pl-s">&quot;.&quot;</span>;
<h3>Usage</h3><div class="markdown"><pre class="highlight"><code><span class="pl-k">import</span> { <span class="pl-smi">Bar</span> } <span class="pl-k">from</span> <span class="pl-s">"."</span>;
</code><button class="context_button" data-copy="import { Bar } from &quot;.&quot;;
"><svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="2" y="2" width="7" height="7" fill="none"/>
<rect x="6" y="6" width="7" height="7" fill="none"/>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"/>
<rect x="2" y="2" width="7" height="7" fill="none"></rect>
<rect x="6" y="6" width="7" height="7" fill="none"></rect>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"></path>
</svg>
</button><code></code></pre>
</div></div></div>
Expand Down
16 changes: 8 additions & 8 deletions tests/snapshots/html_test__html_doc_files-5.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ expression: "files.get(\"./~/Foo.html\").unwrap()"
<div>
<div class="text-2xl leading-none break-all">
<span class="text-Class">class</span>&nbsp;<span class="font-bold">Foo</span>
</div><div class="symbolSubtitle"></div></div></div><div><div class="space-y-7" id=""><div class="markdown"><pre class=" highlight"><code class="language-ts">using time <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">FakeTime</span>();
</div><div class="symbolSubtitle"></div></div></div><div><div class="space-y-7" id=""><div class="markdown"><pre class="highlight"><code>using time <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">FakeTime</span>();
</code><button class="context_button" data-copy="using time = new FakeTime();
"><svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="2" y="2" width="7" height="7" fill="none"/>
<rect x="6" y="6" width="7" height="7" fill="none"/>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"/>
<rect x="2" y="2" width="7" height="7" fill="none"></rect>
<rect x="6" y="6" width="7" height="7" fill="none"></rect>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"></path>
</svg>
</button><code></code></pre>
</div></div>
</div>
</article></main>
<div class="toc">
<div><div class="usageContent">
<h3>Usage</h3><div class="markdown"><pre class=" highlight"><code class="language-typescript"><span class="pl-k">import</span> { <span class="pl-smi">Foo</span> } <span class="pl-k">from</span> <span class="pl-s">&quot;.&quot;</span>;
<h3>Usage</h3><div class="markdown"><pre class="highlight"><code><span class="pl-k">import</span> { <span class="pl-smi">Foo</span> } <span class="pl-k">from</span> <span class="pl-s">"."</span>;
</code><button class="context_button" data-copy="import { Foo } from &quot;.&quot;;
"><svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="2" y="2" width="7" height="7" fill="none"/>
<rect x="6" y="6" width="7" height="7" fill="none"/>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"/>
<rect x="2" y="2" width="7" height="7" fill="none"></rect>
<rect x="6" y="6" width="7" height="7" fill="none"></rect>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"></path>
</svg>
</button><code></code></pre>
</div></div></div>
Expand Down
8 changes: 4 additions & 4 deletions tests/snapshots/html_test__html_doc_files-7.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ expression: "files.get(\"./~/Foobar.html\").unwrap()"
</article></main>
<div class="toc">
<div><div class="usageContent">
<h3>Usage</h3><div class="markdown"><pre class=" highlight"><code class="language-typescript"><span class="pl-k">import</span> <span class="pl-smi">Foobar</span> <span class="pl-k">from</span> <span class="pl-s">&quot;.&quot;</span>;
<h3>Usage</h3><div class="markdown"><pre class="highlight"><code><span class="pl-k">import</span> <span class="pl-smi">Foobar</span> <span class="pl-k">from</span> <span class="pl-s">"."</span>;
</code><button class="context_button" data-copy="import Foobar from &quot;.&quot;;
"><svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="2" y="2" width="7" height="7" fill="none"/>
<rect x="6" y="6" width="7" height="7" fill="none"/>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"/>
<rect x="2" y="2" width="7" height="7" fill="none"></rect>
<rect x="6" y="6" width="7" height="7" fill="none"></rect>
<path d="M1.55566 2.7C1.55566 2.03726 2.09292 1.5 2.75566 1.5H8.75566C9.41841 1.5 9.95566 2.03726 9.95566 2.7V5.1H12.3557C13.0184 5.1 13.5557 5.63726 13.5557 6.3V12.3C13.5557 12.9627 13.0184 13.5 12.3557 13.5H6.35566C5.69292 13.5 5.15566 12.9627 5.15566 12.3V9.9H2.75566C2.09292 9.9 1.55566 9.36274 1.55566 8.7V2.7ZM6.35566 9.9V12.3H12.3557V6.3H9.95566V8.7C9.95566 9.36274 9.41841 9.9 8.75566 9.9H6.35566ZM8.75566 8.7V2.7H2.75566V8.7H8.75566Z" fill="#232323"></path>
</svg>
</button><code></code></pre>
</div></div></div>
Expand Down
Loading

0 comments on commit 3f41a99

Please sign in to comment.