Skip to content

Commit

Permalink
feat(html): allow overwriting the file identifier for usages
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 8, 2024
1 parent ea552e0 commit e2453b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl UsageComposer for EmptyResolver {
name: "".to_string(),
icon: None,
},
usage_to_md(nodes, current_file.specifier.as_str()),
usage_to_md(nodes, current_file.specifier.as_str(), None),
)])
})
.unwrap_or_default()
Expand Down
35 changes: 21 additions & 14 deletions src/html/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn usage_to_md(
ctx: &RenderContext,
doc_nodes: &[DocNodeWithContext],
url: &str,
custom_file_identifier: Option<&str>,
) -> String {
let usage =
if let UrlResolveKind::Symbol { symbol, .. } = ctx.get_current_resolve() {
Expand All @@ -48,7 +49,7 @@ fn usage_to_md(
if top_node.is_default.is_some_and(|is_default| is_default) {
let default_name = top_node.get_name();
if default_name == "default" {
get_identifier_for_file(ctx).into()
get_identifier_for_file(ctx, custom_file_identifier).into()
} else {
default_name.into()
}
Expand Down Expand Up @@ -125,16 +126,20 @@ fn usage_to_md(

usage_statement
} else {
let module_import_symbol = get_identifier_for_file(ctx);
let module_import_symbol =
get_identifier_for_file(ctx, custom_file_identifier);

format!(r#"import * as {module_import_symbol} from "{url}";"#)
};

format!("```typescript\n{usage}\n```")
}

fn get_identifier_for_file(ctx: &RenderContext) -> String {
let maybe_idenfitier =
fn get_identifier_for_file(
ctx: &RenderContext,
custom_file_identifier: Option<&str>,
) -> String {
let maybe_identifier =
if let Some(file) = ctx.get_current_resolve().get_file() {
ctx
.ctx
Expand All @@ -154,27 +159,26 @@ fn get_identifier_for_file(ctx: &RenderContext) -> String {
}
})
})
} else if let Some(context_name) = custom_file_identifier {
Some(context_name.to_string())
} else {
ctx.ctx.package_name.clone()
};

maybe_idenfitier.as_ref().map_or_else(
maybe_identifier.as_ref().map_or_else(
|| "mod".to_string(),
|identifier| IDENTIFIER_RE.replace_all(identifier, "_").to_string(),
)
}

#[cfg(not(feature = "rust"))]
pub type UsageToMd<'a> = &'a js_sys::Function;

#[cfg(feature = "rust")]
pub type UsageToMd<'a> = &'a dyn Fn(&[DocNodeWithContext], &str) -> String;
pub type UsageToMd<'a> =
&'a dyn Fn(&[DocNodeWithContext], &str, Option<&str>) -> String;

#[derive(Clone, Debug, Serialize)]
struct UsageCtx {
name: String,
content: String,
icon: Option<std::borrow::Cow<'static, str>>,
icon: Option<Cow<'static, str>>,
additional_css: String,
}

Expand All @@ -198,9 +202,12 @@ impl UsagesCtx {
}

let usage_ctx = ctx.clone();
let usage_to_md_closure = move |nodes: &[DocNodeWithContext], url: &str| {
usage_to_md(&usage_ctx, nodes, url)
};
let usage_to_md_closure =
move |nodes: &[DocNodeWithContext],
url: &str,
custom_file_identifier: Option<&str>| {
usage_to_md(&usage_ctx, nodes, url, custom_file_identifier)
};

let usages = ctx.ctx.usage_composer.compose(
doc_nodes,
Expand Down

0 comments on commit e2453b6

Please sign in to comment.