Skip to content

Commit 4af7953

Browse files
committed
rework "all symbols" page
1 parent 7bdbb2d commit 4af7953

33 files changed

+1035
-458
lines changed

examples/ddoc/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,8 @@ impl HrefResolver for EmptyResolver {
186186
None
187187
}
188188

189-
fn resolve_usage(
190-
&self,
191-
current_specifier: &deno_ast::ModuleSpecifier,
192-
_current_file: Option<&ShortPath>,
193-
) -> Option<String> {
194-
Some(current_specifier.to_string())
189+
fn resolve_usage(&self, current_file: &ShortPath) -> Option<String> {
190+
Some(current_file.specifier.to_string())
195191
}
196192

197193
fn resolve_source(&self, location: &deno_doc::Location) -> Option<String> {

src/html/comrak_adapters.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Adapter for the Syntect syntax highlighter plugin.
44
5+
use crate::html::ShortPath;
56
use comrak::adapters::HeadingAdapter;
67
use comrak::adapters::HeadingMeta;
78
use comrak::adapters::SyntaxHighlighterAdapter;
@@ -264,6 +265,5 @@ impl HeadingAdapter for HeadingToCAdapter {
264265
}
265266

266267
#[cfg(feature = "ammonia")]
267-
pub type URLRewriter = Arc<
268-
dyn (Fn(Option<&deno_ast::ModuleSpecifier>, &str) -> String) + Send + Sync,
269-
>;
268+
pub type URLRewriter =
269+
Arc<dyn (Fn(Option<&ShortPath>, &str) -> String) + Send + Sync>;

src/html/jsdoc.rs

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::render_context::RenderContext;
22
use super::util::*;
33
use crate::html::usage::UsagesCtx;
4+
use crate::html::ShortPath;
45
use crate::js_doc::JsDoc;
56
use crate::js_doc::JsDocTag;
67
use crate::DocNodeKind;
@@ -9,7 +10,6 @@ use comrak::nodes::AstNode;
910
use comrak::nodes::NodeHtmlBlock;
1011
use comrak::nodes::NodeValue;
1112
use comrak::Arena;
12-
use deno_ast::ModuleSpecifier;
1313
use serde::Serialize;
1414
use std::borrow::Cow;
1515
use std::cell::RefCell;
@@ -79,6 +79,8 @@ fn split_markdown_title(md: &str) -> (Option<&str>, &str) {
7979

8080
let index = newline.min(codeblock).min(md.len());
8181

82+
dbg!(md.split_at(index));
83+
8284
match md.split_at(index) {
8385
("", body) => (None, body),
8486
(title, "") => (None, title),
@@ -88,14 +90,14 @@ fn split_markdown_title(md: &str) -> (Option<&str>, &str) {
8890

8991
#[cfg(feature = "ammonia")]
9092
struct AmmoniaRelativeUrlEvaluator {
91-
current_specifier: Option<ModuleSpecifier>,
93+
current_file: Option<ShortPath>,
9294
url_rewriter: URLRewriter,
9395
}
9496

9597
#[cfg(feature = "ammonia")]
9698
impl ammonia::UrlRelativeEvaluate for AmmoniaRelativeUrlEvaluator {
9799
fn evaluate<'a>(&self, url: &'a str) -> Option<Cow<'a, str>> {
98-
Some((self.url_rewriter)(self.current_specifier.as_ref(), url).into())
100+
Some((self.url_rewriter)(self.current_file.as_ref(), url).into())
99101
}
100102
}
101103

@@ -241,7 +243,7 @@ fn render_node<'a>(
241243
String::from_utf8(bw.into_inner().unwrap()).unwrap()
242244
}
243245

244-
#[derive(Debug, Serialize, Clone)]
246+
#[derive(Debug, Serialize, Clone, Default)]
245247
pub struct Markdown {
246248
pub html: String,
247249
pub toc: Option<String>,
@@ -252,7 +254,7 @@ pub fn markdown_to_html(
252254
md: &str,
253255
summary: bool,
254256
render_toc: bool,
255-
) -> Markdown {
257+
) -> Option<Markdown> {
256258
// TODO(bartlomieju): this should be initialized only once
257259
let mut options = comrak::Options::default();
258260
options.extension.autolink = true;
@@ -281,12 +283,16 @@ pub fn markdown_to_html(
281283
let md = parse_links(md, render_ctx);
282284

283285
let md = if summary {
284-
let (title, body) = split_markdown_title(md.as_ref());
285-
title.unwrap_or(body)
286+
let (title, _body) = split_markdown_title(&md);
287+
title.unwrap_or_default()
286288
} else {
287289
md.as_ref()
288290
};
289291

292+
if md.is_empty() {
293+
return None;
294+
}
295+
290296
let class_name = if summary {
291297
"markdown_summary"
292298
} else {
@@ -356,7 +362,7 @@ pub fn markdown_to_html(
356362
ammonia::UrlRelative::PassThrough,
357363
|url_rewriter| {
358364
ammonia::UrlRelative::Custom(Box::new(AmmoniaRelativeUrlEvaluator {
359-
current_specifier: render_ctx.get_current_specifier().cloned(),
365+
current_file: render_ctx.get_current_resolve().get_file().cloned(),
360366
url_rewriter: url_rewriter.clone(),
361367
}))
362368
},
@@ -407,21 +413,25 @@ pub fn markdown_to_html(
407413
None
408414
};
409415

410-
Markdown {
416+
Some(Markdown {
411417
html: format!(r#"<div class="{class_name} flex-1">{html}</div>"#),
412418
toc,
413-
}
419+
})
414420
}
415421

416422
pub(crate) fn render_markdown_summary(
417423
render_ctx: &RenderContext,
418424
md: &str,
419425
) -> String {
420-
markdown_to_html(render_ctx, md, true, false).html
426+
markdown_to_html(render_ctx, md, true, false)
427+
.unwrap_or_default()
428+
.html
421429
}
422430

423431
pub(crate) fn render_markdown(render_ctx: &RenderContext, md: &str) -> String {
424-
markdown_to_html(render_ctx, md, false, false).html
432+
markdown_to_html(render_ctx, md, false, false)
433+
.unwrap_or_default()
434+
.html
425435
}
426436

427437
pub(crate) fn jsdoc_body_to_html(
@@ -430,11 +440,7 @@ pub(crate) fn jsdoc_body_to_html(
430440
summary: bool,
431441
) -> Option<String> {
432442
if let Some(doc) = js_doc.doc.as_deref() {
433-
if doc.is_empty() {
434-
None
435-
} else {
436-
Some(markdown_to_html(ctx, doc, summary, false).html)
437-
}
443+
markdown_to_html(ctx, doc, summary, false).map(|markdown| markdown.html)
438444
} else {
439445
None
440446
}
@@ -461,10 +467,10 @@ pub(crate) fn jsdoc_examples(
461467
.collect::<Vec<ExampleCtx>>();
462468

463469
if !examples.is_empty() {
464-
Some(SectionCtx {
465-
title: "Examples".to_string(),
466-
content: SectionContentCtx::Example(examples),
467-
})
470+
Some(SectionCtx::new(
471+
"Examples",
472+
SectionContentCtx::Example(examples),
473+
))
468474
} else {
469475
None
470476
}
@@ -513,13 +519,13 @@ pub struct ModuleDocCtx {
513519
impl ModuleDocCtx {
514520
pub fn new(
515521
render_ctx: &RenderContext,
516-
specifier: &ModuleSpecifier,
522+
short_path: &ShortPath,
517523
doc_nodes_by_url: &super::ContextDocNodesByUrl,
518524
) -> Self {
519-
let module_doc_nodes = doc_nodes_by_url.get(specifier).unwrap();
525+
let module_doc_nodes = doc_nodes_by_url.get(&short_path.specifier).unwrap();
520526

521527
let title = if !render_ctx.ctx.hide_module_doc_title {
522-
Some(render_ctx.ctx.url_to_short_path(specifier).to_name())
528+
Some(short_path.display_name())
523529
} else {
524530
None
525531
};
@@ -546,7 +552,7 @@ impl ModuleDocCtx {
546552
.js_doc
547553
.doc
548554
.as_ref()
549-
.map(|doc| markdown_to_html(render_ctx, doc, false, true))
555+
.and_then(|doc| markdown_to_html(render_ctx, doc, false, true))
550556
{
551557
(Some(markdown.html), markdown.toc)
552558
} else {
@@ -558,18 +564,25 @@ impl ModuleDocCtx {
558564
(None, None, None)
559565
};
560566

561-
if !render_ctx
562-
.ctx
563-
.main_entrypoint
564-
.as_ref()
565-
.is_some_and(|main_entrypoint| main_entrypoint == specifier)
566-
{
567+
if short_path.is_main {
567568
let partitions_by_kind =
568569
super::partition::partition_nodes_by_kind(module_doc_nodes, true);
569570

570571
sections.extend(super::namespace::render_namespace(
571572
render_ctx,
572-
partitions_by_kind,
573+
partitions_by_kind
574+
.into_iter()
575+
.map(|(title, nodes)| {
576+
(
577+
SectionHeaderCtx {
578+
title,
579+
href: None,
580+
doc: None,
581+
},
582+
nodes,
583+
)
584+
})
585+
.collect(),
573586
));
574587
}
575588

0 commit comments

Comments
 (0)