From e547623438b2274a1a6c29d7ca4916b28a1a43f4 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 16 Dec 2024 13:06:26 +0100 Subject: [PATCH] fix(links): unify link code --- crates/rari-doc/src/helpers/subpages.rs | 58 ++++++++++--------- .../templ/templs/list_subpages_for_sidebar.rs | 22 ++++--- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/crates/rari-doc/src/helpers/subpages.rs b/crates/rari-doc/src/helpers/subpages.rs index 9dde7eb5..37ad3ec1 100644 --- a/crates/rari-doc/src/helpers/subpages.rs +++ b/crates/rari-doc/src/helpers/subpages.rs @@ -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; @@ -71,41 +71,43 @@ pub fn write_li_with_badges( } else { page }; - out.extend([ - "
  • ", - r#""); + render_internal_link( + out, locale_page.url(), - r#"">"#, - if code { "" } else { "" }, + None, &html_escape::encode_safe(locale_page.short_title().unwrap_or(locale_page.title())), - if code { "" } else { "" }, - "", - ]); - 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, "
  • ")?; } 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, "
  • {}", page.url(), title)?; - add_inline_badges(out, page, locale)?; - write!(out, "
  • ")?; + let content = l10n_json_data("Template", "overview", locale)?; + out.push_str("
  • "); + 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("
  • "); Ok(()) } diff --git a/crates/rari-doc/src/templ/templs/list_subpages_for_sidebar.rs b/crates/rari-doc/src/templ/templs/list_subpages_for_sidebar.rs index 2607f233..2da5d9bf 100644 --- a/crates/rari-doc/src/templ/templs/list_subpages_for_sidebar.rs +++ b/crates/rari-doc/src/templ/templs/list_subpages_for_sidebar.rs @@ -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}; @@ -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#"
  • "#, - if code { "" } else { "" }, + None, &html_escape::encode_safe(title), - if code { "" } else { "" }, - r#""#, - ]); - 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("
  • "); } out.push_str("");