Skip to content

Commit

Permalink
fix(links): unify link code
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Dec 16, 2024
1 parent 717f770 commit e547623
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
58 changes: 30 additions & 28 deletions crates/rari-doc/src/helpers/subpages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::fmt::Write;
use std::path::PathBuf;

use memoize::memoize;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::fm_types::PageType;
use rari_types::globals::{cache_content, deny_warnings};
use rari_types::locale::Locale;

use super::l10n::l10n_json_data;
use super::titles::api_page_title;
use crate::error::DocError;
use crate::html::links::{render_internal_link, LinkModifier};
use crate::pages::page::{Page, PageLike, PageReader};
use crate::redirects::resolve_redirect;
use crate::templ::templs::badges::{write_deprecated, write_experimental, write_non_standard};
use crate::utils::COLLATOR;
use crate::walker::walk_builder;

Expand Down Expand Up @@ -71,41 +71,43 @@ pub fn write_li_with_badges(
} else {
page
};
out.extend([
"<li>",
r#"<a href=""#,
out.push_str("<li>");
render_internal_link(
out,
locale_page.url(),
r#"">"#,
if code { "<code>" } else { "" },
None,
&html_escape::encode_safe(locale_page.short_title().unwrap_or(locale_page.title())),
if code { "</code>" } else { "" },
"</a>",
]);
add_inline_badges(out, page, locale)?;
None,
&LinkModifier {
badges: page.status(),
badge_locale: locale,
code,
only_en_us: locale_page.locale() != locale,
},
)?;
if closed {
write!(out, "</li>")?;
}
Ok(())
}

pub fn add_inline_badges(out: &mut String, page: &Page, locale: Locale) -> Result<(), DocError> {
if page.status().contains(&FeatureStatus::Experimental) {
write_experimental(out, locale)?;
}
if page.status().contains(&FeatureStatus::NonStandard) {
write_non_standard(out, locale)?;
}
if page.status().contains(&FeatureStatus::Deprecated) {
write_deprecated(out, locale)?;
}
Ok(())
}

pub fn write_parent_li(out: &mut String, page: &Page, locale: Locale) -> Result<(), DocError> {
let title = l10n_json_data("Template", "overview", locale)?;
write!(out, "<li><a href=\"{}\">{}</a>", page.url(), title)?;
add_inline_badges(out, page, locale)?;
write!(out, "</li>")?;
let content = l10n_json_data("Template", "overview", locale)?;
out.push_str("<li>");
render_internal_link(
out,
page.url(),
None,
content,
None,
&LinkModifier {
badges: page.status(),
badge_locale: locale,
code: false,
only_en_us: page.locale() != locale,
},
)?;
out.push_str("</li>");
Ok(())
}

Expand Down
22 changes: 13 additions & 9 deletions crates/rari-doc/src/templ/templs/list_subpages_for_sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use rari_templ_func::rari_f;
use rari_types::AnyArg;

use crate::error::DocError;
use crate::helpers::subpages::{add_inline_badges, get_sub_pages, SubPagesSorter};
use crate::helpers::subpages::{get_sub_pages, SubPagesSorter};
use crate::html::links::{render_internal_link, LinkModifier};
use crate::pages::page::{Page, PageLike};
use crate::utils::{trim_after, trim_fefore};

Expand Down Expand Up @@ -37,16 +38,19 @@ pub fn list_subpages_for_sidebar(
let title = locale_page.short_title().unwrap_or(locale_page.title());
let title = trim_fefore(title, title_only_after.as_deref());
let title = trim_after(title, title_only_before.as_deref());
out.extend([
r#"<li><a href=""#,
render_internal_link(
&mut out,
locale_page.url(),
r#"">"#,
if code { "<code>" } else { "" },
None,
&html_escape::encode_safe(title),
if code { "</code>" } else { "" },
r#"</a>"#,
]);
add_inline_badges(&mut out, &page, env.locale)?;
None,
&LinkModifier {
badges: page.status(),
badge_locale: env.locale,
code,
only_en_us: locale_page.locale() != env.locale,
},
)?;
out.push_str("</li>");
}
out.push_str("</ol>");
Expand Down

0 comments on commit e547623

Please sign in to comment.