aboutsummaryrefslogtreecommitdiff
path: root/src/content/markdown
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2021-10-20 19:58:38 +0200
committerlns <matzeton@googlemail.com>2021-10-20 19:58:38 +0200
commit384724a00be9dbb8f40d295f99b9c6bcfb48b387 (patch)
tree6e053365bb995bf675397368281c0a3ff92bd9fc /src/content/markdown
parent7b01dd8e4b1779e4c4dd782dbec87acce0f4754b (diff)
Added MD4C support to render HTML from Markdown text.
* additional blog metadata properties Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src/content/markdown')
-rw-r--r--src/content/markdown/Markdown.cpp32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/content/markdown/Markdown.cpp b/src/content/markdown/Markdown.cpp
index 63358b4..a85f333 100644
--- a/src/content/markdown/Markdown.cpp
+++ b/src/content/markdown/Markdown.cpp
@@ -1,5 +1,7 @@
#include "Markdown.hpp"
+#include <md4c-html.h>
+
Markdown::Markdown(std::string uriBasePath, std::string markdownFilesPath, std::string mainTemplatePath)
: Content(),
m_UriBasePath(uriBasePath),
@@ -8,6 +10,13 @@ Markdown::Markdown(std::string uriBasePath, std::string markdownFilesPath, std::
{
}
+extern "C" void markdown_to_html_conversion(const MD_CHAR * const text, MD_SIZE size, void * const userdata)
+{
+ std::string * html = (std::string *)userdata;
+
+ html->append(text, size);
+}
+
bool Markdown::Init()
{
std::cout << "Markdown files path: " << m_MarkdownFilesPath << std::endl;
@@ -22,8 +31,27 @@ bool Markdown::Init()
for (auto const & mfile : fs.GetFiles())
{
- m_Markdowns[mfile.first] =
- std::make_shared<std::string>(std::string(mfile.second.data.begin(), mfile.second.data.end()));
+ Data const & data = mfile.second.data;
+ std::string html;
+
+ html.reserve(data.size() / 8 + 64);
+ int ret = md_html((MD_CHAR const *)data.data(),
+ data.size(),
+ markdown_to_html_conversion,
+ &html,
+ MD_DIALECT_GITHUB,
+ MD_FLAG_COLLAPSEWHITESPACE | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS |
+ MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEAUTOLINKS | MD_FLAG_TABLES |
+ MD_FLAG_STRIKETHROUGH | MD_FLAG_LATEXMATHSPANS | MD_FLAG_WIKILINKS | MD_FLAG_TASKLISTS |
+ MD_FLAG_UNDERLINE);
+
+ if (ret != 0)
+ {
+ std::cerr << "Markdown HTML rendering failed." << std::endl;
+ return false;
+ }
+
+ m_Markdowns[mfile.first] = std::make_shared<std::string>(html);
}
return true;