Skip to content

Commit

Permalink
fix(templ): corrects ids from templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 10, 2024
1 parent ac5d606 commit d4398c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 4 additions & 1 deletion crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashSet;

use lol_html::html_content::ContentType;
use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings};
use rari_md::ext::DELIM_START;
use rari_md::node_card::NoteCard;
use rari_types::fm_types::PageType;
use rari_types::locale::Locale;
Expand Down Expand Up @@ -53,7 +54,9 @@ pub fn post_process_html<T: PageLike>(
let mut element_content_handlers = vec![
element!("*[id]", |el| {
if let Some(id) = el.get_attribute("id") {
if ids.contains(id.as_str()) {
if id.contains(DELIM_START) {
el.set_attribute("data-update-id", "")?;
} else if ids.contains(id.as_str()) {
let (prefix, mut count) = if let Some((prefix, counter)) = id.rsplit_once('_') {
if counter.chars().all(|c| c.is_ascii_digit()) {
let count = counter.parse::<i64>().unwrap_or_default() + 1;
Expand Down
13 changes: 8 additions & 5 deletions crates/rari-doc/src/templ/templs/links/mathmlxref.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use rari_templ_func::rari_f;
use rari_utils::concat_strs;

use crate::error::DocError;
use crate::templ::api::RariApi;

#[rari_f]
pub fn mathmlxref(element_name: String) -> Result<String, DocError> {
let element_name = element_name.to_lowercase();
let display = format!("&lt;{element_name}&gt;");
let url = format!(
"/{}/docs/Web/MathML/Element/{}",
let display = concat_strs!("&lt;", element_name.as_str(), "&gt;");
let title = concat_strs!("<", element_name.as_str(), ">");
let url = concat_strs!(
"/",
env.locale.as_url_str(),
element_name,
"/docs/Web/MathML/Element/",
element_name.as_str()
);

RariApi::link(&url, None, Some(&display), true, Some(&display), false)
RariApi::link(&url, None, Some(&display), true, Some(&title), false)
}
21 changes: 13 additions & 8 deletions crates/rari-md/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl Anchorizer {
///
/// assert_eq!("ticks-arent-in".to_string(), anchorizer.anchorize(source.to_string()));
/// ```
pub fn anchorize(&mut self, header: String) -> String {
let id = anchor::anchorize(&header);
pub fn anchorize(&mut self, header: impl AsRef<str>) -> String {
let id = anchor::anchorize(header.as_ref());

let mut uniq = 0;
let id = loop {
Expand Down Expand Up @@ -597,12 +597,12 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
self.cr()?;
let mut text_content = Vec::with_capacity(20);
Self::collect_first_child_text(node, &mut text_content);
let raw_id = String::from_utf8(text_content).unwrap();
let is_templ = raw_id.starts_with(DELIM_START);
let raw_id = String::from_utf8_lossy(&text_content);
let is_templ = raw_id.contains(DELIM_START);
if is_templ {
write!(self.output, "<dt data-update-id")?;
} else {
let id = self.anchorizer.anchorize(raw_id);
let id = self.anchorizer.anchorize(&raw_id);
write!(self.output, "<dt id=\"{}\"", id)?;
};
if !is_templ && !Self::next_is_link(node) {
Expand Down Expand Up @@ -633,9 +633,14 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
let mut text_content = Vec::with_capacity(20);
Self::collect_text(node, &mut text_content);

let mut id = String::from_utf8(text_content).unwrap();
id = self.anchorizer.anchorize(id);
write!(self.output, " id=\"{}\"", id)?;
let raw_id = String::from_utf8(text_content).unwrap();
let is_templ = raw_id.contains(DELIM_START);
if is_templ {
write!(self.output, " data-update-id")?;
} else {
let id = self.anchorizer.anchorize(&raw_id);
write!(self.output, " id=\"{}\"", id)?;
};
}
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
Expand Down

0 comments on commit d4398c8

Please sign in to comment.