Skip to content

Commit 1a0695b

Browse files
committed
feat(html): no href for page-not-found
And some fixes for links.
1 parent d66b941 commit 1a0695b

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

crates/rari-doc/src/html/rewriter.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rari_md::ext::DELIM_START;
77
use rari_md::node_card::NoteCard;
88
use rari_types::fm_types::PageType;
99
use rari_types::globals::settings;
10-
use rari_types::locale::Locale;
1110
use rari_utils::concat_strs;
1211
use tracing::warn;
1312
use url::Url;
@@ -164,9 +163,13 @@ pub fn post_process_html<T: PageLike>(
164163
.strip_prefix("https://developer.mozilla.org")
165164
.map(|href| if href.is_empty() { "/" } else { href })
166165
.unwrap_or(&original_href);
166+
let href_no_hash = &href[..href.find('#').unwrap_or(href.len())];
167167
let no_locale = strip_locale_from_url(href).0.is_none();
168+
if no_locale && Page::ignore_link_check(href_no_hash) {
169+
return Ok(());
170+
}
168171
let maybe_prefixed_href = if no_locale {
169-
Cow::Owned(concat_strs!("/", Locale::default().as_url_str(), href))
172+
Cow::Owned(concat_strs!("/", page.locale().as_url_str(), href))
170173
} else {
171174
Cow::Borrowed(href)
172175
};
@@ -196,6 +199,11 @@ pub fn post_process_html<T: PageLike>(
196199
} else {
197200
false
198201
};
202+
let resolved_href = if no_locale {
203+
strip_locale_from_url(&resolved_href).1
204+
} else {
205+
resolved_href.as_ref()
206+
};
199207
if original_href != resolved_href {
200208
if let Some(pos) = el.get_attribute("data-sourcepos") {
201209
if let Some((start, _)) = pos.split_once('-') {
@@ -214,7 +222,7 @@ pub fn post_process_html<T: PageLike>(
214222
line = line,
215223
col = col,
216224
url = original_href,
217-
redirect = resolved_href.as_ref()
225+
redirect = resolved_href
218226
);
219227
if data_issues {
220228
el.set_attribute("data-flaw", &ic.to_string())?;
@@ -227,24 +235,19 @@ pub fn post_process_html<T: PageLike>(
227235
source = "redirected-link",
228236
ic = ic,
229237
url = original_href,
230-
redirect = resolved_href.as_ref()
238+
redirect = resolved_href
231239
);
232240
if data_issues {
233241
el.set_attribute("data-flaw", &ic.to_string())?;
234242
}
235243
}
244+
245+
if !remove_href {
246+
el.set_attribute("href", resolved_href)?;
247+
}
236248
}
237249
if remove_href {
238250
el.remove_attribute("href");
239-
} else {
240-
el.set_attribute(
241-
"href",
242-
if no_locale {
243-
strip_locale_from_url(&resolved_href).1
244-
} else {
245-
&resolved_href
246-
},
247-
)?;
248251
}
249252
} else if original_href.starts_with("http:") || original_href.starts_with("https:") {
250253
let class = el.get_attribute("class").unwrap_or_default();

crates/rari-doc/src/pages/page.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use std::sync::Arc;
33

44
use enum_dispatch::enum_dispatch;
55
use rari_types::fm_types::{FeatureStatus, PageType};
6-
use rari_types::globals::blog_root;
6+
use rari_types::globals::{blog_root, curriculum_root};
77
use rari_types::locale::Locale;
88
use rari_types::RariEnv;
9+
use rari_utils::concat_strs;
910

1011
use super::json::BuiltPage;
1112
use super::types::contributors::contributor_spotlight_from_url;
@@ -169,6 +170,9 @@ impl Page {
169170

170171
/// Checks if a `Page` for a given URL exists.
171172
///
173+
/// Checks for non SPAs owned by the front-end and then calls [Page::from_url_with_fallback].
174+
/// Also checks if there's no locale and in that case returns wether the page exists for the default locale.
175+
///
172176
/// # Arguments
173177
///
174178
/// * `url` - A string slice that holds the URL to be checked.
@@ -177,23 +181,25 @@ impl Page {
177181
///
178182
/// * `bool` - Returns `true` if the `Page` for the given URL exists, otherwise `false`.
179183
pub fn exists(url: &str) -> bool {
180-
if url == "/discord" {
181-
return true;
182-
}
183-
if url.starts_with("/users/") {
184-
return true;
185-
}
186184
if url.starts_with("/en-US/blog") && blog_root().is_none() {
187185
return true;
188186
}
189-
if url.starts_with("/en-US/curriculum") {
187+
if url.starts_with("/en-US/curriculum") && curriculum_root().is_none() {
190188
return true;
191189
}
192190
if strip_locale_from_url(url).1 == "/" {
193191
return true;
194192
}
195193

196-
Page::from_url_with_fallback(url).is_ok()
194+
if Page::from_url_with_fallback(url).is_ok() {
195+
return true;
196+
}
197+
198+
if let (None, url) = strip_locale_from_url(url) {
199+
let url_with_default_locale = concat_strs!("/", Locale::default().as_url_str(), url);
200+
return Self::exists(&url_with_default_locale);
201+
}
202+
false
197203
}
198204
}
199205

crates/rari-doc/src/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn strip_locale_from_url(url: &str) -> (Option<Locale>, &str) {
7474
}
7575
let i = url[1..].find('/').map(|i| i + 1).unwrap_or(url.len());
7676
let locale = Locale::from_str(&url[1..i]).ok();
77-
(locale, &url[i..])
77+
(locale, &url[if locale.is_none() { 0 } else { i }..])
7878
}
7979

8080
/// Represents metadata extracted from a URL.

rari-npm/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)