// `@weft/markdown`: CommonMark and GitHub Flavored Markdown. import type { Block, Document, Inline, Options, Policy } from "./ast" import { parseDocument } from "./lib/blocks" import { renderDocument } from "./lib/render" import { sanitize as clean } from "./lib/sanitize" /// Parses Markdown into its syntax tree. Parsing never fails: any text is a document. /// /// ```weft /// let doc = parse("# Hello\n\nSome *text*.") /// doc.children.length // 2 /// ``` export fn parse(source: string, options: Options = {}): Document { return parseDocument(source, options.gfm ?? true) } /// Renders a syntax tree to HTML. Raw HTML is escaped and unsafe URLs are removed unless `options` say otherwise. export fn render(doc: Document, options: Options = {}): string { return renderDocument(doc, options) } /// Markdown to HTML, safe for untrusted input by default. /// /// ```weft /// toHtml("**bold** ") /// // "

bold <script>alert(1)</script>

\n" /// /// toHtml("# Title", { headingOffset: 1, linkTarget: "_blank" }) /// // "

Title

\n" /// ``` export fn toHtml(source: string, options: Options = {}): string { return renderDocument(parseDocument(source, options.gfm ?? true), options) } /// Removes everything `policy` doesn't allow from HTML, whatever produced it. See `Policy` for the defaults. /// /// ```weft /// sanitize("hi") // "hi" /// ``` export fn sanitize(html: string, policy?: Policy): string { return clean(html, policy) } fn inlineText(inlines: Inline[]): string { let mut out: string[] = [] for n of inlines { match n { Str { text } => out.push(text), SoftBreak => out.push(" "), HardBreak => out.push("\n"), CodeSpan { code } => out.push(code), Emphasis { children } | Strong { children } | Strikethrough { children } => out.push( inlineText(children), ), Link { url, children } => { let label = inlineText(children) out.push(label == url || `mailto:${label}` == url ? label : `${label} (${url})`) }, Picture { alt } => { let label = inlineText(alt) out.push(label == "" ? "[image]" : `[image: ${label}]`) }, HtmlInline => {}, } } return out.join("") } fn blockText(blocks: Block[], mut out: string[]): void { for block of blocks { match block { Paragraph { children } | Heading { children } => out.push(inlineText(children)), ThematicBreak => out.push("---"), CodeBlock { code } => out.push(code.trimEnd()), HtmlBlock => {}, Blockquote { children } => blockText(children, &out), BulletList { items } | OrderedList { items } => { for item of items { let mut inner: string[] = [] blockText(item.children, &inner) let mark = item.checked == null ? "- " : item.checked ? "- [x] " : "- [ ] " out.push(mark + inner.join("\n ")) } }, Table { head, rows } => { out.push(head.map(c => inlineText(c.children)).join(" | ")) for row of rows { out.push(row.map(c => inlineText(c.children)).join(" | ")) } }, } } } /// The document as plain text, for previews, search and the text part of an email. Links keep their address and images /// become `[image: caption]`. /// /// ```weft /// toText(parse("Read [the guide](https://weft.dev).")) /// // "Read the guide (https://weft.dev)." /// ``` export fn toText(doc: Document): string { let mut out: string[] = [] blockText(doc.children, &out) return out.join("\n\n") } fn visitInlines(inlines: Inline[], f: (node: Inline) => void): void { for n of inlines { f(n) match n { Emphasis { children } | Strong { children } | Strikethrough { children } | Link { children, } => visitInlines(children, f), Picture { alt } => visitInlines(alt, f), _ => {}, } } } fn visitBlocks(blocks: Block[], f: (node: Inline) => void): void { for block of blocks { match block { Paragraph { children } | Heading { children } => visitInlines(children, f), Blockquote { children } => visitBlocks(children, f), BulletList { items } | OrderedList { items } => { for item of items { visitBlocks(item.children, f) } }, Table { head, rows } => { for cell of head { visitInlines(cell.children, f) } for row of rows { for cell of row { visitInlines(cell.children, f) } } }, _ => {}, } } } /// Calls `f` with every inline of the document, in order, outermost first. /// /// ```weft /// let mut images: string[] = [] /// visit(parse(source), n => { /// if let Picture { url } = n { images.push(url) } /// }) /// ``` export fn visit(doc: Document, f: (node: Inline) => void) { visitBlocks(doc.children, f) }