Skip to content

Commit

Permalink
Invert the order of the pattern match for readability.
Browse files Browse the repository at this point in the history
Trying to make changes to this made my head hurt: the fundamental split
is on the state, *not* on the tag. So flip it.
  • Loading branch information
chriskrycho committed Apr 26, 2024
1 parent 301c704 commit 5f19f77
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,23 @@ pub fn rewrite(text: &str) -> String {
let mut state = Default;

for event in parser {
match (event, &mut state) {
(Start(Tag::BlockQuote), Default) => {
match (&mut state, event) {
(Default, Start(Tag::BlockQuote)) => {
state = StartingBlockquote(vec![Start(Tag::BlockQuote)]);
}

(Text(content), StartingBlockquote(blockquote_events)) => {
(StartingBlockquote(blockquote_events), Text(content)) => {
if content.starts_with("Note: ") {
// This needs the “extra” `SoftBreak`s so that when the
// final rendering pass happens, it does not end up treating
// the internal content as inline: content inside HTML
// blocks is only rendered as Markdown when it is separated
// from the block HTML elements: otherwise it gets treated
// as inline HTML and *not* rendered.
// This needs the "extra" `SoftBreak`s so that when the final rendering pass
// happens, it does not end up treating the internal content as inline *or*
// treating the HTML tags as inline tags:
//
// - Content inside HTML blocks is only rendered as Markdown when it is
// separated from the block HTML elements: otherwise it gets treated as inline
// HTML and *not* rendered.
// - Along the same lines, an HTML tag that happens to be directly adjacent to
// the end of a previous Markdown block will end up being rendered as part of
// that block.
events.extend([
Html(r#"<section class="note" aria-label="Note" aria-role="note">"#.into()),
SoftBreak,
Expand All @@ -82,7 +86,7 @@ pub fn rewrite(text: &str) -> String {
}
}

(heading @ Start(Tag::Heading { .. }), StartingBlockquote(_blockquote_events)) => {
(StartingBlockquote(_blockquote_events), heading @ Start(Tag::Heading { .. })) => {
events.extend([
Html(r#"<section class="note" aria-label="Note" aria-role="note">"#.into()),
SoftBreak,
Expand All @@ -92,18 +96,18 @@ pub fn rewrite(text: &str) -> String {
state = InNote;
}

(Start(Tag::Paragraph), StartingBlockquote(ref mut events)) => {
(StartingBlockquote(ref mut events), Start(Tag::Paragraph)) => {
events.push(Start(Tag::Paragraph));
}

(End(TagEnd::BlockQuote), InNote) => {
(InNote, End(TagEnd::BlockQuote)) => {
// As with the start of the block HTML, the closing HTML must be
// separated from the Markdown text by two newlines.
events.extend([SoftBreak, SoftBreak, Html("</section>".into())]);
state = Default;
}

(event, _) => {
(_, event) => {
events.push(event);
}
}
Expand Down

0 comments on commit 5f19f77

Please sign in to comment.