diff markdown_converter/markdown_to_html.c @ 184:8c74204fd362

[MD to HTML] Updated so it can be used through readme to html
author MrJuneJune <me@mrjunejune.com>
date Fri, 23 Jan 2026 22:20:35 -0800
parents 1c0878eb17de
children a2725419f988
line wrap: on
line diff
--- a/markdown_converter/markdown_to_html.c	Fri Jan 23 21:19:08 2026 -0800
+++ b/markdown_converter/markdown_to_html.c	Fri Jan 23 22:20:35 2026 -0800
@@ -4,6 +4,15 @@
 #include <ctype.h>
 #include "markdown_converter/markdown_to_html.h"
 
+// JavaScript needs this to free the memory later
+MDAPI void *wasm_alloc(size_t size) {
+    return malloc(size);
+}
+
+MDAPI void wasm_free(void* ptr) {
+    free(ptr);
+}
+
 #define INITIAL_BUFFER_SIZE 1024 * 1024 // 1MB
 
 // String buffer for building HTML output
@@ -132,6 +141,44 @@
   return (*line == '-' || *line == '*' || *line == '+') && line[1] == ' ';
 }
 
+// Check if line starts with an HTML tag
+static int is_html_block_start(const char *line)
+{
+  line = skip_whitespace(line);
+  if (*line != '<') return 0;
+  line++;
+
+  // Check for closing tag or comment
+  if (*line == '/' || *line == '!') return 1;
+
+  // Check for valid tag name (letter followed by alphanumeric)
+  if (!isalpha((unsigned char)*line)) return 0;
+
+  return 1;
+}
+
+// Check if line starts with a specific HTML tag (e.g., "script", "style")
+static int is_html_tag(const char *line, const char *tag)
+{
+  line = skip_whitespace(line);
+  if (*line != '<') return 0;
+  line++;
+
+  // Skip optional /
+  int is_closing = 0;
+  if (*line == '/') {
+    is_closing = 1;
+    line++;
+  }
+
+  size_t tag_len = strlen(tag);
+  if (strncasecmp(line, tag, tag_len) != 0) return 0;
+
+  char next = line[tag_len];
+  // Tag must be followed by space, >, or end for closing tags
+  return next == '>' || next == ' ' || next == '\t' || next == '\n' || next == '\0';
+}
+
 // Check if line is ordered list item
 static int is_ordered_list(const char *line)
 {
@@ -485,6 +532,53 @@
       continue;
     }
 
+    // HTML block - pass through unchanged
+    if (is_html_block_start(line)) {
+      // Check if it's a script or style tag that needs special handling
+      int is_script = is_html_tag(line, "script");
+      int is_style = is_html_tag(line, "style");
+
+      if (is_script || is_style) {
+        const char *end_tag = is_script ? "</script>" : "</style>";
+
+        // Output the opening line
+        buffer_append(buf, line);
+        buffer_append_char(buf, '\n');
+
+        free(line);
+        if (*ptr == '\n') ptr++;
+
+        // Collect content until closing tag
+        while (*ptr) {
+          line_start = ptr;
+          while (*ptr && *ptr != '\n') ptr++;
+          line_len = ptr - line_start;
+
+          line = (char *)malloc(line_len + 1);
+          if (!line) break;
+          memcpy(line, line_start, line_len);
+          line[line_len] = '\0';
+
+          buffer_append(buf, line);
+          buffer_append_char(buf, '\n');
+
+          int found_end = (strstr(line, end_tag) != NULL);
+          free(line);
+          if (*ptr == '\n') ptr++;
+
+          if (found_end) break;
+        }
+        continue;
+      }
+
+      // Regular HTML tag - just pass through the line
+      buffer_append(buf, line);
+      buffer_append_char(buf, '\n');
+      free(line);
+      if (*ptr == '\n') ptr++;
+      continue;
+    }
+
     // Regular paragraph
     buffer_append(buf, "<p>");
 
@@ -512,7 +606,8 @@
           starts_with(line, ">") ||
           is_horizontal_rule(line) ||
           is_unordered_list(line) ||
-          is_ordered_list(line)) {
+          is_ordered_list(line) ||
+          is_html_block_start(line)) {
         ptr = line_start;
         free(line);
         break;