From 3bf9b7bb6187ecf232dca5f5a582c981e07d9cd8 Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 2 Aug 2024 18:00:14 +0200 Subject: [PATCH 1/4] fix(html): better titles --- src/html/mod.rs | 11 ++++++++++- src/html/pages.rs | 17 ++++++++++++----- tests/html_test.rs | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index 0c413633..eea664b9 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -823,9 +823,18 @@ pub fn generate( UrlResolveKind::Root, ); + let title = breadcrumbs_ctx + .parts + .iter() + .skip(1) + .rev() + .map(|breadcrumb| breadcrumb.name.as_str()) + .collect::>() + .join(" - "); + let html_head_ctx = pages::HtmlHeadCtx::new( &root, - &symbol_group_ctx.name, + Some(&title), ctx.package_name.as_ref(), Some(short_path), ctx.disable_search, diff --git a/src/html/pages.rs b/src/html/pages.rs index ffecd89b..b6aec646 100644 --- a/src/html/pages.rs +++ b/src/html/pages.rs @@ -47,14 +47,15 @@ impl HtmlHeadCtx { pub fn new( root: &str, - page: &str, + page: Option<&str>, package_name: Option<&String>, current_file: Option<&ShortPath>, disable_search: bool, ) -> Self { Self { title: format!( - "{page} - {}documentation", + "{}{}documentation", + page.map(|page| format!("{page} - ")).unwrap_or_default(), package_name .map(|package_name| format!("{package_name} ")) .unwrap_or_default() @@ -222,7 +223,13 @@ impl IndexCtx { let html_head_ctx = HtmlHeadCtx::new( &root, - "Index", + short_path.as_ref().and_then(|short_path| { + if short_path.is_main { + None + } else { + Some(short_path.display_name()) + } + }), ctx.package_name.as_ref(), None, ctx.disable_search, @@ -385,7 +392,7 @@ impl IndexCtx { let html_head_ctx = HtmlHeadCtx::new( &root, - name, + Some(name), ctx.package_name.as_ref(), None, ctx.disable_search, @@ -450,7 +457,7 @@ impl AllSymbolsCtx { let html_head_ctx = HtmlHeadCtx::new( "./", - "All Symbols", + Some("All Symbols"), ctx.package_name.as_ref(), None, ctx.disable_search, diff --git a/tests/html_test.rs b/tests/html_test.rs index b585cf3d..ce8ff6a9 100644 --- a/tests/html_test.rs +++ b/tests/html_test.rs @@ -361,7 +361,7 @@ async fn symbol_group() { let html_head_ctx = pages::HtmlHeadCtx::new( &root, - &symbol_group_ctx.name, + Some(&symbol_group_ctx.name), ctx.package_name.as_ref(), Some(short_path), false, From 2f983bea244ae335a0b0397c46cbf44a35d89caf Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 2 Aug 2024 18:09:19 +0200 Subject: [PATCH 2/4] merge symbol parts together --- src/html/mod.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index eea664b9..1d0f0904 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -823,18 +823,24 @@ pub fn generate( UrlResolveKind::Root, ); - let title = breadcrumbs_ctx - .parts - .iter() - .skip(1) - .rev() - .map(|breadcrumb| breadcrumb.name.as_str()) - .collect::>() - .join(" - "); + let mut title_parts = + Vec::with_capacity(breadcrumbs_ctx.parts.len()); + let mut symbol_parts = vec![]; + + for breadcrumb in breadcrumbs_ctx.parts.iter().skip(1) { + if breadcrumb.is_symbol { + symbol_parts.push(breadcrumb.name.as_str()); + } else { + title_parts.push(breadcrumb.name.as_str()); + } + } + let symbol_name = symbol_parts.join("."); + title_parts.push(&symbol_name); + title_parts.reverse(); let html_head_ctx = pages::HtmlHeadCtx::new( &root, - Some(&title), + Some(&title_parts.join(" - ")), ctx.package_name.as_ref(), Some(short_path), ctx.disable_search, From 124de7ed48f9940ad8ad82daf57682650f4d884a Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 2 Aug 2024 18:17:01 +0200 Subject: [PATCH 3/4] fix breadcrumbs for drilldown --- src/html/mod.rs | 3 +-- src/html/pages.rs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index 1d0f0904..b5d6b4cc 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -823,8 +823,7 @@ pub fn generate( UrlResolveKind::Root, ); - let mut title_parts = - Vec::with_capacity(breadcrumbs_ctx.parts.len()); + let mut title_parts = vec![]; let mut symbol_parts = vec![]; for breadcrumb in breadcrumbs_ctx.parts.iter().skip(1) { diff --git a/src/html/pages.rs b/src/html/pages.rs index b6aec646..5ed75da8 100644 --- a/src/html/pages.rs +++ b/src/html/pages.rs @@ -742,13 +742,18 @@ pub fn render_symbol_page( symbol: namespaced_name, }) .with_category(if render_ctx.ctx.file_mode == FileMode::SingleDts { - doc_nodes[0].js_doc.tags.iter().find_map(|tag| { - if let JsDocTag::Category { doc } = tag { - Some(doc.as_ref()) - } else { - None - } - }) + doc_nodes[0] + .get_topmost_ancestor() + .js_doc + .tags + .iter() + .find_map(|tag| { + if let JsDocTag::Category { doc } = tag { + Some(doc.as_ref()) + } else { + None + } + }) } else { None }); From e8e7c118a5e5831f19e3e02f29db4714b6f446fd Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 2 Aug 2024 18:29:55 +0200 Subject: [PATCH 4/4] cleanup & update tests --- src/html/mod.rs | 15 +- src/html/util.rs | 17 ++ tests/html_test.rs | 9 +- .../html_test__html_doc_files-2.snap | 2 +- .../html_test__html_doc_files_rewrite-10.snap | 28 +-- .../html_test__html_doc_files_rewrite-11.snap | 2 +- .../html_test__html_doc_files_rewrite-12.snap | 24 +- .../html_test__html_doc_files_rewrite-13.snap | 2 +- .../html_test__html_doc_files_rewrite-14.snap | 26 +-- .../html_test__html_doc_files_rewrite-15.snap | 26 +-- .../html_test__html_doc_files_rewrite-16.snap | 2 +- .../html_test__html_doc_files_rewrite-17.snap | 2 +- .../html_test__html_doc_files_rewrite-2.snap | 205 ++---------------- .../html_test__html_doc_files_rewrite-21.snap | 2 +- .../html_test__html_doc_files_rewrite-3.snap | 26 +-- .../html_test__html_doc_files_rewrite-4.snap | 2 +- .../html_test__html_doc_files_rewrite-5.snap | 26 +-- .../html_test__html_doc_files_rewrite-6.snap | 26 +-- .../html_test__html_doc_files_rewrite-7.snap | 30 +-- .../html_test__html_doc_files_rewrite-8.snap | 26 +-- .../html_test__html_doc_files_rewrite-9.snap | 26 +-- .../html_test__html_doc_files_rewrite.snap | 6 +- 22 files changed, 143 insertions(+), 387 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index b5d6b4cc..1fce8c6b 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -823,19 +823,10 @@ pub fn generate( UrlResolveKind::Root, ); - let mut title_parts = vec![]; - let mut symbol_parts = vec![]; - - for breadcrumb in breadcrumbs_ctx.parts.iter().skip(1) { - if breadcrumb.is_symbol { - symbol_parts.push(breadcrumb.name.as_str()); - } else { - title_parts.push(breadcrumb.name.as_str()); - } - } - let symbol_name = symbol_parts.join("."); - title_parts.push(&symbol_name); + let mut title_parts = breadcrumbs_ctx.to_strings(); title_parts.reverse(); + // contains the package name, which we already render in the head + title_parts.pop(); let html_head_ctx = pages::HtmlHeadCtx::new( &root, diff --git a/src/html/util.rs b/src/html/util.rs index f1cdb876..53d1e5c5 100644 --- a/src/html/util.rs +++ b/src/html/util.rs @@ -14,6 +14,7 @@ use deno_ast::swc::atoms::once_cell::sync::Lazy; use indexmap::IndexSet; use regex::Regex; use serde::Serialize; +use std::borrow::Cow; use std::collections::HashMap; use std::collections::HashSet; use std::rc::Rc; @@ -332,6 +333,22 @@ pub struct BreadcrumbsCtx { impl BreadcrumbsCtx { pub const TEMPLATE: &'static str = "breadcrumbs"; + + pub fn to_strings(&self) -> Vec> { + let mut title_parts = vec![]; + let mut symbol_parts = vec![]; + + for breadcrumb in self.parts.iter() { + if breadcrumb.is_symbol { + symbol_parts.push(breadcrumb.name.as_str()); + } else { + title_parts.push(Cow::Borrowed(breadcrumb.name.as_str())); + } + } + title_parts.push(Cow::Owned(symbol_parts.join("."))); + + title_parts + } } #[derive(Debug, Serialize, Clone, Eq, PartialEq, Hash)] diff --git a/tests/html_test.rs b/tests/html_test.rs index ce8ff6a9..6c3ac1c7 100644 --- a/tests/html_test.rs +++ b/tests/html_test.rs @@ -200,10 +200,9 @@ async fn html_doc_files_rewrite() { .join("testdata") .join("multiple"); let mut rewrite_map = IndexMap::new(); - rewrite_map.insert( - ModuleSpecifier::from_file_path(multiple_dir.join("a.ts")).unwrap(), - ".".to_string(), - ); + let main_specifier = + ModuleSpecifier::from_file_path(multiple_dir.join("a.ts")).unwrap(); + rewrite_map.insert(main_specifier.clone(), ".".to_string()); rewrite_map.insert( ModuleSpecifier::from_file_path(multiple_dir.join("b.ts")).unwrap(), "foo".to_string(), @@ -212,7 +211,7 @@ async fn html_doc_files_rewrite() { let files = generate( GenerateOptions { package_name: None, - main_entrypoint: None, + main_entrypoint: Some(main_specifier), href_resolver: Rc::new(EmptyResolver {}), usage_composer: None, rewrite_map: Some(rewrite_map), diff --git a/tests/snapshots/html_test__html_doc_files-2.snap b/tests/snapshots/html_test__html_doc_files-2.snap index 48a6dd88..69a98a48 100644 --- a/tests/snapshots/html_test__html_doc_files-2.snap +++ b/tests/snapshots/html_test__html_doc_files-2.snap @@ -5,7 +5,7 @@ expression: "files.get(\"./index.html\").unwrap()" - Index - documentation + documentation diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-10.snap b/tests/snapshots/html_test__html_doc_files_rewrite-10.snap index 75067eff..461d7c21 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-10.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-10.snap @@ -9,20 +9,20 @@ expression: "files.get(\"./~/Foo.prototype.foo.html\").unwrap()" - - - + + + - - - - + + + +
diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-21.snap b/tests/snapshots/html_test__html_doc_files_rewrite-21.snap index 6e74eec6..595dfde0 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-21.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-21.snap @@ -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"],"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}]}; + window.DENO_DOC_SEARCH_INDEX = {"nodes":[{"kind":["class"],"name":"A","file":".","doc":"","location":{"filename":"default","line":45,"col":0,"byteIndex":741},"url":"././~/A.html","category":"","declarationKind":"private","deprecated":false},{"kind":["class"],"name":"B","file":".","doc":"","location":{"filename":"default","line":48,"col":0,"byteIndex":770},"url":"././~/B.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Bar","file":".","doc":"","location":{"filename":"default","line":31,"col":0,"byteIndex":588},"url":"././~/Bar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["typeAlias"],"name":"Baz","file":".","doc":"","location":{"filename":"default","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":"default","line":25,"col":0,"byteIndex":488},"url":"././~/Foo.html","category":"","declarationKind":"export","deprecated":false},{"kind":["class"],"name":"Foobar","file":".","doc":"","location":{"filename":"default","line":34,"col":0,"byteIndex":622},"url":"././~/Foobar.html","category":"","declarationKind":"export","deprecated":false},{"kind":["interface"],"name":"Hello","file":".","doc":"","location":{"filename":"default","line":37,"col":0,"byteIndex":655},"url":"././~/Hello.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"c","file":".","doc":"","location":{"filename":"default","line":57,"col":13,"byteIndex":933},"url":"././~/c.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function"],"name":"d","file":".","doc":"","location":{"filename":"default","line":59,"col":0,"byteIndex":961},"url":"././~/d.html","category":"","declarationKind":"export","deprecated":false},{"kind":["function","function","function"],"name":"qaz","file":".","doc":"","location":{"filename":"default","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}]}; })() diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-3.snap b/tests/snapshots/html_test__html_doc_files_rewrite-3.snap index dabc4da9..0ceb343c 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-3.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-3.snap @@ -9,28 +9,20 @@ expression: "files.get(\"./~/Bar.html\").unwrap()" - - - + + + - - - - + + + +
diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-4.snap b/tests/snapshots/html_test__html_doc_files_rewrite-4.snap index 251b4b6b..44deac7d 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-4.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-4.snap @@ -2,4 +2,4 @@ source: tests/html_test.rs expression: "files.get(\"./~/Bar.prototype.html\").unwrap()" --- - + diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-5.snap b/tests/snapshots/html_test__html_doc_files_rewrite-5.snap index 8c045305..a114fbf0 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-5.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-5.snap @@ -9,28 +9,20 @@ expression: "files.get(\"./~/Baz.html\").unwrap()" - - - + + + - - - - + + + +
diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-6.snap b/tests/snapshots/html_test__html_doc_files_rewrite-6.snap index a1703bbf..102582b0 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-6.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-6.snap @@ -9,20 +9,20 @@ expression: "files.get(\"./~/Baz.foo.html\").unwrap()" - - - + + + - - - - + + + +
diff --git a/tests/snapshots/html_test__html_doc_files_rewrite-8.snap b/tests/snapshots/html_test__html_doc_files_rewrite-8.snap index 46ccc69c..0311628e 100644 --- a/tests/snapshots/html_test__html_doc_files_rewrite-8.snap +++ b/tests/snapshots/html_test__html_doc_files_rewrite-8.snap @@ -9,20 +9,20 @@ expression: "files.get(\"./~/Foo.bar.html\").unwrap()" - - - + + + - - - - + + + +