Skip to content

Commit

Permalink
feat(issues): improve flaw compatibility (#95)
Browse files Browse the repository at this point in the history
* feat(issues): improve flaw compatibility

* fix and refactor
  • Loading branch information
fiji-flo authored Jan 24, 2025
1 parent 70a86a0 commit 8b1f018
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 182 deletions.
2 changes: 2 additions & 0 deletions crates/rari-doc/src/helpers/subpages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn write_li_with_badges(
code,
only_en_us: locale_page.locale() != locale,
},
true,
)?;
if closed {
write!(out, "</li>")?;
Expand All @@ -106,6 +107,7 @@ pub fn write_parent_li(out: &mut String, page: &Page, locale: Locale) -> Result<
code: false,
only_en_us: page.locale() != locale,
},
true,
)?;
out.push_str("</li>");
Ok(())
Expand Down
100 changes: 60 additions & 40 deletions crates/rari-doc/src/html/fix_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ pub fn check_and_fix_link(
page: &impl PageLike,
data_issues: bool,
) -> HandlerResult {
let templ_link = el.has_attribute("data-templ-link");
if templ_link {
el.remove_attribute("data-templ-link");
}
let original_href = el.get_attribute("href").expect("href was required");

if original_href.starts_with('/') || original_href.starts_with("https://developer.mozilla.org")
{
handle_internal_link(&original_href, el, page, data_issues)
handle_internal_link(&original_href, el, page, data_issues, templ_link)
} else if original_href.starts_with("http:") || original_href.starts_with("https:") {
handle_external_link(el)
} else {
Expand All @@ -48,6 +52,7 @@ pub fn handle_internal_link(
el: &mut Element,
page: &impl PageLike,
data_issues: bool,
templ_link: bool,
) -> HandlerResult {
// Strip prefix for curriculum links.
let original_href = if page.page_type() == PageType::Curriculum {
Expand Down Expand Up @@ -130,56 +135,71 @@ pub fn handle_internal_link(
}
}

let resolved_href = if no_locale {
strip_locale_from_url(&resolved_href).1
} else {
resolved_href.as_ref()
};
if original_href != resolved_href {
if !en_us_fallback {
if let Some(pos) = el.get_attribute("data-sourcepos") {
if let Some((start, _)) = pos.split_once('-') {
if let Some((line, col)) = start.split_once(':') {
let line = line
.parse::<i64>()
.map(|l| l + i64::try_from(page.fm_offset()).unwrap_or(l - 1))
.ok()
.unwrap_or(-1);
let col = col.parse::<i64>().ok().unwrap_or(0);
let ic = get_issue_counter();
if !templ_link {
let resolved_href = if no_locale {
strip_locale_from_url(&resolved_href).1
} else {
resolved_href.as_ref()
};
if original_href != resolved_href || remove_href {
if !en_us_fallback {
if let Some(pos) = el.get_attribute("data-sourcepos") {
if let Some((start, _)) = pos.split_once('-') {
if let Some((line, col)) = start.split_once(':') {
let line = line
.parse::<i64>()
.map(|l| l + i64::try_from(page.fm_offset()).unwrap_or(l - 1))
.ok()
.unwrap_or(-1);
let col = col.parse::<i64>().ok().unwrap_or(0);
let ic = get_issue_counter();
if remove_href {
tracing::warn!(
source = "broken-link",
ic = ic,
line = line,
col = col,
url = original_href,
);
} else {
tracing::warn!(
source = "redirected-link",
ic = ic,
line = line,
col = col,
url = original_href,
redirect = resolved_href
);
}
if data_issues {
el.set_attribute("data-flaw", &ic.to_string())?;
}
}
}
} else {
let ic = get_issue_counter();
if remove_href {
tracing::warn!(source = "broken-link", ic = ic, url = original_href);
} else {
tracing::warn!(
source = "redirected-link",
ic = ic,
line = line,
col = col,
url = original_href,
redirect = resolved_href
);
if data_issues {
el.set_attribute("data-flaw", &ic.to_string())?;
}
}
}
} else {
let ic = get_issue_counter();
tracing::warn!(
source = "redirected-link",
ic = ic,
url = original_href,
redirect = resolved_href
);
if data_issues {
el.set_attribute("data-flaw", &ic.to_string())?;
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", resolved_href)?;
}
}
}
if remove_href {
el.remove_attribute("href");
}
Ok(())
}
91 changes: 44 additions & 47 deletions crates/rari-doc/src/html/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rari_md::anchor::anchorize;
use rari_types::fm_types::FeatureStatus;
use rari_types::locale::Locale;
use rari_utils::concat_strs;
use tracing::warn;

use crate::error::DocError;
use crate::pages::page::{Page, PageLike};
Expand All @@ -26,21 +25,29 @@ pub fn render_internal_link(
content: &str,
title: Option<&str>,
modifier: &LinkModifier,
checked: bool,
) -> Result<(), DocError> {
out.push_str("<a href=\"");
out.push_str(url);
if let Some(anchor) = anchor {
out.push('#');
out.push_str(&anchorize(anchor));
}
out.push('"');
if let Some(title) = title {
out.push_str("\" title=\"");
out.push_str(&html_escape::encode_quoted_attribute(title));
out.extend([
" title=\"",
&html_escape::encode_quoted_attribute(title),
"\"",
]);
}
if modifier.only_en_us {
out.push_str("\" class=\"only-in-en-us")
out.push_str(" class=\"only-in-en-us\"");
}
out.push_str("\">");
if checked {
out.push_str(" data-templ-link");
}
out.push('>');
if modifier.code {
out.push_str("<code>");
}
Expand Down Expand Up @@ -76,7 +83,7 @@ pub fn render_link_from_page(
} else {
Cow::Borrowed(content)
};
render_internal_link(out, page.url(), None, &content, None, modifier)
render_internal_link(out, page.url(), None, &content, None, modifier, true)
}

pub fn render_link_via_page(
Expand All @@ -94,52 +101,42 @@ 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, ""));
match RariApi::get_page(url) {
Ok(page) => {
let url = page.url();
let content = if let Some(content) = content {
Cow::Borrowed(content)
if let Ok(page) = RariApi::get_page(url) {
let url = page.url();
let content = if let Some(content) = content {
Cow::Borrowed(content)
} else {
let content = page.short_title().unwrap_or(page.title());
let decoded_content = html_escape::decode_html_entities(content);
let encoded_content = html_escape::encode_safe(&decoded_content);
if content != encoded_content {
Cow::Owned(encoded_content.into_owned())
} else {
let content = page.short_title().unwrap_or(page.title());
let decoded_content = html_escape::decode_html_entities(content);
let encoded_content = html_escape::encode_safe(&decoded_content);
if content != encoded_content {
Cow::Owned(encoded_content.into_owned())
} else {
Cow::Borrowed(content)
}
};
return render_internal_link(
out,
url,
if anchor.is_empty() {
None
} else {
Some(anchor)
},
&content,
title,
&LinkModifier {
badges: if with_badges { page.status() } else { &[] },
badge_locale: locale,
code,
only_en_us: page.locale() == Locale::EnUs && locale != Locale::EnUs,
},
);
}
Err(e) => {
if !Page::ignore_link_check(url) {
warn!(
source = "invalid-link",
url = url,
"Link via page not found: {e}"
)
Cow::Borrowed(content)
}
}
};
return render_internal_link(
out,
url,
if anchor.is_empty() {
None
} else {
Some(anchor)
},
&content,
title,
&LinkModifier {
badges: if with_badges { page.status() } else { &[] },
badge_locale: locale,
code,
only_en_us: page.locale() == Locale::EnUs && locale != Locale::EnUs,
},
true,
);
}
}

out.push_str("<a href=\"");
out.push_str("<a data-templ-link href=\"");
let content = match content {
Some(content) => {
let decoded_content = html_escape::decode_html_entities(content);
Expand Down
3 changes: 2 additions & 1 deletion crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ pub fn post_process_html<T: PageLike>(
Ok(())
}),
element!("a[href]", |el| {
check_and_fix_link(el, page, data_issues)
check_and_fix_link(el, page, data_issues)?;
Ok(())
}),
element!("pre:not(.notranslate)", |el| {
let mut class = el.get_attribute("class").unwrap_or_default();
Expand Down
Loading

0 comments on commit 8b1f018

Please sign in to comment.