Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep inline HTML inline #112

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,20 @@ outside divs
│ , Para [ Str "outside" , Space , Str "divs" ]
│ ]
"###);

// Make sure logic doesn't trigger on inline html since inserting divs
// introduces newlines and breaks the original structure
let output = MDBook::init()
.config(Config::markdown())
.chapter(Chapter::new("Chapter", "2<sup>n - 1</sup>", "chapter.md"))
.build();
insta::assert_snapshot!(output, @r###"
├─ log output
│ INFO mdbook::book: Running the pandoc backend
│ INFO mdbook_pandoc::pandoc::renderer: Wrote output to book/markdown/book.md
├─ markdown/book.md
│ 2`<sup>`{=html}n - 1`</sup>`{=html}
"###);
}

#[test]
Expand Down
50 changes: 30 additions & 20 deletions src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ enum LinkContext {
Image,
}

enum HtmlContext {
Inline,
Block,
}

#[derive(Debug)]
struct UnresolvableRemoteImage {
err: ureq::Error,
Expand Down Expand Up @@ -1038,7 +1043,7 @@ impl<'book, 'preprocessor> PreprocessChapter<'book, 'preprocessor> {
// Actually consume the item from the iterator
self.parser.next();
}
for event in self.preprocess_contiguous_html(html, Event::Html) {
for event in self.preprocess_contiguous_html(html, HtmlContext::Block) {
co.yield_((event, None)).await
}
continue 'events;
Expand All @@ -1051,7 +1056,7 @@ impl<'book, 'preprocessor> PreprocessChapter<'book, 'preprocessor> {
// Actually consume the item from the iterator
self.parser.next();
}
for event in self.preprocess_contiguous_html(html, Event::InlineHtml) {
for event in self.preprocess_contiguous_html(html, HtmlContext::Inline) {
co.yield_((event, None)).await
}
continue 'events;
Expand Down Expand Up @@ -1125,8 +1130,10 @@ impl<'book, 'preprocessor> PreprocessChapter<'book, 'preprocessor> {
fn preprocess_contiguous_html(
&mut self,
mut html: CowStr<'book>,
wrap_html: impl FnOnce(CowStr<'book>) -> pulldown_cmark::Event,
ctx: HtmlContext,
) -> impl Iterator<Item = pulldown_cmark::Event<'book>> + '_ {
use pulldown_cmark::Event;

if let OutputFormat::Latex { packages } = &mut self.preprocessor.ctx.output {
static FONT_AWESOME_ICON: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"<i\s+class\s*=\s*"fa fa-(?P<icon>.*?)"(>\s*</i>|/>)"#).unwrap()
Expand All @@ -1147,24 +1154,25 @@ impl<'book, 'preprocessor> PreprocessChapter<'book, 'preprocessor> {

let already_open_tags = self.open_html_tags.len();
let mut still_open_tags = self.open_html_tags.len();
for node in html5gum::Tokenizer::new(html.as_ref()).infallible() {
match node {
html5gum::Token::StartTag(start) => {
if !start.self_closing {
self.open_html_tags.push(start.name);
}
}
html5gum::Token::EndTag(end) => match self.open_html_tags.last() {
Some(tag) if *tag == end.name => {
self.open_html_tags.pop();
still_open_tags = still_open_tags.min(self.open_html_tags.len());
if let HtmlContext::Block = ctx {
for node in html5gum::Tokenizer::new(html.as_ref()).infallible() {
match node {
html5gum::Token::StartTag(start) => {
if !start.self_closing {
self.open_html_tags.push(start.name);
}
}
html5gum::Token::EndTag(end) => match self.open_html_tags.last() {
Some(tag) if *tag == end.name => {
self.open_html_tags.pop();
still_open_tags = still_open_tags.min(self.open_html_tags.len());
}
_ => {}
},
_ => {}
},
_ => {}
}
}
}
use pulldown_cmark::Event;
let mut fenced_divs_available = || {
self.preprocessor
.ctx
Expand Down Expand Up @@ -1198,9 +1206,11 @@ impl<'book, 'preprocessor> PreprocessChapter<'book, 'preprocessor> {
.into_iter()
.flatten()
};
close_divs
.chain(iter::once(wrap_html(html)))
.chain(open_divs)
let html = match ctx {
HtmlContext::Inline => Event::InlineHtml(html),
HtmlContext::Block => Event::Html(html),
};
close_divs.chain(iter::once(html)).chain(open_divs)
}
}

Expand Down
Loading