Mercurial
comparison markdown_converter/markdown_to_html.c @ 158:1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 07:59:19 -0800 |
| parents | cd35e600ae34 |
| children | 8c74204fd362 |
comparison
equal
deleted
inserted
replaced
| 157:2db6253f355d | 158:1c0878eb17de |
|---|---|
| 1 /** | |
| 2 * Markdown to HTML Converter - C Implementation | |
| 3 * Supports: headers, bold, italic, links, lists, code blocks, blockquotes, horizontal rules | |
| 4 */ | |
| 5 | |
| 6 #ifndef MARKDOWN_TO_HTML_H | |
| 7 #define MARKDOWN_TO_HTML_H | |
| 8 | |
| 9 #include <stddef.h> | |
| 10 | |
| 11 // Export macro for WASM/Emscripten | |
| 12 #ifdef __EMSCRIPTEN__ | |
| 13 #include <emscripten.h> | |
| 14 #define MDAPI EMSCRIPTEN_KEEPALIVE | |
| 15 #else | |
| 16 #ifdef _WIN32 | |
| 17 #ifdef MARKDOWN_EXPORTS | |
| 18 #define MDAPI __declspec(dllexport) | |
| 19 #else | |
| 20 #define MDAPI __declspec(dllimport) | |
| 21 #endif | |
| 22 #else | |
| 23 #define MDAPI extern | |
| 24 #endif | |
| 25 #endif | |
| 26 | |
| 27 /** | |
| 28 * Convert markdown string to HTML string. | |
| 29 * | |
| 30 * @param markdown The input markdown string (null-terminated) | |
| 31 * @return Newly allocated HTML string. Caller must free with markdown_free(). | |
| 32 * Returns NULL on allocation failure. | |
| 33 * | |
| 34 * Supported markdown features: | |
| 35 * - Headers: # H1, ## H2, ... ###### H6 | |
| 36 * - Bold: **text** or __text__ | |
| 37 * - Italic: *text* or _text_ | |
| 38 * - Strikethrough: ~~text~~ | |
| 39 * - Links: [text](url) | |
| 40 * - Images:  | |
| 41 * - Inline code: `code` | |
| 42 * - Code blocks: ```code``` | |
| 43 * - Unordered lists: -, *, + | |
| 44 * - Ordered lists: 1., 2., etc. | |
| 45 * - Blockquotes: > text | |
| 46 * - Horizontal rules: ---, ***, ___ | |
| 47 */ | |
| 48 MDAPI char *markdown_to_html(const char *markdown); | |
| 49 | |
| 50 /** | |
| 51 * Free HTML string returned by markdown_to_html. | |
| 52 * | |
| 53 * @param html The HTML string to free | |
| 54 */ | |
| 55 MDAPI void markdown_free(char *html); | |
| 56 | |
| 57 /** | |
| 58 * Get length of HTML string (useful for WASM memory operations). | |
| 59 * | |
| 60 * @param html The HTML string | |
| 61 * @return Length of the string, or 0 if NULL | |
| 62 */ | |
| 63 MDAPI size_t markdown_get_length(const char *html); | |
| 64 | |
| 65 #endif // MARKDOWN_TO_HTML_H | |
| 66 #include <string.h> | 1 #include <string.h> |
| 67 #include <stdlib.h> | 2 #include <stdlib.h> |
| 68 #include <stdio.h> | 3 #include <stdio.h> |
| 69 #include <ctype.h> | 4 #include <ctype.h> |
| 70 | 5 #include "markdown_converter/markdown_to_html.h" |
| 71 #define INITIAL_BUFFER_SIZE 4096 | 6 |
| 7 #define INITIAL_BUFFER_SIZE 1024 * 1024 // 1MB | |
| 72 | 8 |
| 73 // String buffer for building HTML output | 9 // String buffer for building HTML output |
| 74 typedef struct { | 10 typedef struct { |
| 75 char *data; | 11 char *data; |
| 76 size_t length; | 12 size_t length; |
| 317 i = end + 1; | 253 i = end + 1; |
| 318 continue; | 254 continue; |
| 319 } | 255 } |
| 320 } | 256 } |
| 321 | 257 |
| 258 // This might not be needed for now. | |
| 322 // HTML escape special characters | 259 // HTML escape special characters |
| 323 if (text[i] == '<') { | 260 // if (text[i] == '<') { |
| 324 buffer_append(buf, "<"); | 261 // buffer_append(buf, "<"); |
| 325 } else if (text[i] == '>') { | 262 // } else if (text[i] == '>') { |
| 326 buffer_append(buf, ">"); | 263 // buffer_append(buf, ">"); |
| 327 } else if (text[i] == '&') { | 264 // } else if (text[i] == '&') { |
| 328 buffer_append(buf, "&"); | 265 // buffer_append(buf, "&"); |
| 329 } else { | 266 // } else { |
| 330 buffer_append_char(buf, text[i]); | 267 // buffer_append_char(buf, text[i]); |
| 331 } | 268 // } |
| 269 buffer_append_char(buf, text[i]); | |
| 332 i++; | 270 i++; |
| 333 } | 271 } |
| 334 } | 272 } |
| 335 | 273 |
| 336 // Convert markdown to HTML | 274 // Convert markdown to HTML |