From 6d4cb188096390b2db7683be11e3977725834789 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Tue, 6 Jan 2026 17:46:10 +0100 Subject: [PATCH 1/2] added get_html() function --- src/markdown.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/markdown.cpp b/src/markdown.cpp index fdb7f86..3049ba3 100644 --- a/src/markdown.cpp +++ b/src/markdown.cpp @@ -1,5 +1,6 @@ #include "markdown.hpp" #include "utils.hpp" +#include #include #include #include @@ -41,3 +42,33 @@ std::optional get_metadata(const std::string &FILE_PATH) { metadata_map["title"], convert_string_to_set(metadata_map["tags"])}); } + +static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) { + std::string *out = static_cast(userdata); + out->append(data, size); +} + +std::string get_html(const std::string &FILE_PATH) { + std::fstream file(FILE_PATH); + std::string buffer, file_string, out; + getline(file, buffer); + bool is_metadata = buffer == "---"; + + /* skip metadata */ + if (is_metadata) { + while (getline(file, buffer)) { + if (buffer == "---") { + break; + } + } + } else { + file_string += buffer + '\n'; + } + + while (getline(file, buffer)) { + file_string += buffer + '\n'; + } + + md_html(file_string.c_str(), file_string.size(), html_callback, &out, 0, 0); + return out; +} From 81cc7979b756db57db50c5997fd4d590ea2035c5 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Tue, 6 Jan 2026 17:46:31 +0100 Subject: [PATCH 2/2] added tests for get_html() function --- src/tests.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/tests.cpp b/src/tests.cpp index e0ad3f1..ac5d076 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -47,3 +47,44 @@ TEST_CASE("get_metadata function test with a file that doesn't have metadata") { std::optional result = get_metadata(FILE_NAME); REQUIRE(result == std::nullopt); } + +/* theres to much testing stuff in examples + * i need to move tests to some other directory with testing files + */ +TEST_CASE("get_html function parsing test") { + const std::string INPUT = "../examples/post.md"; + const std::string EXPECTED_OUTPUT = R"HTML(

This is an example post

+

In this example we will do a couple of cool markdown things.

+

This is a h2 header

+

This text is written in italic!

+

Look at this!

+

This text is BOLD!

+

Hello world!

+
#include <iostream>
+
+int main() {
+    std::cout << "Hello world!" << std::endl;
+}
+
+

This is super cool!

+

Time to link some random website!

+

This should be clickable

+

Image test

+

this should show an image

+

This is an ordered list

+
    +
  1. one
  2. +
  3. six
  4. +
  5. seven
  6. +
+

This is an unordered one

+
    +
  • six
  • +
  • nine
  • +
  • six
  • +
  • nine
  • +
+)HTML"; + + REQUIRE(get_html(INPUT) == EXPECTED_OUTPUT); +}