Reference · Markup

markdown_page

signature
markdown_page(str, map | null=) -> html

What it does

Markdown text as a whole HTML page, for a route to answer with: what markdown makes, inside a doctype, an <html>, and a <head> with <meta charset="utf-8">, a viewport and a <title>. A fragment from markdown has none of these, so a browser shows it in quirks mode. The title is the title option, or else the text of the first level-one heading (# Title, or a line underlined with =), as it shows, without its marks, or else empty. The options are title, lang (a language tag, written on <html>) and html, which keeps HTML written in the text, as markdown's does.

Examples

in a program
route GET /notes = markdown_page(read_file("notes.md")?)route GET /about = markdown_page(read_file("about.md")?, { title: "About us", lang: "en" })
print(markdown_page("# Notes\n\nSome *text*."))print(markdown_page("no heading", { title: "Notes", lang: "en" }))
output
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Notes</title></head><body><h1>Notes</h1><p>Some <em>text</em>.</p></body></html><!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Notes</title></head><body><p>no heading</p></body></html>

Edge cases

let title = (s) => re.find(str(markdown_page(s)), "<title>(.*)</title>").groups[0]print(title("Notes\n=====\n\ntext"), "|", title("```\n# not this\n```\n\n# Real *one*"), "|", title("## Second\n\n# First"))print(markdown_page("<b>x</b>"))print(markdown_page("<b>x</b>", { html: true, title: "<b>" }))print(type_of(markdown_page("")))
output
Notes | Real one | First<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title></title></head><body><p>&lt;b&gt;x&lt;/b&gt;</p></body></html><!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>&lt;b&gt;</title></head><body><p><b>x</b></p></body></html>html

Errors

An option it does not take, a title that is not a str, a lang that is not a language tag, or options that are not a map, are runtime errors, not error values.

this one fails
print(markdown_page("x", { titel: "a" }))
output
line 1: `markdown_page` has no option `titel`: it takes `title`, `lang` and `html`
this one fails
print(markdown_page("x", { lang: "en us" }))
output
line 1: `markdown_page`'s lang is a language tag such as "en", got "en us"

Chaining

The text comes first, so a file read in a pipeline goes straight in:

write_file("notes.md", "# Today\n\n- milk")?print(read_file("notes.md")? | markdown_page({ lang: "en" }) | str | len)
output
214

See also

markdown, files, read_file, highlight

Thread
4Useful? Like it, or add to its thread below.

Thread 0 posts

On markdown_page(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo