Skip to content

Commit

Permalink
feat: fix various minor issues & improve inferring (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Mar 27, 2024
1 parent dc556c8 commit 0f1ef3e
Show file tree
Hide file tree
Showing 35 changed files with 1,307 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl core::ops::Deref for DocNodeWithContext {

pub fn setup_hbs<'t>() -> Result<Handlebars<'t>, anyhow::Error> {
let mut reg = Handlebars::new();
reg.register_escape_fn(|str| html_escape::encode_safe(str).to_string());
reg.register_escape_fn(|str| html_escape::encode_safe(str).into_owned());
reg.set_strict_mode(true);

#[cfg(debug_assertions)]
Expand Down
5 changes: 1 addition & 4 deletions src/html/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub(crate) fn render_params(

format!("<span>{items}</span>")
} else {
// TODO(bartlomieju): refactor to use template
let mut items = Vec::with_capacity(params.len());

for (i, def) in params.iter().enumerate() {
Expand Down Expand Up @@ -63,13 +62,11 @@ pub(crate) fn param_name(param: &ParamDef, i: usize) -> (String, String) {
),
ParamPatternDef::Assign { left, .. } => param_name(left, i),
ParamPatternDef::Identifier { name, .. } => {
(html_escape::encode_safe(name).to_string(), name.clone())
(html_escape::encode_safe(name).into_owned(), name.clone())
}
ParamPatternDef::Rest { arg } => (
format!("<span>...{}</span>", param_name(arg, i).0),
format!("(...{})", param_name(arg, i).1),
),
}
}

// TODO: classes: italic
31 changes: 21 additions & 10 deletions src/html/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ fn doc_node_into_search_index_nodes(
.ns_qualifiers
.is_empty()
{
name.to_string()
html_escape::encode_safe(name).into_owned()
} else {
format!("{}.{}", doc_nodes[0].ns_qualifiers.join("."), name)
format!(
"{}.{}",
doc_nodes[0].ns_qualifiers.join("."),
html_escape::encode_safe(name)
)
};

if doc_nodes[0].kind != DocNodeKind::Namespace {
Expand All @@ -50,15 +54,16 @@ fn doc_node_into_search_index_nodes(
.map(|main_entrypoint| main_entrypoint != &location_url)
.unwrap_or(true)
{
ctx.url_to_short_path(&location_url).to_name()
html_escape::encode_safe(&ctx.url_to_short_path(&location_url).to_name())
.into_owned()
} else {
String::new()
};

return vec![SearchIndexNode {
kind: kinds,
name,
file: doc_nodes[0].origin.as_str().to_string(),
file: html_escape::encode_safe(doc_nodes[0].origin.as_str()).into_owned(),
location,
declaration_kind: doc_nodes[0].declaration_kind,
deprecated,
Expand All @@ -77,15 +82,16 @@ fn doc_node_into_search_index_nodes(
.map(|main_entrypoint| main_entrypoint != &location_url)
.unwrap_or(true)
{
ctx.url_to_short_path(&location_url).to_name()
html_escape::encode_safe(&ctx.url_to_short_path(&location_url).to_name())
.into_owned()
} else {
String::new()
};

nodes.push(SearchIndexNode {
kind: kinds,
name,
file: doc_nodes[0].origin.as_str().to_string(),
file: html_escape::encode_safe(doc_nodes[0].origin.as_str()).into_owned(),
location,
declaration_kind: doc_nodes[0].declaration_kind,
deprecated,
Expand Down Expand Up @@ -117,23 +123,28 @@ fn doc_node_into_search_index_nodes(
.map(|main_entrypoint| main_entrypoint != &location_url)
.unwrap_or(true)
{
ctx.url_to_short_path(&location_url).to_name()
html_escape::encode_safe(&ctx.url_to_short_path(&location_url).to_name())
.into_owned()
} else {
String::new()
};

let name = if ns_qualifiers_.is_empty() {
el_name.to_string()
html_escape::encode_safe(el_name).into_owned()
} else {
format!("{}.{}", ns_qualifiers_.join("."), el_name)
format!(
"{}.{}",
ns_qualifiers_.join("."),
html_escape::encode_safe(el_name)
)
};

let kinds = el_nodes.iter().map(|node| node.kind).collect();

nodes.push(SearchIndexNode {
kind: kinds,
name,
file: doc_nodes[0].origin.as_str().to_string(),
file: html_escape::encode_safe(doc_nodes[0].origin.as_str()).into_owned(),
location,
declaration_kind: el_nodes[0].declaration_kind,
deprecated,
Expand Down
9 changes: 5 additions & 4 deletions src/html/symbols/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(crate) fn render_class(

if let Some(type_params) = crate::html::types::render_type_params(
ctx,
&doc_node.js_doc,
&class_def.type_params,
&doc_node.location,
) {
Expand Down Expand Up @@ -127,7 +128,7 @@ fn render_constructors(
DocEntryCtx::new(
ctx,
&id,
name,
&html_escape::encode_safe(&name),
None,
&format!("({params})"),
HashSet::from([Tag::New]),
Expand Down Expand Up @@ -379,7 +380,7 @@ fn render_class_accessor(
DocEntryCtx::new(
ctx,
&id,
name,
&html_escape::encode_safe(&name),
ctx.lookup_symbol_href(&qualify_drilldown_name(
class_name,
name,
Expand Down Expand Up @@ -418,7 +419,7 @@ fn render_class_method(
Some(DocEntryCtx::new(
ctx,
&id,
&method.name,
&html_escape::encode_safe(&method.name),
ctx.lookup_symbol_href(&qualify_drilldown_name(
class_name,
&method.name,
Expand Down Expand Up @@ -461,7 +462,7 @@ fn render_class_property(
DocEntryCtx::new(
ctx,
&id,
&property.name,
&html_escape::encode_safe(&property.name),
ctx.lookup_symbol_href(&qualify_drilldown_name(
class_name,
&property.name,
Expand Down
2 changes: 1 addition & 1 deletion src/html/symbols/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn render_enum(
DocEntryCtx::new(
render_ctx,
&id,
&member.name,
&html_escape::encode_safe(&member.name),
None,
&member
.init
Expand Down
12 changes: 6 additions & 6 deletions src/html/symbols/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::html::parameters::render_params;
use crate::html::render_context::RenderContext;
use crate::html::types::render_type_def;
use crate::html::types::render_type_def_colon;
use crate::html::types::render_type_params;
use crate::html::types::type_params_summary;
use crate::html::util::*;
use crate::html::DocNodeWithContext;
Expand Down Expand Up @@ -154,8 +153,6 @@ fn render_single_function(
.collect::<HashSet<&str>>();
let ctx = &ctx.with_current_type_params(current_type_params);

// TODO: tags

let param_docs =
doc_node
.js_doc
Expand Down Expand Up @@ -251,9 +248,12 @@ fn render_single_function(
sections.push(examples);
}

if let Some(type_params) =
render_type_params(ctx, &function_def.type_params, &doc_node.location)
{
if let Some(type_params) = crate::html::types::render_type_params(
ctx,
&doc_node.js_doc,
&function_def.type_params,
&doc_node.location,
) {
sections.push(type_params);
}

Expand Down
18 changes: 10 additions & 8 deletions src/html/symbols/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::html::parameters::render_params;
use crate::html::render_context::RenderContext;
use crate::html::symbols::class::IndexSignatureCtx;
use crate::html::types::render_type_def_colon;
use crate::html::types::render_type_params;
use crate::html::types::type_params_summary;
use crate::html::util::*;
use crate::html::DocNodeWithContext;
Expand All @@ -22,9 +21,12 @@ pub(crate) fn render_interface(

let mut sections = vec![];

if let Some(type_params) =
render_type_params(ctx, &interface_def.type_params, &doc_node.location)
{
if let Some(type_params) = crate::html::types::render_type_params(
ctx,
&doc_node.js_doc,
&interface_def.type_params,
&doc_node.location,
) {
sections.push(type_params);
}

Expand Down Expand Up @@ -185,9 +187,9 @@ fn render_properties(
ctx,
&id,
&if property.computed {
format!("[{}]", property.name)
format!("[{}]", html_escape::encode_safe(&property.name))
} else {
property.name.clone()
html_escape::encode_safe(&property.name).into_owned()
},
ctx.lookup_symbol_href(&qualify_drilldown_name(
interface_name,
Expand Down Expand Up @@ -226,9 +228,9 @@ fn render_methods(
let name = if method.name == "new" {
"<span>new</span>".to_string()
} else if method.computed {
format!("[{}]", method.name)
format!("[{}]", html_escape::encode_safe(&method.name))
} else {
method.name.clone()
html_escape::encode_safe(&method.name).into_owned()
};

let return_type = method
Expand Down
2 changes: 1 addition & 1 deletion src/html/symbols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl DocBlockSubtitleCtx {
if let Some(extends) = class_def.extends.as_ref() {
class_extends = Some(DocBlockClassSubtitleExtendsCtx {
href: ctx.lookup_symbol_href(extends),
symbol: extends.to_owned(),
symbol: html_escape::encode_safe(extends).into_owned(),
type_args: super::types::type_arguments(
ctx,
&class_def.super_type_params,
Expand Down
3 changes: 2 additions & 1 deletion src/html/symbols/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ pub(crate) fn render_type_alias(

let id = name_to_id("typeAlias", doc_node.get_name());

// TODO: tags, TypeParamsDoc
// TODO: tags

let mut sections = vec![];

if let Some(type_params) = crate::html::types::render_type_params(
ctx,
&doc_node.js_doc,
&type_alias_def.type_params,
&doc_node.location,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/html/templates/deprecated.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{{~#if (ne this "")~}}
<div class="ml-1 pl-2 border-l-4 border-red-300">
{{{this}}}
{{{this}}} {{! markdown rendering }}
</div>
{{~/if~}}
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/html/templates/doc_block_subtitle_class.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div>
<span class="text-stone-400 italic"> implements </span>
{{~#each implements~}}
{{{~this~}}}
{{{~this~}}} {{! typedef rendering }}
{{~#unless @last~}}
<span>, </span>
{{~/unless~}}
Expand All @@ -15,12 +15,12 @@
<span class="text-stone-400 italic"> extends </span>
{{~#if href ~}}
<a class="link" href="{{href}}">
{{{~symbol~}}}
{{~symbol~}}
</a>
{{~else~}}
<span>{{{symbol}}}</span>
<span>{{symbol}}</span>
{{~/if~}}
<span>{{{type_args}}}</span>
<span>{{{type_args}}} {{! typedef rendering }}</span>
</div>
{{~else~}}
{{~/with~}}
2 changes: 1 addition & 1 deletion src/html/templates/doc_block_subtitle_interface.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div>
<span class="text-stone-400 italic"> extends </span>
{{~#each extends~}}
{{{~this~}}}
{{{~this~}}} {{! typedef rendering }}
{{~#unless @last~}}
<span>, </span>
{{~/unless~}}
Expand Down
4 changes: 2 additions & 2 deletions src/html/templates/doc_entry.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<code>
{{~#if name_href~}}
<a class="font-bold link" href="{{{name_href}}}">{{{name}}}</a>
<a class="font-bold link" href="{{name_href}}">{{{name}}}</a>
{{~else~}}
<span class="font-bold">{{{name}}}</span>
{{~/if~}}
Expand All @@ -28,7 +28,7 @@

{{~#if js_doc~}}
<div class="markdown_border">
{{{~js_doc~}}}
{{{~js_doc~}}} {{! markdown rendering }}
</div>
{{~/if~}}
</div>
4 changes: 2 additions & 2 deletions src/html/templates/example.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<details id="{{id}}" class="group" open>
<summary class="list-none flex items-center gap-2 py-2 rounded-lg w-full leading-6 cursor-pointer">
<div class="text-stone-600 group-open:rotate-90 select-none">&#x25B6;</div>
{{{~markdown_title~}}}
{{{~markdown_title~}}} {{! markdown rendering }}
</summary>
<div class="!ml-2 markdown_border">
{{{~markdown_body~}}}
{{{~markdown_body~}}} {{! markdown rendering }}
</div>
</details>
</div>
6 changes: 3 additions & 3 deletions src/html/templates/function.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{~#each overloads_ctx~}}
<style scoped>
{{{~this.additional_css~}}}
{{{~this.additional_css~}}} {{! css }}
</style>
<input type="radio" name="{{this.function_id}}" id="{{this.overload_id}}" {{this.html_attrs}} />
{{~/each~}}
Expand All @@ -10,11 +10,11 @@
<label for="{{this.overload_id}}" class="space-y-1 block px-4 py-2.5 rounded-lg border border-stone-300 cursor-pointer hover:bg-stone-100">
{{~> deprecated this.deprecated ~}}
<code class="text-sm">
<span class="font-bold">{{this.name}}</span><span class="font-medium">{{{this.summary}}}</span>
<span class="font-bold">{{this.name}}</span><span class="font-medium">{{{this.summary}}} {{! typedef rendering }}</span>
</code>
{{~#if this.summary_doc~}}
<div class="markdown_border">
{{{~this.summary_doc~}}}
{{{~this.summary_doc~}}} {{! markdown rendering }}
</div>
{{~/if~}}
</label>
Expand Down
4 changes: 2 additions & 2 deletions src/html/templates/index_signature.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
{{~#if readonly~}}
<span>readonly </span>
{{~/if~}}
[{{{params}}}]
{{{~ts_type~}}}
[{{{params}}}] {{! param rendering }}
{{{~ts_type~}}} {{! typedef rendering }}
</div>
2 changes: 1 addition & 1 deletion src/html/templates/module_doc.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

{{~#if toc~}}
<nav class="flex-none max-w-52 text-sm max-lg:hidden sticky top-0 py-4 max-h-screen box-border">
{{{~toc~}}}
{{{~toc~}}} {{! markdown rendering ToC }}
</nav>
{{~/if~}}
</section>
2 changes: 1 addition & 1 deletion src/html/templates/namespace_section.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
<div class="block py-1 text-sm leading-5 text-stone-600 !ml-2 markdown_border">
{{~#if this.docs~}}
{{{~this.docs~}}}
{{{~this.docs~}}} {{! markdown rendering }}
{{~else~}}
<span class="italic">No documentation available</span>
{{~/if~}}
Expand Down
Loading

0 comments on commit 0f1ef3e

Please sign in to comment.