Skip to content

Commit

Permalink
Merge branch 'main' into tailwind-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Dec 14, 2023
2 parents e0f3d2c + d31afbb commit a2257e4
Show file tree
Hide file tree
Showing 20 changed files with 459 additions and 146 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deno_doc"
version = "0.75.1"
version = "0.79.0"
edition = "2021"
description = "doc generation for deno"
authors = ["the Deno authors"]
Expand Down
7 changes: 7 additions & 0 deletions examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,19 @@ fn generate_docs_directory(
let cwd = current_dir().unwrap();
let output_dir_resolved = cwd.join(output_dir);

let mut index_map = IndexMap::new();
if let Some(main_entrypoint) = main_entrypoint.as_ref() {
index_map.insert(main_entrypoint.clone(), String::from("."));
}

let options = deno_doc::html::GenerateOptions {
package_name: Some(name),
main_entrypoint,
global_symbols: Default::default(),
global_symbol_href_resolver: std::rc::Rc::new(|_, _| String::new()),
url_resolver: std::rc::Rc::new(deno_doc::html::default_url_resolver),
rewrite_map: Some(index_map),
hide_module_doc_title: false,
};
let html = deno_doc::html::generate(options.clone(), doc_nodes_by_url)?;

Expand Down
6 changes: 4 additions & 2 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub(crate) fn render_doc_entry(

#[derive(Debug, Serialize, Clone)]
pub struct ModuleDocCtx {
url: String,
title: Option<String>,
docs: String,
}

Expand All @@ -286,7 +286,9 @@ impl ModuleDocCtx {
let rendered_docs = render_markdown(docs_md, render_ctx);

Self {
url: ctx.url_to_short_path(main_entrypoint),
title: (!ctx.hide_module_doc_title).then(|| {
super::short_path_to_name(ctx.url_to_short_path(main_entrypoint))
}),
docs: rendered_docs,
}
})
Expand Down
100 changes: 64 additions & 36 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ mod jsdoc;
mod pages;
mod parameters;
mod search;
mod sidepanels;
pub mod sidepanels;
mod symbol;
mod symbols;
mod types;
mod util;

pub use jsdoc::ModuleDocCtx;
pub use search::generate_search_index;
pub use symbol::SymbolGroupCtx;

pub use self::symbols::namespace;
pub use self::util::DocNodeKindCtx;
pub use self::util::GlobalSymbolHrefResolver;
pub use self::util::NamespacedGlobalSymbols;
pub use self::util::NamespacedSymbols;
pub use self::util::RenderContext;
pub use symbols::namespace;
pub use util::DocNodeKindCtx;
pub use util::GlobalSymbolHrefResolver;
pub use util::NamespacedGlobalSymbols;
pub use util::NamespacedSymbols;
pub use util::RenderContext;

pub const STYLESHEET: &str = include_str!("./templates/styles.css");
pub const STYLESHEET_FILENAME: &str = "styles.css";
Expand Down Expand Up @@ -69,9 +69,12 @@ pub fn default_url_resolver(
resolve: UrlResolveKind,
) -> String {
let backs = match current {
UrlResolveKind::Symbol { file, .. } | UrlResolveKind::File(file) => {
"../".repeat(file.split('.').count() + 1)
}
UrlResolveKind::Symbol { file, .. } | UrlResolveKind::File(file) => "../"
.repeat(if file == "." {
1
} else {
file.split('.').count() + 1
}),
UrlResolveKind::Root | UrlResolveKind::AllSymbols => String::new(),
};

Expand Down Expand Up @@ -101,27 +104,32 @@ pub struct GenerateOptions {
pub global_symbols: NamespacedGlobalSymbols,
pub global_symbol_href_resolver: GlobalSymbolHrefResolver,
pub url_resolver: UrlResolver,
pub rewrite_map: Option<IndexMap<ModuleSpecifier, String>>,
pub hide_module_doc_title: bool,
}

pub struct GenerateCtx<'ctx> {
pub package_name: Option<String>,
pub common_ancestor: Option<PathBuf>,
pub main_entrypoint: Option<ModuleSpecifier>,
pub tt: Rc<TinyTemplate<'ctx>>,
pub global_symbols: NamespacedGlobalSymbols,
pub global_symbol_href_resolver: GlobalSymbolHrefResolver,
pub url_resolver: UrlResolver,
pub rewrite_map: Option<IndexMap<ModuleSpecifier, String>>,
pub hide_module_doc_title: bool,
}

impl<'ctx> GenerateCtx<'ctx> {
// @foo/bar
// /mod.ts -> file:///mod.ts

// "baz": "/mod.ts"

// @foo/bar/doc/mod.ts/~/symbol
// @foo/bar/doc/baz/~/symbol

pub fn url_to_short_path(&self, url: &ModuleSpecifier) -> String {
if let Some(rewrite) = self
.rewrite_map
.as_ref()
.and_then(|rewrite_map| rewrite_map.get(url))
{
return rewrite.to_owned();
}

if url.scheme() != "file" {
return url.to_string();
}
Expand All @@ -140,6 +148,19 @@ impl<'ctx> GenerateCtx<'ctx> {
}
}

fn short_path_to_name(short_path: String) -> String {
if short_path == "." || short_path.is_empty() {
"main".to_string()
} else {
short_path
.strip_prefix('.')
.unwrap_or(&short_path)
.strip_prefix('/')
.unwrap_or(&short_path)
.to_string()
}
}

#[derive(Clone, Debug)]
pub struct DocNodeWithContext {
pub origin: Option<String>,
Expand All @@ -153,10 +174,6 @@ pub fn setup_tt<'t>() -> Result<Rc<TinyTemplate<'t>>, anyhow::Error> {
"html_head.html",
include_str!("./templates/html_head.html"),
)?;
tt.add_template(
"html_tail.html",
include_str!("./templates/html_tail.html"),
)?;
tt.add_template(
"all_symbols.html",
include_str!("./templates/all_symbols.html"),
Expand All @@ -173,7 +190,10 @@ pub fn setup_tt<'t>() -> Result<Rc<TinyTemplate<'t>>, anyhow::Error> {
"sidepanel.html",
include_str!("./templates/sidepanel.html"),
)?;
tt.add_template("page.html", include_str!("./templates/page.html"))?;
tt.add_template(
"symbol_page.html",
include_str!("./templates/symbol_page.html"),
)?;
tt.add_template(
"doc_entry.html",
include_str!("./templates/doc_entry.html"),
Expand Down Expand Up @@ -207,6 +227,10 @@ pub fn setup_tt<'t>() -> Result<Rc<TinyTemplate<'t>>, anyhow::Error> {
"module_doc.html",
include_str!("./templates/module_doc.html"),
)?;
tt.add_template(
"breadcrumbs.html",
include_str!("./templates/breadcrumbs.html"),
)?;
Ok(Rc::new(tt))
}

Expand All @@ -224,10 +248,13 @@ pub fn generate(
let ctx = GenerateCtx {
package_name: options.package_name,
common_ancestor,
main_entrypoint: options.main_entrypoint,
tt,
global_symbols: options.global_symbols,
global_symbol_href_resolver: options.global_symbol_href_resolver,
url_resolver: options.url_resolver,
rewrite_map: options.rewrite_map,
hide_module_doc_title: options.hide_module_doc_title,
};
let mut files = HashMap::new();

Expand All @@ -252,17 +279,14 @@ pub fn generate(

// Index page
{
let partitions_for_nodes = get_partitions_for_specifier(
&ctx,
options.main_entrypoint.as_ref(),
doc_nodes_by_url,
);
let partitions_for_entrypoint_nodes =
get_partitions_for_main_entrypoint(&ctx, doc_nodes_by_url);

let index = pages::render_index(
&ctx,
options.main_entrypoint.as_ref(),
ctx.main_entrypoint.as_ref(),
doc_nodes_by_url,
partitions_for_nodes,
partitions_for_entrypoint_nodes,
all_symbols.clone(),
None,
)?;
Expand Down Expand Up @@ -292,6 +316,7 @@ pub fn generate(

files.extend(pages::generate_pages_for_file(
&ctx,
specifier.to_owned(),
&partitions_for_nodes,
short_path.clone(),
doc_nodes,
Expand Down Expand Up @@ -360,20 +385,23 @@ pub fn get_partitions_for_file(
}
}

pub fn get_partitions_for_specifier(
pub fn get_partitions_for_main_entrypoint(
ctx: &GenerateCtx,
main_entrypoint: Option<&ModuleSpecifier>,
doc_nodes_by_url: &IndexMap<ModuleSpecifier, Vec<DocNode>>,
) -> IndexMap<String, Vec<DocNodeWithContext>> {
let doc_nodes = main_entrypoint
let doc_nodes = ctx
.main_entrypoint
.as_ref()
.and_then(|main_entrypoint| doc_nodes_by_url.get(main_entrypoint));

if let Some(doc_nodes) = doc_nodes {
let doc_nodes_with_context = doc_nodes
.iter()
.map(|node| DocNodeWithContext {
doc_node: node.clone(),
origin: Some(ctx.url_to_short_path(main_entrypoint.as_ref().unwrap())),
origin: Some(
ctx.url_to_short_path(ctx.main_entrypoint.as_ref().unwrap()),
),
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -421,7 +449,7 @@ pub fn partition_nodes_by_name(
partitions
}

fn find_common_ancestor<'a>(
pub fn find_common_ancestor<'a>(
urls: impl Iterator<Item = &'a ModuleSpecifier>,
single_file_is_common_ancestor: bool,
) -> Option<PathBuf> {
Expand Down
Loading

0 comments on commit a2257e4

Please sign in to comment.