Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(templ): don't report ill cased as broken in templs #159

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions crates/rari-doc/src/html/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rari_types::locale::Locale;
use rari_utils::concat_strs;

use crate::error::DocError;
use crate::issues::get_issue_counter;
use crate::pages::page::{Page, PageLike};
use crate::resolve::locale_from_url;
use crate::templ::api::RariApi;
Expand Down Expand Up @@ -112,16 +111,11 @@ pub fn render_link_via_page(
url = Cow::Owned(concat_strs!("/", locale.as_url_str(), "/docs/", link));
}
let (url, anchor) = url.split_once('#').unwrap_or((&url, ""));
if let Ok(page) = RariApi::get_page(url) {
if report && url != page.url() && url.to_lowercase() == page.url().to_lowercase() {
let ic = get_issue_counter();
tracing::warn!(
source = "ill-cased-link",
ic = ic,
url = url,
redirect = page.url()
);
}
if let Ok(page) = if report {
RariApi::get_page(url)
} else {
RariApi::get_page_ignore_case(url)
} {
let url = page.url();
let content = if let Some(content) = content {
Cow::Borrowed(content)
Expand Down
49 changes: 37 additions & 12 deletions crates/rari-doc/src/templ/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use crate::pages::page::Page;
use crate::percent::PATH_SEGMENT;
use crate::redirects::resolve_redirect;

enum LinkWarn {
No,
All,
IgnoreCase,
}

pub struct RariApi {}
impl RariApi {
pub fn anchorize(content: &str) -> Cow<'_, str> {
Expand All @@ -22,25 +28,44 @@ impl RariApi {
&settings().live_samples_base_url
}
pub fn get_page_nowarn(url: &str) -> Result<Page, DocError> {
RariApi::get_page_internal(url, false)
RariApi::get_page_internal(url, LinkWarn::No)
}

pub fn get_page_ignore_case(url: &str) -> Result<Page, DocError> {
RariApi::get_page_internal(url, LinkWarn::IgnoreCase)
}

pub fn get_page(url: &str) -> Result<Page, DocError> {
RariApi::get_page_internal(url, true)
RariApi::get_page_internal(url, LinkWarn::All)
}

fn get_page_internal(url: &str, warn: bool) -> Result<Page, DocError> {
fn get_page_internal(url: &str, warn: LinkWarn) -> Result<Page, DocError> {
let redirect = resolve_redirect(url);
let url = match redirect.as_ref() {
Some(redirect) => {
if warn {
let ic = get_issue_counter();
tracing::warn!(
source = "templ-redirected-link",
ic = ic,
url = url,
href = redirect.as_ref()
);
if !matches!(warn, LinkWarn::No) {
let ill_cased = url.to_lowercase() == redirect.to_lowercase();
match warn {
LinkWarn::All | LinkWarn::IgnoreCase if !ill_cased => {
let ic = get_issue_counter();
tracing::warn!(
source = "templ-redirected-link",
ic = ic,
url = url,
href = redirect.as_ref()
);
}
LinkWarn::All if ill_cased => {
let ic = get_issue_counter();
tracing::warn!(
source = "ill-cased-link",
ic = ic,
url = url,
redirect = redirect.as_ref()
);
}
_ => {}
}
}
if deny_warnings() {
return Err(DocError::RedirectedLink {
Expand All @@ -55,7 +80,7 @@ impl RariApi {
};
Page::from_url_with_fallback(url).map_err(|e| {
if let DocError::PageNotFound(_, _) = e {
if warn {
if !matches!(warn, LinkWarn::No) {
let ic = get_issue_counter();
tracing::warn!(source = "templ-broken-link", ic = ic, url = url);
}
Expand Down
Loading