🔍

markdown.o

Markdown parser based on PEG (Parsing Expression Grammar). Converts Markdown text into structured O data.

Main element

Name Description / comments
.md.pars[<markdown text>] Parse markdown string and return list of dictionaries

Examples

Parse simple markdown:

o)load[getenv[`OHOME];"markdown"];
o)md: "c"$read[`$":/path/to/file.md"];
o)parsed: .md.pars md;
o)parsed
,`h1!(Hello World)
,`para!((,`text!(This is );,`bold!(bold);,`text!( text.)))

Supported markdown elements:

Element Syntax Parsed as
Heading 1 `# Text` `` `h1!(Text) ``
Heading 2 `## Text` `` `h2!(Text) ``
Heading 3-6 `### Text` ... `` `h3!(Text) `` ... `` `h6!(Text) ``
Horizontal rule `---` `` `hr!... ``
Bold `**text**` `` `bold!(text) ``
Italic `_text_` `` `curs!(text) ``
Inline code `` `code` `` `` `esc1!(code) ``
Code block ` ```langncoden``` ` `` `code!(`lang`body!(lang;code)) ``
Link `[text](url)` `` `link!(text;url) ``
Image `![alt](url)` `` `image!(alt;url) ``
List `- item` or `1. item` `` `list!(...) ``
Table `| col1 | col2 |` `` `table!(...) ``
Blockquote `> text` `` `block!(text) ``
Alert `::: notentextn:::` `` `note!(text) ``
Paragraph Plain text `` `para!(text) ``

Complete example:

o)load[getenv[`OHOME];"markdown"];
o)
o)md: "# Title\n\n",
      "This is **bold** and _italic_.\n\n",
      "
on", "a: 1 + 2;n", "\n\n", "- Item 1\n", "- Item 2\n"; o) o)parsed: .md.pars md; o)#parsed // Number of top-level elements 4 o) o)parsed[0] // First element (heading) ,`h1!(Title) o) o)parsed[1] // Second element (paragraph) ,`para!((,`text!(This is );,`bold!(bold);,`text!( and );,`curs!(italic);,`text!(.)))

PEG Grammar elements

The parser uses PEG (Parsing Expression Grammar) with the following building blocks:

Name Description
.md.concat Concatenate parsed results
.md.indent Calculate list indentation level
.md.sp Parse spaces
.md.lf Parse line feeds (\r\n or \n)
.md.h1 - .md.h6 Parse headings level 1-6
.md.hr Parse horizontal rule
.md.esc1-4 Parse escaped/code elements
.md.code Parse fenced code blocks
.md.link Parse links
.md.imge Parse images
.md.embd Parse embedded content
.md.bold Parse bold text
.md.curs Parse italic (cursive) text
.md.list Parse lists (ordered and unordered)
.md.alrt Parse alert blocks (::: note, ::: warn, etc.)
.md.blck Parse blockquotes
.md.tble Parse tables
.md.para Parse paragraphs
.md.pars Top-level parser entry point