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))
}<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 <script>x()</script> <a href="#">x</a></p>- Blocks: ATX (
#) and setext headings, paragraphs,>quotes,-*+and1.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"),,<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 tojavascript:,vbscript:ordata:(other thandata:image/) point to#. toTextgives the same text readable without markup (a plain-text mail), links written astext (url).
API
class Markdown
Static members
static fn toHtml(source: string, safe: bool = false) -> string— HTML for Markdownsource;safefor 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) -> stringfn inline(text: string) -> stringfn 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.