Skip to content

Commit f26eb97

Browse files
authored
fix(html): always show example content, enable ammonia by default, and dont collapse newlines in jsdoc (#654)
1 parent 8923ce0 commit f26eb97

16 files changed

+194
-67
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ pretty_assertions = "1.4.0"
7575
insta = { version = "1.39.0", features = ["json"] }
7676

7777
[features]
78-
default = ["html", "rust", "ammonia", "tree-sitter"]
78+
default = ["html", "rust", "tree-sitter"]
7979
rust = []
80-
html = ["html-escape", "comrak", "handlebars"]
80+
html = ["html-escape", "comrak", "handlebars", "ammonia"]
8181
tree-sitter = [
8282
"tree-sitter-highlight",
8383
"tree-sitter-javascript",

src/html/comrak_adapters.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,5 @@ impl HeadingAdapter for HeadingToCAdapter {
368368
}
369369
}
370370

371-
#[cfg(feature = "ammonia")]
372371
pub type URLRewriter =
373372
Arc<dyn (Fn(Option<&crate::html::ShortPath>, &str) -> String) + Send + Sync>;

src/html/jsdoc.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::render_context::RenderContext;
22
use super::util::*;
3-
#[cfg(feature = "ammonia")]
43
use crate::html::comrak_adapters::URLRewriter;
54
use crate::html::ShortPath;
65
use crate::js_doc::JsDoc;
@@ -28,7 +27,6 @@ lazy_static! {
2827
regex::Regex::new(r"^\[(\S+)\](?:\.(\S+)|\s|)$").unwrap();
2928
}
3029

31-
#[cfg(feature = "ammonia")]
3230
lazy_static! {
3331
static ref AMMONIA: ammonia::Builder<'static> = {
3432
let mut ammonia_builder = ammonia::Builder::default();
@@ -94,7 +92,6 @@ lazy_static! {
9492
};
9593
}
9694

97-
#[cfg(feature = "ammonia")]
9895
thread_local! {
9996
static CURRENT_FILE: RefCell<Option<Option<ShortPath>>> = const { RefCell::new(None) };
10097
static URL_REWRITER: RefCell<Option<Option<URLRewriter>>> = const { RefCell::new(None) };
@@ -222,10 +219,8 @@ fn split_markdown_title(md: &str) -> (Option<&str>, Option<&str>) {
222219
}
223220
}
224221

225-
#[cfg(feature = "ammonia")]
226222
struct AmmoniaRelativeUrlEvaluator();
227223

228-
#[cfg(feature = "ammonia")]
229224
impl<'b> ammonia::UrlRelativeEvaluate<'b> for AmmoniaRelativeUrlEvaluator {
230225
fn evaluate<'a>(&self, url: &'a str) -> Option<Cow<'a, str>> {
231226
URL_REWRITER.with(|url_rewriter| {
@@ -412,6 +407,7 @@ fn walk_node_title<'a>(node: &'a AstNode<'a>) {
412407
| NodeValue::Escaped
413408
| NodeValue::WikiLink(_)
414409
| NodeValue::Underline
410+
| NodeValue::SoftBreak
415411
) {
416412
walk_node_title(child);
417413
} else {
@@ -490,14 +486,7 @@ pub fn markdown_to_html(
490486
options.extension.table = true;
491487
options.extension.tagfilter = true;
492488
options.extension.tasklist = true;
493-
#[cfg(not(feature = "ammonia"))]
494-
{
495-
options.render.escape = true;
496-
}
497-
#[cfg(feature = "ammonia")]
498-
{
499-
options.render.unsafe_ = true; // its fine because we run ammonia afterwards
500-
}
489+
options.render.unsafe_ = true; // its fine because we run ammonia afterwards
501490

502491
let mut plugins = comrak::Plugins::default();
503492

@@ -535,26 +524,18 @@ pub fn markdown_to_html(
535524
}
536525
};
537526

538-
#[cfg(feature = "ammonia")]
539-
{
540-
CURRENT_FILE
541-
.set(Some(render_ctx.get_current_resolve().get_file().cloned()));
542-
URL_REWRITER.set(Some(render_ctx.ctx.url_rewriter.clone()));
527+
CURRENT_FILE.set(Some(render_ctx.get_current_resolve().get_file().cloned()));
528+
URL_REWRITER.set(Some(render_ctx.ctx.url_rewriter.clone()));
543529

544-
let html = Some(format!(
545-
r#"<div class="{class_name}">{}</div>"#,
546-
AMMONIA.clean(&html)
547-
));
530+
let html = Some(format!(
531+
r#"<div class="{class_name}">{}</div>"#,
532+
AMMONIA.clean(&html)
533+
));
548534

549-
CURRENT_FILE.set(None);
550-
URL_REWRITER.set(None);
535+
CURRENT_FILE.set(None);
536+
URL_REWRITER.set(None);
551537

552-
html
553-
}
554-
#[cfg(not(feature = "ammonia"))]
555-
{
556-
Some(format!(r#"<div class="{class_name}">{html}</div>"#))
557-
}
538+
html
558539
}
559540

560541
pub(crate) fn render_markdown(

src/html/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ pub struct GenerateCtx {
264264
pub common_ancestor: Option<PathBuf>,
265265
pub doc_nodes: IndexMap<Rc<ShortPath>, Vec<DocNodeWithContext>>,
266266
pub highlight_adapter: comrak_adapters::HighlightAdapter,
267-
#[cfg(feature = "ammonia")]
268267
pub url_rewriter: Option<comrak_adapters::URLRewriter>,
269268
pub href_resolver: Rc<dyn HrefResolver>,
270269
pub usage_composer: Option<UsageComposer>,
@@ -370,7 +369,6 @@ impl GenerateCtx {
370369
common_ancestor,
371370
doc_nodes,
372371
highlight_adapter: setup_highlighter(false),
373-
#[cfg(feature = "ammonia")]
374372
url_rewriter: None,
375373
href_resolver: options.href_resolver,
376374
usage_composer: options.usage_composer,

src/html/templates/example.hbs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
<div class="anchorable example">
1+
<div class="anchorable">
22
{{~> anchor anchor ~}}
33

4-
<details id="{{id}}" open>
5-
<summary>
6-
<div>&#x25B6;</div>
7-
{{{~markdown_title~}}} {{! markdown rendering }}
8-
</summary>
9-
<div>
10-
{{{~markdown_body~}}} {{! markdown rendering }}
11-
</div>
12-
</details>
4+
<h3 class="example-header">
5+
{{{~markdown_title~}}} {{! markdown rendering }}
6+
</h3>
7+
<div>
8+
{{{~markdown_body~}}} {{! markdown rendering }}
9+
</div>
1310
</div>

src/html/templates/section.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<div class="space-y-8">
2323
{{~#each content.content~}}
2424
{{~> (lookup ../content "kind") this ~}}
25+
26+
{{~#if (and (eq (lookup ../content "kind") "example") (not @last))~}}
27+
<div class="border-b border-gray-300"></div>
28+
{{~/if~}}
2529
{{~/each~}}
2630
</div>
2731
{{~/if~}}

src/html/templates/styles.css

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,8 @@ a {
167167
}
168168
}
169169

170-
.example details {
171-
summary {
172-
@apply list-none flex items-center gap-2 py-2 rounded-lg w-full leading-6
173-
cursor-pointer;
174-
175-
> div:first-child {
176-
@apply text-stone-600 select-none;
177-
}
178-
}
179-
180-
&[open] summary > div:first-child {
181-
@apply rotate-90;
182-
}
170+
.example-header {
171+
@apply font-bold text-lg mb-3;
183172
}
184173

185174
.toc {

src/html/templates/styles.gen.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snapshots/html_test__html_doc_files_rewrite-2.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ expression: "files.get(\"./index.html\").unwrap()"
3434
</nav>
3535
<div id="searchResults"></div><div id="content">
3636
<main><section >
37-
<div class="space-y-2 flex-1 "><div class="space-y-7" id="module_doc"><div class="markdown"><p>Some docs</p>
37+
<div class="space-y-2 flex-1 "><div class="space-y-7" id="module_doc"><div class="markdown"><p>Some docs
38+
with a line break</p>
3839
<div class="alert alert-note"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
3940
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
4041
<path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0"></path>

tests/snapshots/html_test__html_doc_files_rewrite-21.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ source: tests/html_test.rs
33
expression: "files.get(\"search_index.js\").unwrap()"
44
---
55
(function () {
6-
window.DENO_DOC_SEARCH_INDEX = {"nodes":[{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"A","file":".","doc":"","url":"././~/A.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"B","file":".","doc":"","url":"././~/B.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Bar","file":".","doc":"> Some quote in bar docs\n> This quote part is ignored\n> when getting the title of this doc\n\nBar docs","url":"././~/Bar.html","deprecated":false},{"kind":[{"char":"T","kind":"TypeAlias","title":"Type Alias"}],"name":"Baz","file":".","doc":"","url":"././~/Baz.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Baz.foo","file":".","doc":"","url":"././~/Baz.foo.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Foo","file":".","doc":"some Foo docs {@linkcode Bar}","url":"././~/Foo.html","deprecated":false},{"kind":[{"char":"m","kind":"Method","title":"Method"}],"name":"Foo.prototype.test","file":".","doc":"","url":"././~/Foo.prototype.test.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.bar","file":".","doc":"","url":"././~/Foo.bar.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.prototype.foo","file":".","doc":"","url":"././~/Foo.prototype.foo.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.prototype.\"&gt;&lt;img src=x onerror=alert(1)&gt;","file":".","doc":"","url":"././~/Foo.prototype.\"><img src=x onerror=alert(1)>.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Foobar","file":".","doc":"```ts\n// This code block is ignored when getting the title of this doc\nconst foobar = new Foobar();\n```\n\nFoobar docs\n","url":"././~/Foobar.html","deprecated":false},{"kind":[{"char":"I","kind":"Interface","title":"Interface"}],"name":"Hello","file":".","doc":"","url":"././~/Hello.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Hello.world","file":".","doc":"","url":"././~/Hello.world.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"c","file":".","doc":"","url":"././~/c.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"d","file":".","doc":"","url":"././~/d.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"},{"char":"f","kind":"Function","title":"Function"},{"char":"f","kind":"Function","title":"Function"}],"name":"qaz","file":".","doc":"","url":"././~/qaz.html","deprecated":false},{"kind":[{"char":"v","kind":"Variable","title":"Variable"}],"name":"default","file":"foo","doc":"The default export item.\n\nThis item reproduces the issue reported in {@link https://github.com/jsr-io/jsr/issues/459}","url":"./foo/~/default.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"x","file":"foo","doc":"","url":"./foo/~/x.html","deprecated":false}]};
6+
window.DENO_DOC_SEARCH_INDEX = {"nodes":[{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"A","file":".","doc":"","url":"././~/A.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"B","file":".","doc":"","url":"././~/B.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Bar","file":".","doc":"> Some quote in bar docs\n> This quote part is ignored\n> when getting the title of this doc\n\nBar docs","url":"././~/Bar.html","deprecated":false},{"kind":[{"char":"T","kind":"TypeAlias","title":"Type Alias"}],"name":"Baz","file":".","doc":"","url":"././~/Baz.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Baz.foo","file":".","doc":"","url":"././~/Baz.foo.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Foo","file":".","doc":"some Foo docs {@linkcode Bar}\n","url":"././~/Foo.html","deprecated":false},{"kind":[{"char":"m","kind":"Method","title":"Method"}],"name":"Foo.prototype.test","file":".","doc":"","url":"././~/Foo.prototype.test.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.bar","file":".","doc":"","url":"././~/Foo.bar.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.prototype.foo","file":".","doc":"","url":"././~/Foo.prototype.foo.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Foo.prototype.\"&gt;&lt;img src=x onerror=alert(1)&gt;","file":".","doc":"","url":"././~/Foo.prototype.\"><img src=x onerror=alert(1)>.html","deprecated":false},{"kind":[{"char":"c","kind":"Class","title":"Class"}],"name":"Foobar","file":".","doc":"```ts\n// This code block is ignored when getting the title of this doc\nconst foobar = new Foobar();\n```\n\nFoobar docs\n","url":"././~/Foobar.html","deprecated":false},{"kind":[{"char":"I","kind":"Interface","title":"Interface"}],"name":"Hello","file":".","doc":"","url":"././~/Hello.html","deprecated":false},{"kind":[{"char":"p","kind":"Property","title":"Property"}],"name":"Hello.world","file":".","doc":"","url":"././~/Hello.world.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"c","file":".","doc":"","url":"././~/c.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"d","file":".","doc":"","url":"././~/d.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"},{"char":"f","kind":"Function","title":"Function"},{"char":"f","kind":"Function","title":"Function"}],"name":"qaz","file":".","doc":"","url":"././~/qaz.html","deprecated":false},{"kind":[{"char":"v","kind":"Variable","title":"Variable"}],"name":"default","file":"foo","doc":"The default export item.\n\nThis item reproduces the issue reported in {@link https://github.com/jsr-io/jsr/issues/459}","url":"./foo/~/default.html","deprecated":false},{"kind":[{"char":"f","kind":"Function","title":"Function"}],"name":"x","file":"foo","doc":"","url":"./foo/~/x.html","deprecated":false}]};
77
})()

0 commit comments

Comments
 (0)