From eefaabb7856970fd9443ec36145bfee0a8398299 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Thu, 17 Apr 2025 16:21:20 +0000 Subject: [PATCH] Remove broken footnote links from grammar summary Our grammar syntax supports linking to footnotes which we render in the Markdown version. This works when the rendering is done on the same page that the footnote appears. On the grammar summary page, however, where we also render the grammar, these footnotes are not present, and so the links were all broken. Let's fix this for now by not rendering these footnote links on the grammar summary page. --- mdbook-spec/src/grammar/render_markdown.rs | 39 +++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/mdbook-spec/src/grammar/render_markdown.rs b/mdbook-spec/src/grammar/render_markdown.rs index 36de0cef8..1cf0cde82 100644 --- a/mdbook-spec/src/grammar/render_markdown.rs +++ b/mdbook-spec/src/grammar/render_markdown.rs @@ -64,7 +64,8 @@ impl Production { name = self.name, ) .unwrap(); - self.expression.render_markdown(link_map, output); + self.expression + .render_markdown(link_map, output, for_summary); output.push('\n'); } } @@ -91,11 +92,16 @@ impl Expression { } } - fn render_markdown(&self, link_map: &HashMap, output: &mut String) { + fn render_markdown( + &self, + link_map: &HashMap, + output: &mut String, + for_summary: bool, + ) { match &self.kind { ExpressionKind::Grouped(e) => { output.push_str("( "); - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); if !matches!(e.last(), ExpressionKind::Break(_)) { output.push(' '); } @@ -104,7 +110,7 @@ impl Expression { ExpressionKind::Alt(es) => { let mut iter = es.iter().peekable(); while let Some(e) = iter.next() { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); if iter.peek().is_some() { if !matches!(e.last(), ExpressionKind::Break(_)) { output.push(' '); @@ -116,34 +122,34 @@ impl Expression { ExpressionKind::Sequence(es) => { let mut iter = es.iter().peekable(); while let Some(e) = iter.next() { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); if iter.peek().is_some() && !matches!(e.last(), ExpressionKind::Break(_)) { output.push(' '); } } } ExpressionKind::Optional(e) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); output.push_str("?"); } ExpressionKind::Repeat(e) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); output.push_str("\\*"); } ExpressionKind::RepeatNonGreedy(e) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); output.push_str("\\* (non-greedy)"); } ExpressionKind::RepeatPlus(e) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); output.push_str("+"); } ExpressionKind::RepeatPlusNonGreedy(e) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); output.push_str("+ (non-greedy)"); } ExpressionKind::RepeatRange(e, a, b) => { - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); write!( output, "{}..{}", @@ -174,7 +180,7 @@ impl Expression { ExpressionKind::Charset(set) => charset_render_markdown(set, link_map, output), ExpressionKind::NegExpression(e) => { output.push('~'); - e.render_markdown(link_map, output); + e.render_markdown(link_map, output, for_summary); } ExpressionKind::Unicode(s) => { output.push_str("U+"); @@ -184,9 +190,12 @@ impl Expression { if let Some(suffix) = &self.suffix { write!(output, "{suffix}").unwrap(); } - if let Some(footnote) = &self.footnote { - // The ZeroWidthSpace is to avoid conflicts with markdown link references. - write!(output, "​[^{footnote}]").unwrap(); + if !for_summary { + if let Some(footnote) = &self.footnote { + // The `ZeroWidthSpace` is to avoid conflicts with markdown link + // references. + write!(output, "​[^{footnote}]").unwrap(); + } } } }