V Markdown Parser
A pure V implementation of a Markdown parser that supports extended Markdown syntax and provides an easy way to navigate through the document structure.
Features
- Parses Markdown text into a structured representation
- Supports both basic and extended Markdown syntax
- Provides an easy way to navigate through the document structure
- Includes renderers for different output formats
- No external dependencies
Supported Markdown Syntax
- Headings (# to ######)
- Paragraphs
- Blockquotes
- Lists (ordered and unordered)
- Task lists
- Code blocks (fenced with language support)
- Tables with alignment
- Horizontal rules
- Footnotes
- Basic text elements (currently as plain text, with planned support for inline formatting)
Usage
Parsing Markdown
import markdownparser2
// Parse Markdown text
md_text := '# Hello World\n\nThis is a paragraph.'
doc := markdownparser2.parse(md_text)
// Access the document structure
root := doc.root
for child in root.children {
println(child.typ)
}
Navigating the Document
import markdownparser2
// Parse Markdown text
md_text := '# Hello World\n\nThis is a paragraph.'
doc := markdownparser2.parse(md_text)
// Create a navigator
mut nav := markdownparser2.new_navigator(doc)
// Find elements by type
headings := nav.find_all_by_type(.heading)
for heading in headings {
level := heading.attributes['level']
println('Heading level ${level}: ${heading.content}')
}
// Find elements by content
if para := nav.find_by_content('paragraph') {
println('Found paragraph: ${para.content}')
}
// Navigate through the document
if first_heading := nav.find_by_type(.heading) {
println('First heading: ${first_heading.content}')
// Move to next sibling
if next := nav.next_sibling() {
println('Next element after heading: ${next.typ}')
}
}
Rendering the Document
import markdownparser2
// Parse Markdown text
md_text := '# Hello World\n\nThis is a paragraph.'
// Render as structure (for debugging)
structure := markdownparser2.to_structure(md_text)
println(structure)
// Render as plain text
plain_text := markdownparser2.to_plain(md_text)
println(plain_text)
Element Types
The parser recognizes the following element types:
document: The root element of the documentheading: A heading element (h1-h6)paragraph: A paragraph of textblockquote: A blockquotecode_block: A code blocklist: A list (ordered or unordered)list_item: An item in a listtask_list_item: A task list item with checkboxtable: A tabletable_row: A row in a tabletable_cell: A cell in a tablehorizontal_rule: A horizontal rulefootnote: A footnote definitionfootnote_ref: A reference to a footnotetext: A text elementlink,image,emphasis,strong,strikethrough,inline_code: Inline formatting elements (planned for future implementation)
Element Structure
Each Markdown element has the following properties:
typ: The type of the elementcontent: The text content of the elementchildren: Child elementsattributes: Additional attributes specific to the element typeline_number: The line number where the element starts in the sourcecolumn: The column number where the element starts in the source
Future Improvements
- Implement parsing of inline elements (bold, italic, links, etc.)
- Add HTML renderer
- Support for more extended Markdown syntax
- Performance optimizations