From cd899155256eb00cc5461a94daf13521e60b679d Mon Sep 17 00:00:00 2001
From: Federico Terzi
Date: Thu, 21 Oct 2021 21:52:22 +0200
Subject: [PATCH] fix(engine): add workaround to markdown parsing to avoid
crashing espanso with malformed markdown. Fix #759
---
.../src/process/middleware/markdown.rs | 42 ++++++++++++-------
1 file changed, 27 insertions(+), 15 deletions(-)
diff --git a/espanso-engine/src/process/middleware/markdown.rs b/espanso-engine/src/process/middleware/markdown.rs
index abebc54..7deec85 100644
--- a/espanso-engine/src/process/middleware/markdown.rs
+++ b/espanso-engine/src/process/middleware/markdown.rs
@@ -17,6 +17,8 @@
* along with espanso. If not, see .
*/
+use log::error;
+
use super::super::Middleware;
use crate::event::{effect::HtmlInjectRequest, Event, EventType};
@@ -37,23 +39,33 @@ impl Middleware for MarkdownMiddleware {
fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event {
if let EventType::MarkdownInject(m_event) = &event.etype {
// Render the markdown into HTML
- let html = markdown::to_html(&m_event.markdown);
- let mut html = html.trim();
+ // NOTE: we wrap the `to_html` call between catch_unwind because if the markdown is malformed,
+ // the library panics. Ideally, the library would return a Result::Err in that case, but
+ // for now it doesn't, so we employ that workaround.
+ // See also: https://github.com/federico-terzi/espanso/issues/759
+ let html = std::panic::catch_unwind(|| markdown::to_html(&m_event.markdown));
+ if let Ok(html) = html {
+ let mut html = html.trim();
- // Remove the surrounding paragraph
- if html.starts_with("") {
- html = html.trim_start_matches("
");
- }
- if html.ends_with("
") {
- html = html.trim_end_matches("
");
- }
+ // Remove the surrounding paragraph
+ if html.starts_with("") {
+ html = html.trim_start_matches("
");
+ }
+ if html.ends_with("
") {
+ html = html.trim_end_matches("");
+ }
- return Event::caused_by(
- event.source_id,
- EventType::HtmlInject(HtmlInjectRequest {
- html: html.to_owned(),
- }),
- );
+ return Event::caused_by(
+ event.source_id,
+ EventType::HtmlInject(HtmlInjectRequest {
+ html: html.to_owned(),
+ }),
+ );
+ } else {
+ error!("unable to convert markdown to HTML, is it malformed?");
+
+ return Event::caused_by(event.source_id, EventType::NOOP);
+ }
}
event