Skip to content

Commit

Permalink
feat(html): no href for page-not-found
Browse files Browse the repository at this point in the history
And some fixes for links.
  • Loading branch information
fiji-flo committed Oct 29, 2024
1 parent d66b941 commit 1a0695b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
29 changes: 16 additions & 13 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use rari_md::ext::DELIM_START;
use rari_md::node_card::NoteCard;
use rari_types::fm_types::PageType;
use rari_types::globals::settings;
use rari_types::locale::Locale;
use rari_utils::concat_strs;
use tracing::warn;
use url::Url;
Expand Down Expand Up @@ -164,9 +163,13 @@ pub fn post_process_html<T: PageLike>(
.strip_prefix("https://developer.mozilla.org")
.map(|href| if href.is_empty() { "/" } else { href })
.unwrap_or(&original_href);
let href_no_hash = &href[..href.find('#').unwrap_or(href.len())];
let no_locale = strip_locale_from_url(href).0.is_none();
if no_locale && Page::ignore_link_check(href_no_hash) {
return Ok(());
}
let maybe_prefixed_href = if no_locale {
Cow::Owned(concat_strs!("/", Locale::default().as_url_str(), href))
Cow::Owned(concat_strs!("/", page.locale().as_url_str(), href))
} else {
Cow::Borrowed(href)
};
Expand Down Expand Up @@ -196,6 +199,11 @@ pub fn post_process_html<T: PageLike>(
} else {
false
};
let resolved_href = if no_locale {
strip_locale_from_url(&resolved_href).1
} else {
resolved_href.as_ref()
};
if original_href != resolved_href {
if let Some(pos) = el.get_attribute("data-sourcepos") {
if let Some((start, _)) = pos.split_once('-') {
Expand All @@ -214,7 +222,7 @@ pub fn post_process_html<T: PageLike>(
line = line,
col = col,
url = original_href,
redirect = resolved_href.as_ref()
redirect = resolved_href
);
if data_issues {
el.set_attribute("data-flaw", &ic.to_string())?;
Expand All @@ -227,24 +235,19 @@ pub fn post_process_html<T: PageLike>(
source = "redirected-link",
ic = ic,
url = original_href,
redirect = resolved_href.as_ref()
redirect = resolved_href
);
if data_issues {
el.set_attribute("data-flaw", &ic.to_string())?;
}
}

if !remove_href {
el.set_attribute("href", resolved_href)?;
}
}
if remove_href {
el.remove_attribute("href");
} else {
el.set_attribute(
"href",
if no_locale {
strip_locale_from_url(&resolved_href).1
} else {
&resolved_href
},
)?;
}
} else if original_href.starts_with("http:") || original_href.starts_with("https:") {
let class = el.get_attribute("class").unwrap_or_default();
Expand Down
24 changes: 15 additions & 9 deletions crates/rari-doc/src/pages/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::sync::Arc;

use enum_dispatch::enum_dispatch;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::globals::blog_root;
use rari_types::globals::{blog_root, curriculum_root};
use rari_types::locale::Locale;
use rari_types::RariEnv;
use rari_utils::concat_strs;

use super::json::BuiltPage;
use super::types::contributors::contributor_spotlight_from_url;
Expand Down Expand Up @@ -169,6 +170,9 @@ impl Page {

/// Checks if a `Page` for a given URL exists.
///
/// Checks for non SPAs owned by the front-end and then calls [Page::from_url_with_fallback].
/// Also checks if there's no locale and in that case returns wether the page exists for the default locale.
///
/// # Arguments
///
/// * `url` - A string slice that holds the URL to be checked.
Expand All @@ -177,23 +181,25 @@ impl Page {
///
/// * `bool` - Returns `true` if the `Page` for the given URL exists, otherwise `false`.
pub fn exists(url: &str) -> bool {
if url == "/discord" {
return true;
}
if url.starts_with("/users/") {
return true;
}
if url.starts_with("/en-US/blog") && blog_root().is_none() {
return true;
}
if url.starts_with("/en-US/curriculum") {
if url.starts_with("/en-US/curriculum") && curriculum_root().is_none() {
return true;
}
if strip_locale_from_url(url).1 == "/" {
return true;
}

Page::from_url_with_fallback(url).is_ok()
if Page::from_url_with_fallback(url).is_ok() {
return true;
}

if let (None, url) = strip_locale_from_url(url) {
let url_with_default_locale = concat_strs!("/", Locale::default().as_url_str(), url);
return Self::exists(&url_with_default_locale);
}
false
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn strip_locale_from_url(url: &str) -> (Option<Locale>, &str) {
}
let i = url[1..].find('/').map(|i| i + 1).unwrap_or(url.len());
let locale = Locale::from_str(&url[1..i]).ok();
(locale, &url[i..])
(locale, &url[if locale.is_none() { 0 } else { i }..])
}

/// Represents metadata extracted from a URL.
Expand Down
4 changes: 2 additions & 2 deletions rari-npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1a0695b

Please sign in to comment.