| 1 | /* |
| 2 | * fetch_md.h - pure C99 HTML tokenizer -> clean Markdown converter. |
| 3 | * |
| 4 | * Scope (task spec, matching what the fetch MCP tool needs): |
| 5 | * - strips <script>/<style>/<noscript>/<svg>/<head>/<nav>/<aside>/<footer>/ |
| 6 | * <form>/<template> subtrees and comments |
| 7 | * - headings h1..h6 -> ATX (#..######) |
| 8 | * - links -> [text](href) (absolute-ised against the page URL) |
| 9 | * - ul/ol -> - / 1. lists (one nesting level) |
| 10 | * - pre/code -> fenced blocks / `code spans` |
| 11 | * - strong/b -> **..**, em/i -> *..* |
| 12 | * - p/div/br/hr/blockquote/table basics -> line breaks / pipes |
| 13 | * - decodes the common HTML entities and &#NNN; / &#xHH; |
| 14 | * |
| 15 | * It is a forgiving tokenizer over raw bytes (UTF-8 passes through untouched); |
| 16 | * it is not a full DOM. That is enough to take a web page down to clean |
| 17 | * readable Markdown and is a clean-room reimplementation, not a port of any |
| 18 | * upstream converter. |
| 19 | */ |
| 20 | #ifndef SX_FETCH_MD_H |
| 21 | #define SX_FETCH_MD_H |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | /* Convert an HTML document to Markdown. Returns a malloc'd NUL-terminated |
| 26 | * string and sets *out_len. Always returns non-NULL. */ |
| 27 | char *fx_html_to_markdown(const char *html, size_t len, size_t *out_len); |
| 28 | |
| 29 | /* Extract the document title (og:title meta, then <title>). Returns malloc'd |
| 30 | * UTF-8 text or NULL; sets *out_len. */ |
| 31 | char *fx_html_title(const char *html, size_t len, size_t *out_len); |
| 32 | |
| 33 | /* Whether markdown `md` (length md_len) already opens with the title text |
| 34 | * (word-insensitive, like upstream's title.py). */ |
| 35 | int fx_md_starts_with_title(const char *md, size_t md_len, |
| 36 | const char *title, size_t title_len); |
| 37 | |
| 38 | /* Set the base URL used to absolutise relative links (may be NULL). */ |
| 39 | void fx_md_set_base_url(const char *url); |
| 40 | |
| 41 | #endif /* SX_FETCH_MD_H */ |