Fix markdown shortcodes
This commit is contained in:
parent
3dbf811740
commit
9191a2726c
@ -1,7 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## 0.16.0 (unreleased)
|
||||
## 0.15.1 (unreleased)
|
||||
|
||||
- Fix markdown shortcodes not being rendered correctly
|
||||
|
||||
## 0.15.0 (2021-12-05)
|
||||
|
||||
|
@ -39,7 +39,7 @@ pub fn insert_md_shortcodes(
|
||||
for (md_sc_span, rendered_length) in &transforms {
|
||||
sc.update_range(md_sc_span, *rendered_length);
|
||||
}
|
||||
// It has been checked before that this exist
|
||||
|
||||
if sc.file_type() == ShortcodeFileType::Html {
|
||||
html_shortcodes.push(sc);
|
||||
continue;
|
||||
|
@ -50,18 +50,21 @@ impl Shortcode {
|
||||
}
|
||||
|
||||
pub fn update_range(&mut self, sc_span: &Range<usize>, rendered_length: usize) {
|
||||
if self.span.start > sc_span.start {
|
||||
let delta = if sc_span.end < rendered_length {
|
||||
rendered_length - sc_span.end
|
||||
} else {
|
||||
sc_span.end - rendered_length
|
||||
};
|
||||
if self.span.start < sc_span.start {
|
||||
return;
|
||||
}
|
||||
|
||||
if sc_span.end < rendered_length {
|
||||
self.span = (self.span.start + delta)..(self.span.end + delta);
|
||||
} else {
|
||||
self.span = (self.span.start - delta)..(self.span.end - delta);
|
||||
}
|
||||
let rendered_end = sc_span.start + rendered_length;
|
||||
let delta = if sc_span.end < rendered_end {
|
||||
rendered_end - sc_span.end
|
||||
} else {
|
||||
sc_span.end - rendered_end
|
||||
};
|
||||
|
||||
if sc_span.end < rendered_end {
|
||||
self.span = (self.span.start + delta)..(self.span.end + delta);
|
||||
} else {
|
||||
self.span = (self.span.start - delta)..(self.span.end - delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -373,12 +376,27 @@ mod tests {
|
||||
nth: 0,
|
||||
tera_name: String::new(),
|
||||
};
|
||||
// 6 -> 10 in length so +4 on both sides of the range
|
||||
sc.update_range(&(2..8), 10);
|
||||
assert_eq!(sc.span, 12..22);
|
||||
sc.update_range(&(24..30), 30);
|
||||
assert_eq!(sc.span, 12..22);
|
||||
sc.update_range(&(5..11), 6);
|
||||
assert_eq!(sc.span, 7..17);
|
||||
assert_eq!(sc.span, 14..24);
|
||||
// After the shortcode so no impact
|
||||
sc.update_range(&(25..30), 30);
|
||||
assert_eq!(sc.span, 14..24);
|
||||
// +4 again
|
||||
sc.update_range(&(5..11), 10);
|
||||
assert_eq!(sc.span, 18..28);
|
||||
|
||||
// buggy case from https://zola.discourse.group/t/zola-0-15-md-shortcode-stopped-working/1099/3
|
||||
let mut sc = Shortcode {
|
||||
name: "a".to_string(),
|
||||
args: Value::Null,
|
||||
span: 42..65,
|
||||
body: None,
|
||||
nth: 0,
|
||||
tera_name: String::new(),
|
||||
};
|
||||
sc.update_range(&(9..32), 3);
|
||||
assert_eq!(sc.span, 22..45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1625,3 +1625,34 @@ fn can_use_smart_punctuation() {
|
||||
let res = render_content(r#"This -- is "it"..."#, &context).unwrap();
|
||||
assert_eq!(res.body, "<p>This – is “it”…</p>\n");
|
||||
}
|
||||
|
||||
// https://zola.discourse.group/t/zola-0-15-md-shortcode-stopped-working/1099/2
|
||||
#[test]
|
||||
fn md_shortcode_regression() {
|
||||
let permalinks_ctx = HashMap::new();
|
||||
let mut tera = Tera::default();
|
||||
tera.extend(&ZOLA_TERA).unwrap();
|
||||
tera.add_raw_template("shortcodes/code.md", "123").unwrap();
|
||||
let config = Config::default_for_test();
|
||||
let mut context = RenderContext::new(
|
||||
&tera,
|
||||
&config,
|
||||
&config.default_language,
|
||||
"",
|
||||
&permalinks_ctx,
|
||||
InsertAnchor::None,
|
||||
);
|
||||
let shortcode_def = utils::templates::get_shortcodes(&tera);
|
||||
context.set_shortcode_definitions(&shortcode_def);
|
||||
|
||||
let markdown_string = r#"
|
||||
ttest1
|
||||
|
||||
{{ code(path = "content/software/supercollider/pakt-februari/pakt29.scd", syntax = "supercollider") }}
|
||||
|
||||
ttest2
|
||||
|
||||
{{ code(path = "content/software/supercollider/pakt-februari/pakt29.scd", syntax = "supercollider") }}"#;
|
||||
let res = render_content(markdown_string, &context).unwrap();
|
||||
assert_eq!(res.body, "<p>ttest1</p>\n<p>123</p>\n<p>ttest2</p>\n<p>123</p>\n");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user