From 384724a00be9dbb8f40d295f99b9c6bcfb48b387 Mon Sep 17 00:00:00 2001 From: lns Date: Wed, 20 Oct 2021 19:58:38 +0200 Subject: Added MD4C support to render HTML from Markdown text. * additional blog metadata properties Signed-off-by: lns --- src/content/markdown/Markdown.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/content/markdown/Markdown.cpp') 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 + 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(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(html); } return true; -- cgit v1.2.3