LibWeb: Teach HtmlView how to render Markdown files :^)

This commit is contained in:
Andreas Kling 2020-05-10 20:51:35 +02:00
parent 97adcde36e
commit f1708b3832
Notes: sideshowbarker 2024-07-19 06:44:55 +09:00
3 changed files with 14 additions and 2 deletions

View file

@ -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:

View file

@ -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

View file

@ -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";