Md0: Simple Markdown Subset

2 hours ago 1

md0 is a format for hypertext documents. It is designed to be easy to parse and render, and is a proper subset of markdown. The main goal is to make it easier to share text-based content online, for authors, readers, and tool builders. The original motivation is to create a format that can be easily consumed by resource-constrained devices. The following image shows this file rendered by the ORBIT browser on the Playdate console:

md0 on Playdate md0 on Playdate

If you are familiar with gemtext, think of md0 as that but with support for inline links and also happens to be valid markdown (so you can convert it to different formats with existing tools).

md0 consists of two simple extensions to plain text: links and images, as follows:

  • a link ref takes the form [word][n] where word is a sequence of characters, none of which is a space, an opening bracket [, or a closing bracket ], and n is a positive integer (non-zero). There must be a space between a link ref and any preceeding text, while the space can be omitted after the link ref. This is to support cases like this [link][2], where punctuation follows immediately after the link.
  • a link def takes the form [n]: link where n is a positive integer and link is a sequence of non-space characters. Each link def must go on its own line, and all link refs must come after the main text content.
  • an image ref takes the form ![alt text][n] where alt text is a sequence of characters, none of which is an opening bracket [, or a closing bracket ], and n is a positive integer. Link defs for images take the same form as above. Every image ref must be on its own line.

All other text is treated as plain text. This document is a valid md0 file despite the use of bold, code syntax from full markdown - those will simply be rendered verbatim.

Text is rendered line-by-line, and each line is rendered word-by-word (words are delimited by spaces), so consequtive spaces will be treated as a single space (but consequtive lines will be preserved). A link ref should be rendered in a way to distinguish it from plain text. This includes rendering it as-is ([word][n] already indicates its is a link), as word with an underline, in a different font, with a different background color, etc.

A reference Python layout implementation is available in render.py. It lays out md0 text with word wrapping at a maximum of 50 characters per line and returns the laid out text along with a dictionary mapping link numbers to their positions. You can also check out the rendering code of ORBIT (in Lua) which shows how single-word link text makes it trivial to draw the link and caculate hit boxes. But the simplest way to render md0 is just to print it verbatim and only make the link defs clickable - using IDs instead of the link itself in refs makes the raw text readable as-is.

Read Entire Article