Grav v1.14.0

Markdown

import "std/text" (included in import "std") · namespace std.text

Markdown to HTML (CommonMark's everyday parts and GitHub's tables), and to plain text: for mail bodies, docs, comments.

grav
import "std"

fn main() {
    print(Markdown.toHtml("# Hello\n\nThanks for **joining**. See [the docs](https://x.io).\n\n- one\n- two"))
    print(Markdown.toText("Thanks for **joining**. See [the docs](https://x.io)."))
    print(Markdown.toHtml("Hi <script>x()</script> [x](javascript:x())", true))
}
Output
<h1>Hello</h1>
<p>Thanks for <strong>joining</strong>. See <a href="https://x.io">the docs</a>.</p>
<ul>
<li>one</li>
<li>two</li>
</ul>
Thanks for joining. See the docs (https://x.io).
<p>Hi &lt;script&gt;x()&lt;/script&gt; <a href="#">x</a></p>
  • Blocks: ATX (#) and setext headings, paragraphs, > quotes, - * + and 1. lists (nested by indentation), fenced code (with a language class) and indented code, --- rules, | tables | with alignment, and HTML blocks.
  • Inline: **strong**, *em* and _em_, ~~strike~~, `code`, [links](url "title"), ![images](src), <https://autolinks>, line breaks (two trailing spaces or \), and \ escapes. Text is HTML-escaped.
  • Safe mode (toHtml(source, true), for anything users write): raw HTML is escaped instead of passed through, and links or images to javascript:, vbscript: or data: (other than data:image/) point to #.
  • toText gives the same text readable without markup (a plain-text mail), links written as text (url).

API

class Markdown

Static members

  • static fn toHtml(source: string, safe: bool = false) -> string — HTML for Markdown source; safe for text from users (raw HTML escaped, links to javascript:, vbscript: and data: dropped).
  • static fn toText(source: string) -> string — The same text, readable without markup (for a plain-text mail).
  • static fn inline(text: string, safe: bool = false) -> string — One line's inline markup as HTML.
  • static fn escape(s: string) -> string — Text with & < > " ' escaped for HTML.

class MarkdownRenderer

Constructors

  • new MarkdownRenderer(safe: bool)

Static members

  • static fn escape(s: string) -> string

Fields

  • safe: bool = false — Escape raw HTML and drop links to javascript:, data: and vbscript: (for text from users).

Methods

  • fn toHtml(source: string) -> string
  • fn inline(text: string) -> string
  • fn toText(source: string) -> string — The source made readable without markup: links as "text (url)", emphasis and code marks dropped, headings and lists kept as lines.

Edit this page on GitHub