Skip to content

Commit e367780

Browse files
committed
simplify modulespecifier and shortpath usages
1 parent d766b1d commit e367780

File tree

8 files changed

+89
-107
lines changed

8 files changed

+89
-107
lines changed

examples/ddoc/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn generate_docs_directory(
224224
href_resolver: Rc::new(EmptyResolver()),
225225
usage_composer: None,
226226
rewrite_map: Some(index_map),
227-
composable_output: true,
227+
composable_output: false,
228228
};
229229
let html = deno_doc::html::generate(options, doc_nodes_by_url)?;
230230

src/html/jsdoc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ impl ModuleDocCtx {
556556
pub fn new(
557557
render_ctx: &RenderContext,
558558
short_path: &ShortPath,
559-
doc_nodes_by_url: &super::ContextDocNodesByUrl,
559+
doc_nodes_by_url: &super::ContextDocNodesByShortPath,
560560
) -> Self {
561-
let module_doc_nodes = doc_nodes_by_url.get(&short_path.specifier).unwrap();
561+
let module_doc_nodes = doc_nodes_by_url.get(short_path).unwrap();
562562

563563
let mut sections = Vec::with_capacity(7);
564564

src/html/mod.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod usage;
2525
mod util;
2626

2727
use crate::html::pages::SymbolPage;
28+
use crate::html::partition::get_partitions_for_file;
2829
pub use pages::generate_symbol_pages_for_module;
2930
pub use render_context::RenderContext;
3031
pub use search::generate_search_index;
@@ -106,11 +107,11 @@ impl<'ctx> GenerateCtx<'ctx> {
106107
pub fn doc_nodes_by_url_add_context(
107108
&self,
108109
doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<DocNode>>,
109-
) -> ContextDocNodesByUrl {
110+
) -> ContextDocNodesByShortPath {
110111
doc_nodes_by_url
111112
.into_iter()
112113
.map(|(specifier, nodes)| {
113-
let short_path = Rc::new(ShortPath::new(self, specifier.clone()));
114+
let short_path = Rc::new(ShortPath::new(self, specifier));
114115

115116
let nodes = nodes
116117
.into_iter()
@@ -123,7 +124,7 @@ impl<'ctx> GenerateCtx<'ctx> {
123124
})
124125
.collect::<Vec<_>>();
125126

126-
(specifier, nodes)
127+
(short_path, nodes)
127128
})
128129
.collect::<IndexMap<_, _>>()
129130
}
@@ -137,8 +138,8 @@ impl<'ctx> GenerateCtx<'ctx> {
137138
}
138139
}
139140

140-
pub type ContextDocNodesByUrl =
141-
IndexMap<ModuleSpecifier, Vec<DocNodeWithContext>>;
141+
pub type ContextDocNodesByShortPath =
142+
IndexMap<Rc<ShortPath>, Vec<DocNodeWithContext>>;
142143

143144
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
144145
pub struct ShortPath {
@@ -488,19 +489,27 @@ pub fn generate(
488489
};
489490
let mut files = HashMap::new();
490491

491-
let doc_nodes_by_url = ctx.doc_nodes_by_url_add_context(doc_nodes_by_url);
492+
let doc_nodes_by_short_path =
493+
ctx.doc_nodes_by_url_add_context(doc_nodes_by_url);
492494

493495
// Index page
494496
{
497+
let main_entrypoint = doc_nodes_by_short_path
498+
.iter()
499+
.find(|(short_path, _)| short_path.is_main);
500+
495501
let partitions_for_entrypoint_nodes =
496-
partition::get_partitions_for_main_entrypoint(&ctx, &doc_nodes_by_url);
502+
if let Some((_, doc_nodes)) = main_entrypoint {
503+
get_partitions_for_file(&ctx, doc_nodes)
504+
} else {
505+
Default::default()
506+
};
497507

498508
let index = pages::IndexCtx::new(
499509
&ctx,
500-
ctx.main_entrypoint.as_ref(),
501-
&doc_nodes_by_url,
510+
main_entrypoint.map(|(short_path, _)| short_path.clone()),
511+
&doc_nodes_by_short_path,
502512
partitions_for_entrypoint_nodes,
503-
None,
504513
);
505514

506515
if options.composable_output {
@@ -540,7 +549,7 @@ pub fn generate(
540549

541550
// All symbols (list of all symbols in all files)
542551
if ctx.file_mode != FileMode::SingleDts {
543-
let all_doc_nodes = doc_nodes_by_url
552+
let all_doc_nodes = doc_nodes_by_short_path
544553
.values()
545554
.flatten()
546555
.cloned()
@@ -549,8 +558,11 @@ pub fn generate(
549558
let partitions_by_kind =
550559
partition::partition_nodes_by_entrypoint(&all_doc_nodes, true);
551560

552-
let all_symbols =
553-
pages::AllSymbolsCtx::new(&ctx, partitions_by_kind, &doc_nodes_by_url);
561+
let all_symbols = pages::AllSymbolsCtx::new(
562+
&ctx,
563+
partitions_by_kind,
564+
&doc_nodes_by_short_path,
565+
);
554566

555567
if options.composable_output {
556568
files.insert(
@@ -573,15 +585,13 @@ pub fn generate(
573585

574586
// Pages for all discovered symbols
575587
{
576-
for (specifier, doc_nodes) in &doc_nodes_by_url {
577-
let short_path = ShortPath::new(&ctx, specifier.clone());
578-
588+
for (short_path, doc_nodes) in &doc_nodes_by_short_path {
579589
let partitions_for_nodes =
580590
partition::get_partitions_for_file(&ctx, doc_nodes);
581591

582592
let symbol_pages = generate_symbol_pages_for_module(
583593
&ctx,
584-
&short_path,
594+
short_path,
585595
&partitions_for_nodes,
586596
doc_nodes,
587597
);
@@ -605,7 +615,7 @@ pub fn generate(
605615
&root,
606616
&symbol_group_ctx.name,
607617
ctx.package_name.as_ref(),
608-
Some(short_path.clone()),
618+
Some(short_path),
609619
);
610620

611621
if options.composable_output {
@@ -669,10 +679,9 @@ pub fn generate(
669679

670680
let index = pages::IndexCtx::new(
671681
&ctx,
672-
Some(specifier),
673-
&doc_nodes_by_url,
674-
partitions_for_nodes,
675682
Some(short_path.clone()),
683+
&doc_nodes_by_short_path,
684+
partitions_for_nodes,
676685
);
677686

678687
if options.composable_output {
@@ -717,7 +726,7 @@ pub fn generate(
717726
files.insert(STYLESHEET_FILENAME.into(), STYLESHEET.into());
718727
files.insert(
719728
SEARCH_INDEX_FILENAME.into(),
720-
search::get_search_index_file(&ctx, &doc_nodes_by_url)?,
729+
search::get_search_index_file(&ctx, &doc_nodes_by_short_path)?,
721730
);
722731
files.insert(SCRIPT_FILENAME.into(), SCRIPT_JS.into());
723732

src/html/pages.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::sidepanels::SidepanelCtx;
33
use super::symbols::SymbolContentCtx;
44
use super::util::qualify_drilldown_name;
55
use super::util::BreadcrumbsCtx;
6-
use super::util::SectionHeaderCtx;
76
use super::DocNodeKindWithDrilldown;
87
use super::DocNodeWithContext;
98
use super::FileMode;
@@ -28,7 +27,6 @@ use crate::html::partition::Partition;
2827
use crate::variable::VariableDef;
2928
use crate::DocNode;
3029
use crate::DocNodeKind;
31-
use deno_ast::ModuleSpecifier;
3230
use indexmap::IndexMap;
3331
use serde::Serialize;
3432

@@ -51,7 +49,7 @@ impl HtmlHeadCtx {
5149
root: &str,
5250
page: &str,
5351
package_name: Option<&String>,
54-
current_file: Option<ShortPath>,
52+
current_file: Option<&ShortPath>,
5553
) -> Self {
5654
Self {
5755
title: format!(
@@ -61,7 +59,6 @@ impl HtmlHeadCtx {
6159
.unwrap_or_default()
6260
),
6361
current_file: current_file
64-
.as_ref()
6562
.map(|current_file| &*current_file.path)
6663
.unwrap_or_default()
6764
.to_string(),
@@ -87,40 +84,36 @@ pub struct IndexCtx {
8784
impl IndexCtx {
8885
pub const TEMPLATE: &'static str = "pages/index";
8986

87+
/// short_path is None in case there this is a root index page but there is no main entrypoint
9088
pub fn new(
9189
ctx: &GenerateCtx,
92-
specifier: Option<&ModuleSpecifier>,
93-
doc_nodes_by_url: &super::ContextDocNodesByUrl,
90+
short_path: Option<std::rc::Rc<ShortPath>>,
91+
doc_nodes_by_url: &super::ContextDocNodesByShortPath,
9492
partitions: Partition,
95-
file: Option<ShortPath>,
9693
) -> Self {
97-
let short_path = specifier
98-
.cloned()
99-
.map(|specifier| ShortPath::new(ctx, specifier));
100-
10194
// will be default on index page with no main entrypoint
10295
let default = vec![];
103-
let doc_nodes = specifier
104-
.or(ctx.main_entrypoint.as_ref())
105-
.and_then(|specifier| doc_nodes_by_url.get(specifier))
96+
let doc_nodes = short_path
97+
.as_ref()
98+
.and_then(|short_path| doc_nodes_by_url.get(short_path))
10699
.unwrap_or(&default);
107100

108101
let render_ctx = RenderContext::new(
109102
ctx,
110103
doc_nodes,
111104
short_path
112-
.as_ref()
113-
.map_or(UrlResolveKind::Root, UrlResolveKind::File),
105+
.as_deref()
106+
.map_or(UrlResolveKind::Root, ShortPath::as_resolve_kind),
114107
);
115108

116109
let module_doc = short_path.as_ref().map(|short_path| {
117110
super::jsdoc::ModuleDocCtx::new(&render_ctx, short_path, doc_nodes_by_url)
118111
});
119112

120113
let root = ctx.href_resolver.resolve_path(
121-
file
122-
.as_ref()
123-
.map_or(UrlResolveKind::Root, UrlResolveKind::File),
114+
short_path
115+
.as_deref()
116+
.map_or(UrlResolveKind::Root, ShortPath::as_resolve_kind),
124117
UrlResolveKind::Root,
125118
);
126119

@@ -135,7 +128,7 @@ impl IndexCtx {
135128
.into_iter()
136129
.map(|(title, nodes)| {
137130
(
138-
SectionHeaderCtx {
131+
crate::html::util::SectionHeaderCtx {
139132
title,
140133
href: None,
141134
doc: None,
@@ -157,10 +150,9 @@ impl IndexCtx {
157150

158151
let sidepanel_ctx = sidepanels::IndexSidepanelCtx::new(
159152
ctx,
160-
specifier,
153+
short_path.clone(),
161154
doc_nodes_by_url,
162155
partitions,
163-
file.as_ref(),
164156
);
165157

166158
IndexCtx {
@@ -186,7 +178,7 @@ impl AllSymbolsCtx {
186178
pub fn new(
187179
ctx: &GenerateCtx,
188180
partitions: crate::html::partition::EntrypointPartition,
189-
doc_nodes_by_url: &super::ContextDocNodesByUrl,
181+
doc_nodes_by_url: &super::ContextDocNodesByShortPath,
190182
) -> Self {
191183
// TODO(@crowlKats): handle doc_nodes in all symbols page for each symbol
192184
let render_ctx = RenderContext::new(ctx, &[], UrlResolveKind::AllSymbols);
@@ -196,7 +188,7 @@ impl AllSymbolsCtx {
196188
partitions
197189
.into_iter()
198190
.map(|(path, nodes)| {
199-
let module_doc_nodes = doc_nodes_by_url.get(&path.specifier).unwrap();
191+
let module_doc_nodes = doc_nodes_by_url.get(&path).unwrap();
200192

201193
let doc = module_doc_nodes
202194
.iter()

src/html/partition.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,3 @@ pub fn get_partitions_for_file(
318318
categories
319319
}
320320
}
321-
322-
pub fn get_partitions_for_main_entrypoint(
323-
ctx: &GenerateCtx,
324-
doc_nodes_by_url: &super::ContextDocNodesByUrl,
325-
) -> Partition {
326-
let doc_nodes = ctx
327-
.main_entrypoint
328-
.as_ref()
329-
.and_then(|main_entrypoint| doc_nodes_by_url.get(main_entrypoint));
330-
331-
if let Some(doc_nodes) = doc_nodes {
332-
get_partitions_for_file(ctx, doc_nodes)
333-
} else {
334-
Default::default()
335-
}
336-
}

src/html/search.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use super::ContextDocNodesByShortPath;
12
use super::DocNodeWithContext;
23
use super::GenerateCtx;
3-
use super::{ContextDocNodesByUrl, ShortPath};
4+
use super::ShortPath;
45
use crate::node::Location;
56
use crate::DocNodeKind;
67
use deno_ast::ModuleSpecifier;
@@ -184,7 +185,7 @@ fn doc_node_into_search_index_nodes(
184185

185186
pub fn generate_search_index(
186187
ctx: &GenerateCtx,
187-
doc_nodes_by_url: &ContextDocNodesByUrl,
188+
doc_nodes_by_url: &ContextDocNodesByShortPath,
188189
) -> serde_json::Value {
189190
let doc_nodes = doc_nodes_by_url.values().flatten().collect::<Vec<_>>();
190191

@@ -225,7 +226,7 @@ pub fn generate_search_index(
225226

226227
pub(crate) fn get_search_index_file(
227228
ctx: &GenerateCtx,
228-
doc_nodes_by_url: &ContextDocNodesByUrl,
229+
doc_nodes_by_url: &ContextDocNodesByShortPath,
229230
) -> Result<String, anyhow::Error> {
230231
let search_index = generate_search_index(ctx, doc_nodes_by_url);
231232
let search_index_str = serde_json::to_string(&search_index)?;

0 commit comments

Comments
 (0)