summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-10 20:51:35 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-10 22:32:12 +0200
commitf1708b3832d03fc4f4d985f93c89329b5efa78a5 (patch)
treeeb04c8ad71873ba48df84f258402a2a731d4ed72
parent97adcde36ea642e4facf561cb556cfbb23e7ad0c (diff)
downloadserenity-f1708b3832d03fc4f4d985f93c89329b5efa78a5.zip
LibWeb: Teach HtmlView how to render Markdown files :^)
-rw-r--r--Applications/Browser/Makefile2
-rw-r--r--Applications/IRCClient/Makefile2
-rw-r--r--Libraries/LibWeb/HtmlView.cpp12
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 <LibGUI/Window.h>
#include <LibGfx/ImageDecoder.h>
#include <LibJS/Runtime/Value.h>
+#include <LibMarkdown/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/HTMLAnchorElement.h>
@@ -321,6 +322,15 @@ void HtmlView::reload()
load(main_frame().document()->url());
}
+static RefPtr<Document> 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<Document> 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";