From f1708b3832d03fc4f4d985f93c89329b5efa78a5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 May 2020 20:51:35 +0200 Subject: [PATCH] LibWeb: Teach HtmlView how to render Markdown files :^) --- Applications/Browser/Makefile | 2 +- Applications/IRCClient/Makefile | 2 +- Libraries/LibWeb/HtmlView.cpp | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Applications/Browser/Makefile b/Applications/Browser/Makefile index 1cd944c891..6f53cb01d9 100644 --- a/Applications/Browser/Makefile +++ b/Applications/Browser/Makefile @@ -8,7 +8,7 @@ OBJS = \ PROGRAM = Browser -LIB_DEPS = Web JS TextCodec GUI Gfx IPC Protocol Core +LIB_DEPS = Web JS Markdown TextCodec GUI Gfx IPC Protocol Core main.cpp: ../../Libraries/LibWeb/CSS/PropertyID.h ../../Libraries/LibWeb/CSS/PropertyID.h: diff --git a/Applications/IRCClient/Makefile b/Applications/IRCClient/Makefile index 693d63ac6f..85cacc569b 100644 --- a/Applications/IRCClient/Makefile +++ b/Applications/IRCClient/Makefile @@ -11,6 +11,6 @@ OBJS = \ PROGRAM = IRCClient -LIB_DEPS = Web TextCodec JS GUI Gfx Protocol IPC Thread Pthread Core +LIB_DEPS = Web TextCodec JS Markdown GUI Gfx Protocol IPC Thread Pthread Core include ../../Makefile.common diff --git a/Libraries/LibWeb/HtmlView.cpp b/Libraries/LibWeb/HtmlView.cpp index 5ed2bb7504..8c0fb19eae 100644 --- a/Libraries/LibWeb/HtmlView.cpp +++ b/Libraries/LibWeb/HtmlView.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -321,6 +322,15 @@ void HtmlView::reload() load(main_frame().document()->url()); } +static RefPtr create_markdown_document(const ByteBuffer& data, const URL& url) +{ + Markdown::Document markdown_document; + if (!markdown_document.parse(data)) + return nullptr; + + return parse_html_document(markdown_document.render_to_html(), url); +} + static RefPtr create_text_document(const ByteBuffer& data, const URL& url) { auto document = adopt(*new Document(url)); @@ -420,6 +430,8 @@ void HtmlView::load(const URL& url) document = create_image_document(data, url); } else if (url.path().ends_with(".txt")) { document = create_text_document(data, url); + } else if (url.path().ends_with(".md")) { + document = create_markdown_document(data, url); } else { String encoding = "utf-8";