Skip to content

Commit

Permalink
actually generate search index
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 26, 2023
1 parent fb7254b commit f355a95
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn generate_docs_directory(
.unwrap();
std::fs::write(
path.join(deno_doc::html::SEARCH_INDEX_FILENAME),
include_str!("./search_index.js"),
deno_doc::html::generate_search_index(doc_nodes)?,
)
.unwrap();
std::fs::write(
Expand Down
11 changes: 0 additions & 11 deletions examples/ddoc/search_index.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ impl GenerateCtx {
}
}

pub fn generate_search_index(
doc_nodes: &[crate::DocNode],
) -> Result<String, anyhow::Error> {
let mut search_index = serde_json::json!({
"nodes": doc_nodes
});

for el in search_index
.get_mut("nodes")
.unwrap()
.as_array_mut()
.unwrap()
{
el["jsDoc"].take();
match el.get("kind").unwrap().as_str().unwrap() {
"function" => el["functionDef"].take(),
"variable" => el["variableDef"].take(),
"enum" => el["enumDef"].take(),
"class" => el["classDef"].take(),
"typeAlias" => el["typeAliasDef"].take(),
"namespace" => el["namespaceDef"].take(),
"interface" => el["interfaceDef"].take(),
_ => serde_json::Value::Null,
};
}

let search_index_str = serde_json::to_string(&search_index)?;

let index = format!(
r#"(function () {{
window.DENO_DOC_SEARCH_INDEX = {};
}})()"#,
search_index_str
);
Ok(index)
}

pub fn generate(
ctx: GenerateCtx,
doc_nodes: &[crate::DocNode],
Expand Down

0 comments on commit f355a95

Please sign in to comment.