package main import cm "vendor:commonmark" import "core:fmt" import "core:strings" Note_Kind :: enum { Sidenote, Marginnote, } // strip_definitions scans markdown text for note definitions ([^id]: text for // sidenotes, [*id]: text for marginnotes), removes them, and returns the cleaned // text plus separate maps of id->definition for each kind. // Handles multi-line definitions with indented continuation lines. strip_definitions :: proc(body: string) -> (clean_body: string, sn_defs, mn_defs: map[string]string) { sn_defs = make(map[string]string) mn_defs = make(map[string]string) lines := strings.split(body, "\n") output_lines: [dynamic]string defer delete(output_lines) i := 0 for i < len(lines) { line := lines[i] id, def_text, kind, is_def := parse_def_line(line) if !is_def { append(&output_lines, line) i += 1 continue } // Collect definition text (initial line + multi-line continuations) def_parts: [dynamic]string if def_text != "" { append(&def_parts, def_text) } i += 1 for i < len(lines) { next := lines[i] // Stop at blank lines if len(next) == 0 { break } // Stop at new note definitions _, _, _, is_new_def := parse_def_line(next) if is_new_def { break } // Include as continuation (trim indented lines) if is_indented(next) { append(&def_parts, strings.trim_left(next, " \t")) } else { append(&def_parts, next) } i += 1 } joined := strings.join(def_parts[:], "\n") if kind == .Marginnote { mn_defs[id] = joined } else { sn_defs[id] = joined } delete(def_parts) } clean_body = strings.join(output_lines[:], "\n") return } // parse_def_line checks if a line is a note definition: [^id]: text (sidenote) // or [*id]: text (marginnote). parse_def_line :: proc(line: string) -> (id: string, text: string, kind: Note_Kind, ok: bool) { if len(line) < 5 || line[0] != '[' { return } if line[1] == '^' { kind = .Sidenote } else if line[1] == '*' { kind = .Marginnote } else { return } close := strings.index(line[2:], "]") if close < 0 { return } close += 2 if close + 1 >= len(line) || line[close + 1] != ':' { return } id = line[2:close] text = strings.trim_left(line[close + 2:], " \t") ok = true return } is_indented :: proc(line: string) -> bool { if len(line) == 0 { return false } return line[0] == ' ' || line[0] == '\t' } // inject_notes finds [^id] (sidenote) and [*id] (marginnote) references in // rendered HTML and replaces them with the appropriate margin markup. Each // definition is rendered through cmark separately. inject_notes :: proc(html: string, sn_defs, mn_defs: map[string]string) -> string { if len(sn_defs) == 0 && len(mn_defs) == 0 { return html } parts: [dynamic]string defer delete(parts) remaining := html for { sn_pos := strings.index(remaining, "[^") mn_pos := strings.index(remaining, "[*") is_margin := mn_pos >= 0 && (sn_pos < 0 || mn_pos < sn_pos) pos := sn_pos if is_margin { pos = mn_pos } if pos < 0 { append(&parts, remaining) break } // Append text before the reference append(&parts, remaining[:pos]) close := strings.index(remaining[pos + 2:], "]") if close < 0 { append(&parts, remaining[pos:]) break } id := remaining[pos + 2 : pos + 2 + close] ref_end := pos + 2 + close + 1 defs := sn_defs if is_margin { defs = mn_defs } def_text, found := defs[id] if !found { // No definition found, leave as literal text append(&parts, remaining[pos:ref_end]) remaining = remaining[ref_end:] continue } // Render definition through cmark for markdown support def_html := cm.markdown_to_html_from_string(def_text, {.Unsafe}) def_html = strip_p_tags(def_html) note: string if is_margin { note = fmt.aprintf( `%s`, id, id, def_html, ) } else { note = fmt.aprintf( `%s`, id, id, def_html, ) } append(&parts, note) remaining = remaining[ref_end:] } return strings.join(parts[:], "") } // strip_p_tags removes surrounding

if the HTML is a single paragraph. strip_p_tags :: proc(html: string) -> string { s := html if len(s) > 0 && s[len(s) - 1] == '\n' { s = s[:len(s) - 1] } if strings.has_prefix(s, "

") && strings.has_suffix(s, "

") { return s[3:len(s) - 4] } return s }