Skip to content

Commit

Permalink
rendering/markdown: fix shortcode handling in codefences
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuyu committed Jan 7, 2022
1 parent e04d7e6 commit 3d6e131
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions components/rendering/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ pub fn markdown_to_html(
let mut events = Vec::new();
macro_rules! render_shortcodes {
($is_text:expr, $text:expr, $range:expr) => {
render_shortcodes!($is_text, false, $text, $range)
};
($is_text:expr, $should_escape:expr, $text:expr, $range:expr) => {
let orig_range_start = $range.start;
loop {
if let Some(ref shortcode) = next_shortcode {
Expand All @@ -215,7 +218,11 @@ pub fn markdown_to_html(
let shortcode = next_shortcode.take().unwrap();
match shortcode.render(&context.tera, &context.tera_context) {
Ok(s) => {
events.push(Event::Html(s.into()));
events.push(if $should_escape {
Event::Text(s.into())
} else {
Event::Html(s.into())
});
$range.start += SHORTCODE_PLACEHOLDER.len();
}
Err(e) => {
Expand All @@ -242,7 +249,32 @@ pub fn markdown_to_html(
match event {
Event::Text(text) => {
if let Some(ref mut code_block) = code_block {
let html = code_block.highlight(&text);
let html;
if contains_shortcode(text.as_ref()) {
let mut accumulated_block = String::new();
// mark the start of the code block events
let stack_start = events.len();
render_shortcodes!(true, true, text, range);
// after rendering the shortcodes we will collect all the text events
// and re-render them as code blocks
for event in events[stack_start..].iter() {
match event {
Event::Text(t) => accumulated_block += t,
_ => {
error = Some(Error::msg(format!(
"Unexpected event while expanding the code block: {:?}",
event
)));
break;
}
}
}
html = code_block.highlight(&accumulated_block);
// remove all the original events from shortcode rendering
events.truncate(stack_start);
} else {
html = code_block.highlight(&text);
}
events.push(Event::Html(html.into()));
} else {
let text = if context.config.markdown.render_emoji {
Expand Down

0 comments on commit 3d6e131

Please sign in to comment.