Skip to content

Commit

Permalink
fix(html): trim the first empty line in <pre> tag (#90)
Browse files Browse the repository at this point in the history
* fix(html): trim the first empty line in `<pre>` tag

* only trim start (#2)

---------

Co-authored-by: Florian Dieminger <[email protected]>
  • Loading branch information
yin1999 and fiji-flo authored Jan 22, 2025
1 parent 4f5751f commit 95f142f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::collections::HashSet;

use lol_html::html_content::ContentType;
use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings};
use lol_html::{element, rewrite_str, text, HtmlRewriter, RewriteStrSettings, Settings};
use rari_md::ext::DELIM_START;
use rari_md::node_card::NoteCard;
use rari_types::fm_types::PageType;
Expand Down Expand Up @@ -48,6 +48,7 @@ pub fn post_process_html<T: PageLike>(
))?;
let base_url = options.base_url(Some(&base));
let data_issues = settings().data_issues;
let mut in_pre = false;

let mut element_content_handlers = vec![
element!("*[id]", |el| {
Expand Down Expand Up @@ -106,6 +107,18 @@ pub fn post_process_html<T: PageLike>(
el.set_attribute("class", &class)?;
Ok(())
}),
text!("pre[class*=brush]", |text| {
// trim the first _empty_ line,
// fixes issue: https://github.com/mdn/yari/issues/12364
if !in_pre && text.as_str().starts_with('\n') {
text.as_mut_str().remove(0);
}
in_pre = true;
if text.last_in_text_node() {
in_pre = false;
}
Ok(())
}),
element!("pre[class*=brush]", |el| {
let class = el.get_attribute("class");
let class = class.as_deref().unwrap_or_default();
Expand Down

0 comments on commit 95f142f

Please sign in to comment.