fix(engine): add workaround to markdown parsing to avoid crashing espanso with malformed markdown. Fix #759
This commit is contained in:
parent
85bd14236a
commit
cd89915525
|
@ -17,6 +17,8 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use log::error;
|
||||
|
||||
use super::super::Middleware;
|
||||
use crate::event::{effect::HtmlInjectRequest, Event, EventType};
|
||||
|
||||
|
@ -37,7 +39,12 @@ 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);
|
||||
// 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
|
||||
|
@ -54,6 +61,11 @@ impl Middleware for MarkdownMiddleware {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user