Skip to content

Commit

Permalink
feat(templ): webextallcompat
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Aug 10, 2024
1 parent 5c02fde commit fc799b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
56 changes: 51 additions & 5 deletions crates/rari-doc/src/templ/templs/compat.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
use rari_templ_func::rari_f;
use rari_types::fm_types::PageType;

use crate::docs::page::{Page, PageLike};
use crate::error::DocError;
use crate::helpers::subpages::{get_sub_pages, SubPagesSorter};

#[rari_f]
pub fn compat() -> Result<String, DocError> {
let multiple = env.browser_compat.len() > 1;
Ok(env.browser_compat.iter().map(|query| format!(
r#"<div class="bc-data" data-query="{query}" data-depth="1" data-multiple="{multiple}">
If you're able to see this, something went wrong on this page.
</div>"#)).collect::<Vec<String>>().join("\n"))
Ok(compat_internal(env.browser_compat))
}

#[rari_f]
pub fn webextallcompattables() -> Result<String, DocError> {
let mut out = String::new();
let sub_pages = get_sub_pages(
"/en-US/docs/Mozilla/Add-ons/WebExtensions/API",
Some(1),
SubPagesSorter::default(),
)?;
for page in sub_pages.iter().filter_map(|page| {
if page.page_type() == PageType::WebextensionApi {
if let Page::Doc(doc) = page {
return Some(doc);
}
}
None
}) {
for feature_name in &page.meta.browser_compat {
out.extend([
"<h2>",
feature_name
.as_str()
.strip_prefix("webextensions.api.")
.unwrap_or(feature_name.as_str()),
"</h2>",
]);
out.push_str(&compat_internal(&[feature_name]));
}
}
Ok(out)
}

fn compat_internal(browser_compat: &[impl AsRef<str>]) -> String {
let multiple = browser_compat.len() > 1;
browser_compat
.iter()
.map(|query| {
format!(
r#"<div class="bc-data" data-query="{}" data-depth="1" data-multiple="{multiple}">
If you're able to see this, something went wrong on this page.
</div>"#,
query.as_ref()
)
})
.collect::<Vec<String>>()
.join("\n")
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions crates/rari-doc/src/templ/templs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub fn invoke(
"js_property_attributes" => js_property_attributes::js_property_attributes_any,
"svginfo" => svginfo::svginfo_any,
"xsltref" => xsltref::xsltref_any,
"webextallcompattables" => compat::webextallcompattables_any,

// hacky
"glossarydisambiguation" => glossarydisambiguation::glossarydisambiguation_any,
Expand Down Expand Up @@ -153,6 +154,9 @@ pub fn invoke(
"pwasidebar" => sidebars::pwasidebar_any,
"addonsidebarmain" => sidebars::addonsidebarmain_any,

// ignore
"xulelem" => return Ok((Default::default(), false)),

// unknown
_ if deny_warnings() => return Err(DocError::UnknownMacro(ident.to_string())),
_ => {
Expand Down

0 comments on commit fc799b8

Please sign in to comment.