Skip to content

Commit

Permalink
finish up
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 13, 2024
1 parent d22ba6f commit 60d5f68
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 79 deletions.
9 changes: 4 additions & 5 deletions examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use clap::App;
use clap::Arg;
use deno_doc::find_nodes_by_name_recursively;
use deno_doc::html::HrefResolver;
use deno_doc::html::UrlResolveKind;
use deno_doc::html::{
DocNodeWithContext, HrefResolver, UsageComposer, UsageComposerEntry,
};
use deno_doc::html::UsageComposer;
use deno_doc::html::UsageComposerEntry;
use deno_doc::DocNodeKind;
use deno_doc::DocParser;
use deno_doc::DocParserOptions;
Expand Down Expand Up @@ -216,7 +216,6 @@ impl UsageComposer for EmptyResolver {

fn compose(
&self,
nodes: &[DocNodeWithContext],
current_resolve: UrlResolveKind,
usage_to_md: deno_doc::html::UsageToMd,
) -> IndexMap<UsageComposerEntry, String> {
Expand All @@ -228,7 +227,7 @@ impl UsageComposer for EmptyResolver {
name: "".to_string(),
icon: None,
},
usage_to_md(nodes, current_file.specifier.as_str(), None),
usage_to_md(current_file.specifier.as_str(), None),
)])
})
.unwrap_or_default()
Expand Down
33 changes: 17 additions & 16 deletions js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,18 @@ export type UrlResolveKind =
| UrlResolveKindSymbol;

interface HrefResolver {
resolvePath(current: UrlResolveKind, target: UrlResolveKind): string;
resolvePath?(current: UrlResolveKind, target: UrlResolveKind): string;
/** Resolver for global symbols, like the Deno namespace or other built-ins */
resolveGlobalSymbol(symbol: string[]): string | undefined;
resolveGlobalSymbol?(symbol: string[]): string | undefined;
/** Resolver for symbols from non-relative imports */
resolveImportHref(symbol: string[], src: string): string | undefined;
resolveImportHref?(symbol: string[], src: string): string | undefined;
/** Resolve the URL used in source code link buttons. */
resolveSource(location: Location): string | undefined;
resolveSource?(location: Location): string | undefined;
/**
* Resolve external JSDoc module links.
* Returns a tuple with link and title.
*/
resolveExternalJsdocModule(
resolveExternalJsdocModule?(
module: string,
symbol?: string,
): { link: string; title: string } | undefined;
Expand All @@ -186,15 +186,14 @@ export interface UsageComposerEntry {
}

export type UsageToMd = (
_,
url: string,
customFileIdentifier: string | undefined,
) => string;

export interface UsageComposer {
singleMode: boolean;
compose(
current_resolve: UrlResolveKind,
currentResolve: UrlResolveKind,
usageToMd: UsageToMd,
): Map<UsageComposerEntry, string>;
}
Expand All @@ -203,15 +202,16 @@ interface GenerateOptions {
packageName?: string;
mainEntrypoint?: string;
usageComposer: UsageComposer;
hrefResolver: HrefResolver;
hrefResolver?: HrefResolver;
rewriteMap?: Record<string, string>;
categoryDocs?: Record<string, string | undefined>;
disableSearch: boolean;
disableSearch?: boolean;
symbolRedirectMap?: Record<string, Record<string, string>>;
defaultRedirectMap?: Record<string, string>;
headInject?(root: string): string;
markdownRenderer(
md: string,
titleOnly: string,
titleOnly: boolean,
filePath: ShortPath | undefined,
anchorizer: (content: string, depthLevel: number) => string,
): string | undefined;
Expand All @@ -230,16 +230,17 @@ export async function generateHtml(
options.usageComposer.compose,
options.rewriteMap,
options.categoryDocs,
options.disableSearch,
options.disableSearch ?? false,
options.symbolRedirectMap,
options.defaultRedirectMap,
options.hrefResolver.resolvePath,
options.hrefResolver.resolveGlobalSymbol,
options.hrefResolver.resolveImportHref,
options.hrefResolver.resolveSource,
options.hrefResolver.resolveExternalJsdocModule,
options.hrefResolver?.resolvePath,
options.hrefResolver?.resolveGlobalSymbol || (() => undefined),
options.hrefResolver?.resolveImportHref || (() => undefined),
options.hrefResolver?.resolveSource || (() => undefined),
options.hrefResolver?.resolveExternalJsdocModule || (() => undefined),
options.markdownRenderer,
options.markdownStripper,
options.headInject,
docNodesByUrl,
);
}
3 changes: 3 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ name = "deno_doc_wasm"
path = "lib.rs"
crate-type = ["cdylib"]

[profile.release]
debug = true

[dependencies]
anyhow = "1.0.86"
deno_graph = { workspace = true }
Expand Down
74 changes: 49 additions & 25 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use anyhow::anyhow;
use anyhow::Context;
use deno_doc::html::{
DocNodeWithContext, UrlResolveKind, UsageComposerEntry, UsageToMd,
};
use deno_doc::html::UrlResolveKind;
use deno_doc::html::UsageComposerEntry;
use deno_doc::html::UsageToMd;
use deno_doc::DocParser;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadFuture;
Expand Down Expand Up @@ -264,14 +264,15 @@ pub fn generate_html(
symbol_redirect_map: JsValue,
default_symbol_map: JsValue,

resolve_path: js_sys::Function,
resolve_path: Option<js_sys::Function>,
resolve_global_symbol: js_sys::Function,
resolve_import_href: js_sys::Function,
resolve_source: js_sys::Function,
resolve_external_jsdoc_module: js_sys::Function,

markdown_renderer: js_sys::Function,
markdown_stripper: js_sys::Function,
head_inject: Option<js_sys::Function>,

doc_nodes_by_url: JsValue,
) -> Result<JsValue, JsValue> {
Expand All @@ -294,13 +295,14 @@ pub fn generate_html(
resolve_external_jsdoc_module,
markdown_renderer,
markdown_stripper,
head_inject,
doc_nodes_by_url,
)
.map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))
}

struct JsHrefResolver {
resolve_path: js_sys::Function,
resolve_path: Option<js_sys::Function>,
resolve_global_symbol: js_sys::Function,
resolve_import_href: js_sys::Function,
resolve_source: js_sys::Function,
Expand All @@ -313,18 +315,21 @@ impl deno_doc::html::HrefResolver for JsHrefResolver {
current: UrlResolveKind,
target: UrlResolveKind,
) -> String {
let this = JsValue::null();
if let Some(resolve_path) = &self.resolve_path {
let this = JsValue::null();

let current = serde_wasm_bindgen::to_value(&current).unwrap();
let target = serde_wasm_bindgen::to_value(&target).unwrap();
let current = serde_wasm_bindgen::to_value(&current).unwrap();
let target = serde_wasm_bindgen::to_value(&target).unwrap();

let global_symbol = self
.resolve_path
.call2(&this, &current, &target)
.expect("resolve_path errored");
let global_symbol = resolve_path
.call2(&this, &current, &target)
.expect("resolve_path errored");

serde_wasm_bindgen::from_value(global_symbol)
.expect("resolve_path returned an invalid value")
serde_wasm_bindgen::from_value(global_symbol)
.expect("resolve_path returned an invalid value")
} else {
deno_doc::html::href_path_resolve(current, target)
}
}

fn resolve_global_symbol(&self, symbol: &[String]) -> Option<String> {
Expand Down Expand Up @@ -406,19 +411,17 @@ impl deno_doc::html::UsageComposer for JsUsageComposer {

fn compose(
&self,
doc_nodes: &[DocNodeWithContext],
current_resolve: UrlResolveKind,
usage_to_md: UsageToMd,
) -> IndexMap<UsageComposerEntry, String> {
let this = JsValue::null();

let doc_nodes = serde_wasm_bindgen::to_value(&doc_nodes).unwrap();
let current_resolve =
serde_wasm_bindgen::to_value(&current_resolve).unwrap();

let global_symbol = self
.compose
.call3(&this, &doc_nodes, &current_resolve, &usage_to_md)
.call2(&this, &current_resolve, &usage_to_md)
.expect("compose errored");

serde_wasm_bindgen::from_value(global_symbol)
Expand All @@ -440,44 +443,46 @@ fn generate_html_inner(
symbol_redirect_map: JsValue,
default_symbol_map: JsValue,

resolve_path: js_sys::Function,
resolve_path: Option<js_sys::Function>,
resolve_global_symbol: js_sys::Function,
resolve_import_href: js_sys::Function,
resolve_source: js_sys::Function,
resolve_external_jsdoc_module: js_sys::Function,

markdown_renderer: js_sys::Function,
markdown_stripper: js_sys::Function,
head_inject: Option<js_sys::Function>,

doc_nodes_by_url: JsValue,
) -> Result<JsValue, anyhow::Error> {
let main_entrypoint = main_entrypoint
.map(|s| ModuleSpecifier::parse(&s))
.transpose()?;
.transpose()
.map_err(|e| anyhow::Error::from(e).context("mainEntrypoint"))?;

let rewrite_map = serde_wasm_bindgen::from_value::<
Option<IndexMap<ModuleSpecifier, String>>,
>(rewrite_map)
.map_err(|err| anyhow!("{}", err))?;
.map_err(|err| anyhow!("rewriteMap: {}", err))?;

let category_docs = serde_wasm_bindgen::from_value::<
Option<IndexMap<String, Option<String>>>,
>(category_docs)
.map_err(|err| anyhow!("{}", err))?;
.map_err(|err| anyhow!("categoryDocs: {}", err))?;

let symbol_redirect_map = serde_wasm_bindgen::from_value::<
Option<IndexMap<String, IndexMap<String, String>>>,
>(symbol_redirect_map)
.map_err(|err| anyhow!("{}", err))?;
.map_err(|err| anyhow!("symbolRedirectMap: {}", err))?;

let default_symbol_map = serde_wasm_bindgen::from_value::<
Option<IndexMap<String, String>>,
>(default_symbol_map)
.map_err(|err| anyhow!("{}", err))?;
.map_err(|err| anyhow!("defaultSymbolMap: {}", err))?;

let doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<deno_doc::DocNode>> =
serde_wasm_bindgen::from_value(doc_nodes_by_url)
.map_err(|err| anyhow!("{}", err))?;
.map_err(|err| anyhow!("docNodesByUrl: {}", err))?;

let markdown_renderer = Rc::new(
move |md: &str,
Expand Down Expand Up @@ -513,6 +518,25 @@ fn generate_html_inner(
.expect("markdown_stripper returned an invalid value")
});

let head_inject: Option<Rc<dyn Fn(&str) -> String + 'static>> =
if let Some(head_inject) = head_inject {
let head_inject = Rc::new(move |root: &str| {
let this = JsValue::null();
let root = serde_wasm_bindgen::to_value(root).unwrap();

let inject = head_inject
.call1(&this, &root)
.expect("head_inject errored");

serde_wasm_bindgen::from_value::<String>(inject)
.expect("head_inject returned an invalid value")
});

Some(head_inject)
} else {
None
};

let files = deno_doc::html::generate(
deno_doc::html::GenerateOptions {
package_name,
Expand All @@ -535,7 +559,7 @@ fn generate_html_inner(
default_symbol_map,
markdown_renderer,
markdown_stripper,
head_inject: None,
head_inject,
},
doc_nodes_by_url,
)?;
Expand Down
11 changes: 5 additions & 6 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::js_doc::JsDocTag;
use crate::DocNodeKind;
use serde::Serialize;
use std::borrow::Cow;
use std::rc::Rc;

lazy_static! {
static ref JSDOC_LINK_RE: regex::Regex = regex::Regex::new(
Expand Down Expand Up @@ -159,9 +160,8 @@ pub type Anchorizer<'a> = &'a js_sys::Function;
pub type Anchorizer =
std::sync::Arc<dyn Fn(String, u8) -> String + Send + Sync>;

pub type MarkdownRenderer = std::rc::Rc<
dyn (Fn(&str, bool, Option<ShortPath>, Anchorizer) -> Option<String>),
>;
pub type MarkdownRenderer =
Rc<dyn (Fn(&str, bool, Option<ShortPath>, Anchorizer) -> Option<String>)>;

pub fn markdown_to_html(
render_ctx: &RenderContext,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn markdown_to_html(
Box::new(anchorizer) as Box<dyn Fn(String, u8) -> String>
);
#[cfg(target_arch = "wasm32")]
let anchorizer = &wasm_bindgen::JsCast::unchecked_ref::<js_sys::Function>(
let anchorizer = wasm_bindgen::JsCast::unchecked_ref::<js_sys::Function>(
anchorizer.as_ref(),
);

Expand Down Expand Up @@ -454,7 +454,6 @@ mod test {

fn compose(
&self,
nodes: &[DocNodeWithContext],
current_resolve: UrlResolveKind,
usage_to_md: UsageToMd,
) -> IndexMap<UsageComposerEntry, String> {
Expand All @@ -466,7 +465,7 @@ mod test {
name: "".to_string(),
icon: None,
},
usage_to_md(nodes, current_file.display_name(), None),
usage_to_md(current_file.display_name(), None),
)])
})
.unwrap_or_default()
Expand Down
3 changes: 1 addition & 2 deletions src/html/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ mod test {

fn compose(
&self,
doc_nodes: &[DocNodeWithContext],
current_resolve: UrlResolveKind,
usage_to_md: UsageToMd,
) -> IndexMap<UsageComposerEntry, String> {
Expand All @@ -555,7 +554,7 @@ mod test {
name: "".to_string(),
icon: None,
},
usage_to_md(doc_nodes, current_file.specifier.as_str(), None),
usage_to_md(current_file.specifier.as_str(), None),
)])
})
.unwrap_or_default()
Expand Down
Loading

0 comments on commit 60d5f68

Please sign in to comment.