/* Markdown as Pi's terminal UI draws it (markdown.c): headings, paragraphs * with `code`, bold, italic, strikethrough and links, bulleted, numbered and * nested lists, block quotes, fenced code with its language label, rules and * tables. Pure functions: text in, styled and wrapped lines out. */ #ifndef PIC_TUI_MARKDOWN_H #define PIC_TUI_MARKDOWN_H #include #include "width.h" /* How links reach the terminal (tui_md_opts.links, tui_hyperlinks_supported). DRAWN: the link text is an OSC 8 * hyperlink and a markdown link's url is not printed. TEXT, for a terminal with no OSC 8 that opens a URL it can * see (Apple's Terminal): the url is printed after the text and both are marked with OSC 8 all the same, so pic * knows the link under a click; the renderer drops the sequences (tui_render_hide_links). OFF: neither. */ enum { TUI_LINKS_OFF, TUI_LINKS_DRAWN, TUI_LINKS_TEXT }; typedef struct { const char *base; /* SGR set again after every style change; NULL or "": none */ int links; /* TUI_LINKS_* */ int preserve; /* a user message: list markers and backslashes stay as typed */ } tui_md_opts; /* text[0..len) as lines of at most cols columns, appended to out. Blocks are * separated by one blank line; no trailing blank line. 0 or -1 (out of memory). */ int tui_markdown(const char *text, size_t len, size_t cols, const tui_md_opts *o, tui_lines *out); /* A streamed reply is flushed block by block: the bytes of the first block of * s that is finished (it ends in a blank line outside a code fence), blank * line included, or a table that a non-table line follows; 0 when the first * block is still open. */ size_t tui_markdown_block_len(const char *s); /* Whether the open block is a table (or a line that may become its header) * still arriving: it is drawn whole, never a line at a time. */ int tui_markdown_table_open(const char *s); /* One line of a block too long to wait for: inside a code fence (*fence) it is * code; a fence line opens or closes it; otherwise it is rendered as text. */ int tui_markdown_line(const char *line, size_t len, int *fence, size_t cols, const tui_md_opts *o, tui_lines *out); /* Pi's user message: the text rendered as markdown on the user-message * background, one column of padding on each side and a blank row above and * below, every row padded to cols. */ int tui_user_message_lines(const char *text, size_t cols, int links, tui_lines *out); /* How to draw links, a TUI_LINKS_*. MODE is the hyperlinks setting: "on" * (DRAWN), "off", or "auto" (NULL too): DRAWN unless the terminal is known to * show the sequence or to drop it (TERM dumb or linux, GNU screen: OFF; Apple * Terminal, which swallows it: TEXT); inside tmux only from tmux 3.4, which * passes links on. */ int tui_hyperlinks_supported(const char *mode); /* Turns the marking of links in tool output and bare URLs and paths on or off * (their visible text is the same either way), and sets the directory relative paths resolve against (the workspace; NULL: the current * directory). Markdown links follow tui_md_opts.links. */ void tui_links_set(int on, const char *base); int tui_links_on(void); /* The URL a link to dest[0..n) opens: a URL with a scheme (https://, mailto:) * as it is; a path as file:///ABSOLUTE/PATH, resolved against the base, * percent-encoded, with a :LINE[:COL] or :A-B suffix and a #fragment left * out. NULL for an anchor alone or nothing. malloc'd. */ char *tui_link_url(const char *dest, size_t n); /* s[0..n) with each bare http(s) URL and each path that names an existing * file or directory made an OSC 8 link (with an id, so a wrapped one stays * one link); the visible text is unchanged and text already in a link is * left alone. NULL when nothing was linked (or links are off); else malloc'd, * its length in *outn. */ char *tui_linkify(const char *s, size_t n, size_t *outn); /* Whether a click may hand url to the system opener: an http(s) or mailto * URL, or a file:/// URL naming an existing directory or a regular file that * is not executable and whose extension is not one the opener would run * (.app, .command, .sh, .py, ...; Ghostty refuses the same). */ int tui_link_safe(const char *url); /* The sequence that opens an OSC 8 link to url[0..n): ESC ] 8 ; id=ID ; URL * ESC \, the id a hash of the URL so every piece of a wrapped link shares it. * malloc'd. */ char *tui_link_open(const char *url, size_t n); #endif