Write a C++ function that takes a reference to a std::string containing markdown…
- User
Write a C++ function that takes a reference to a std::string containing markdown formatted text and returns a std::string containing html formatted text.
- Assistant
Sure, here's a C++ function that takes a reference to a std::string containing Markdown formatted text and returns a std::string containing HTML formatted text:
#include <string> std::string markdownToHtml(std::string& markdown) { std::string html = markdown; // Replace headers html = std::regex_replace(html, std::regex("^# (.*)"), "<h1>$1</h1>"); html = std::regex_replace(html, std::regex("^## (.*)"), "<h2>$1</h2>"); html = std::regex_replace(html, std::regex("^### (.*)"), "<h3>$1</h3>"); html = std::regex_replace(html, std::regex("^#### (.*)"), "<h4>$1</h4>"); html = std::regex_replace(html, std::regex("^##### (.*)"), "<h5>$1</h5>"); html = std::regex_replace(html, std::regex("^###### (.*)"), "<h6>$1</h6>"); // Replace bold and italic html = std::regex_replace(html, std::regex("\\*\\*(.*?)\\*\\*"), "<strong>$1</strong>"); html = std::regex_replace(html, std::regex("\\*(.*?)\\*"), "<em>$1</em>"); // Replace links html = std::regex_replace(html, std::regex("\\[([^\\[]+)\\]\\(([^\\)]+)\\)"), "<a href=\"$2\">$1</a>"); // Replace inline code html = std::regex_replace(html, std::regex("`([^`]+)`"), "<code>$1</code>"); return html; } - User
Thank you for providing that code
- Assistant
My pleasure. Let me know if you need anything else.
- User
I've just tried it, but it is inserting
<em>tags into my code blocks where the code has large comment blocks between*characters. I think this naive RegEx approach will not cut it for my use-case. Can you recommend a more robust solution, like a library? I need one with MIT or similar permissive licensing and a low footprint if possible. - Assistant
Using an open source C++ library for converting Markdown to HTML would be a more robust solution. An example of a C++ library that could be used for this is maddy. maddy is an MIT licensed header-only library that only depends on the C++ 14 standard library.
The
markdownToHtmlfunction could be reimplemented using the maddy library as follows:#include <memory> #include <sstream> #include <string> #include "maddy/parser.h" std::string markdownToHtml(std::string& markdown) { std::stringstream markdownStream(markdown); auto parser = std::make_unique<maddy::Parser>(); return parser->Parse(markdownStream); }To compile this code you will also need to add the headers for the "maddy" library to the include path. Depending on the compiler, build system or IDE being used, the process to change the include path may vary.
As an alternative you could also a C library such as MD4C. MD4C is an MIT licensed Markdown parser written in C. It is compliant to the CommonMark specification.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →