Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): correctly handle examples in document navigation #612

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/html/comrak_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl HeadingToCAdapter {
}

let mut toc_content = vec!["<ul>".to_string()];
let mut current_level = toc.first().unwrap().level;
let mut current_level = toc.iter().map(|entry| entry.level).min().unwrap();

for entry in toc {
match current_level.cmp(&entry.level) {
Expand Down
51 changes: 47 additions & 4 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::render_context::RenderContext;
use super::util::*;
#[cfg(feature = "ammonia")]
use crate::html::comrak_adapters::URLRewriter;
use crate::html::ShortPath;
use crate::js_doc::JsDoc;
use crate::js_doc::JsDocTag;
Expand All @@ -12,9 +14,8 @@ use comrak::Arena;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;

#[cfg(feature = "ammonia")]
use crate::html::comrak_adapters::URLRewriter;
use std::io::BufWriter;
use std::io::Write;

lazy_static! {
static ref JSDOC_LINK_RE: regex::Regex = regex::Regex::new(
Expand Down Expand Up @@ -298,7 +299,7 @@ fn render_node<'a>(
options: &comrak::Options,
plugins: &comrak::Plugins,
) -> String {
let mut bw = std::io::BufWriter::new(Vec::new());
let mut bw = BufWriter::new(Vec::new());
comrak::format_html_with_plugins(node, options, &mut bw, plugins).unwrap();
String::from_utf8(bw.into_inner().unwrap()).unwrap()
}
Expand All @@ -309,6 +310,46 @@ pub struct MarkdownToHTMLOptions {
pub no_toc: bool,
}

pub fn strip(render_ctx: &RenderContext, md: &str) -> String {
let mut options = comrak::Options::default();
options.extension.autolink = true;
options.extension.description_lists = true;
options.extension.strikethrough = true;
options.extension.superscript = true;
options.extension.table = true;
options.extension.tagfilter = true;
options.extension.tasklist = true;
options.render.escape = true;

let md = parse_links(md, render_ctx);

let arena = Arena::new();
let root = comrak::parse_document(&arena, &md, &options);

walk_node(&arena, root, &options, &Default::default());

fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut BufWriter<Vec<u8>>) {
match node.data.borrow().value {
NodeValue::Text(ref literal)
| NodeValue::Code(comrak::nodes::NodeCode { ref literal, .. }) => {
output.write_all(literal.as_bytes()).unwrap();
}
NodeValue::LineBreak | NodeValue::SoftBreak => {
output.write_all(&[b' ']).unwrap()
}
_ => {
for n in node.children() {
collect_text(n, output);
}
}
}
}

let mut bw = BufWriter::new(Vec::new());
collect_text(root, &mut bw);
String::from_utf8(bw.into_inner().unwrap()).unwrap()
}

pub fn markdown_to_html(
render_ctx: &RenderContext,
md: &str,
Expand Down Expand Up @@ -529,6 +570,7 @@ pub(crate) fn jsdoc_examples(
pub struct ExampleCtx {
pub anchor: AnchorCtx,
pub id: String,
pub title: String,
pub markdown_title: String,
markdown_body: String,
}
Expand All @@ -553,6 +595,7 @@ impl ExampleCtx {
ExampleCtx {
anchor: AnchorCtx { id: id.to_string() },
id: id.to_string(),
title,
markdown_title,
markdown_body,
}
Expand Down
10 changes: 6 additions & 4 deletions src/html/symbols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,12 @@ impl SymbolInnerCtx {
let docs =
crate::html::jsdoc::jsdoc_body_to_html(ctx, &doc_node.js_doc, false);

if let Some(examples) =
crate::html::jsdoc::jsdoc_examples(ctx, &doc_node.js_doc)
{
sections.push(examples);
if doc_node.kind != DocNodeKind::Function {
if let Some(examples) =
crate::html::jsdoc::jsdoc_examples(ctx, &doc_node.js_doc)
{
sections.push(examples);
}
}

sections.extend(match doc_node.kind {
Expand Down
15 changes: 14 additions & 1 deletion src/html/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,20 @@ impl SectionCtx {
render_context.toc.add_entry(2, name.clone(), anchor);
}
}
SectionContentCtx::Example(_) => {}
SectionContentCtx::Example(examples) => {
for example in examples {
let anchor = render_context.toc.anchorize(example.id.to_owned());

example.id = anchor.clone();
example.anchor.id = anchor.clone();

render_context.toc.add_entry(
2,
super::jsdoc::strip(render_context, &example.title),
anchor,
);
}
}
SectionContentCtx::IndexSignature(_) => {}
SectionContentCtx::NamespaceSection(nodes) => {
for node in nodes {
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/html_test__html_doc_files_rewrite-21.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ source: tests/html_test.rs
expression: "files.get(\"search_index.js\").unwrap()"
---
(function () {
window.DENO_DOC_SEARCH_INDEX = {"nodes":[{"kind":["class"],"name":"A","file":".","doc":"","location":{"filename":".","line":45,"col":0,"byteIndex":741},"url":"././~/A.html","category":"","declarationKind":"private","deprecated":false},{"kind":["class"],"name":"B","file":".","doc":"","location":{"filename":".","line":48,"col":0,"byteIndex":770},"url":"././~/B.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Bar","file":".","doc":"","location":{"filename":".","line":31,"col":0,"byteIndex":588},"url":"././~/Bar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["typeAlias"],"name":"Baz","file":".","doc":"","location":{"filename":".","line":41,"col":0,"byteIndex":702},"url":"././~/Baz.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Foo","file":".","doc":"some Foo docs","location":{"filename":".","line":25,"col":0,"byteIndex":488},"url":"././~/Foo.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Foobar","file":".","doc":"","location":{"filename":".","line":34,"col":0,"byteIndex":622},"url":"././~/Foobar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["interface"],"name":"Hello","file":".","doc":"","location":{"filename":".","line":37,"col":0,"byteIndex":655},"url":"././~/Hello.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"c","file":".","doc":"","location":{"filename":".","line":57,"col":13,"byteIndex":933},"url":"././~/c.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function","function","function"],"name":"qaz","file":".","doc":"","location":{"filename":".","line":53,"col":0,"byteIndex":812},"url":"././~/qaz.html","category":"","declarationKind":"export","deprecated":false},{"kind":["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}","location":{"filename":"foo","line":7,"col":6,"byteIndex":167},"url":"./foo/~/default.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"x","file":"foo","doc":"","location":{"filename":"foo","line":1,"col":0,"byteIndex":0},"url":"./foo/~/x.html","category":"","declarationKind":"export","deprecated":false}]};
window.DENO_DOC_SEARCH_INDEX = {"nodes":[{"kind":["class"],"name":"A","file":".","doc":"","location":{"filename":".","line":45,"col":0,"byteIndex":741},"url":"././~/A.html","category":"","declarationKind":"private","deprecated":false},{"kind":["class"],"name":"B","file":".","doc":"","location":{"filename":".","line":48,"col":0,"byteIndex":770},"url":"././~/B.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Bar","file":".","doc":"","location":{"filename":".","line":31,"col":0,"byteIndex":588},"url":"././~/Bar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["typeAlias"],"name":"Baz","file":".","doc":"","location":{"filename":".","line":41,"col":0,"byteIndex":702},"url":"././~/Baz.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Foo","file":".","doc":"some Foo docs","location":{"filename":".","line":25,"col":0,"byteIndex":488},"url":"././~/Foo.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Foobar","file":".","doc":"","location":{"filename":".","line":34,"col":0,"byteIndex":622},"url":"././~/Foobar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["interface"],"name":"Hello","file":".","doc":"","location":{"filename":".","line":37,"col":0,"byteIndex":655},"url":"././~/Hello.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"c","file":".","doc":"","location":{"filename":".","line":57,"col":13,"byteIndex":933},"url":"././~/c.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"d","file":".","doc":"","location":{"filename":".","line":59,"col":0,"byteIndex":961},"url":"././~/d.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function","function","function"],"name":"qaz","file":".","doc":"","location":{"filename":".","line":53,"col":0,"byteIndex":812},"url":"././~/qaz.html","category":"","declarationKind":"export","deprecated":false},{"kind":["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}","location":{"filename":"foo","line":7,"col":6,"byteIndex":167},"url":"./foo/~/default.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"x","file":"foo","doc":"","location":{"filename":"foo","line":1,"col":0,"byteIndex":0},"url":"./foo/~/x.html","category":"","declarationKind":"export","deprecated":false}]};
})()
Loading
Loading