apparata/Markin
Swift library for parsing a Markdown-like text format.
License
Markin is available under the Zero-Clause BSD license. See the LICENSE file in the repository for details.
Usage
Parsing
Simply instantiate a parser and call the parse() method with a string in Markin format as a parameter. The parse() method returns a DocumentElement object, which is the root object of the element tree.
let exampleMarkin = """
# This is a Header
This is a paragraph.
"""
do {
let parser = MarkinParser()
let document = try parser.parse(exampleMarkin)
} catch {
print(error)
}Rendering HTML from Element Tree
let document = try parser.parse(exampleMarkin)
let html = document.formatAsHTML()Back to Markin from Element Tree
let document = try parser.parse(exampleMarkin)
let markin = document.formatAsMarkin()Element Tree to JSON
let document = try parser.parse(exampleMarkin)
let jsonData = try JSONEncoder().encode(document)JSON to Element Tree
let document = try JSONDecoder().decode(DocumentElement.self, from: jsonData)The Markin Format
The format is based on Markdown, but I have plans to extend it. The core syntax will stay the same, however.
Headers
Headers are available in 6 hierarchical levels:
# This is the largest headerThis is the second largest header
This is the third largest header
This is the fourth largest header
This is the fifth largest header
####### This is the sixth largest header.
### Table of Contents
A place holder for where the table of contents should be rendered is written like this on its own separate line:
%TOC
### Text Paragraphs
A text paragraph consists of consecutive lines of text, terminated by a blank line.
This is the first sentence of the paragraph. This is the second sentence. This is the third sentence, also in the paragraph.
This is a second paragraph.
### Block Quotes
A block quote is formatted as text paragraphs, but each line is prefixed with a > in the following manner:
This is the first line of the block quote. This is the second line of the block quote.
This is the first line of the second paragraph of the block quote.
### Lists
Unordered list:
- First list entry
- Second list entry
- First nested list entry - Second nested list entry
- Third list entry
Ordered lists:
- First list entry
- Second list entry
1. First nested list entry 1. Second nested list entry
- Third list entry
### Horizontal Rule
A horizontal divider line is written as three dashes on a separate line:
### Code Blocks
Code can be written in code blocks. The language can be specified after the opening ``` sequence.
<pre><code>```Swift
let a = 7Bold Text
Bold text is achieved by using the marker \* as follows:
The word *bold* is bold in this sentence.Currently, bold and italic text can't be nested.
Italic Text
Italic text is achieved by using the marker \_ as follows:
The word _italic_ is in italics in this sentence.Currently, bold and italic text can't be nested.
Inline Code
Code can be written inline using single backticks \` like this:
This is text that has `inline code` in it.Links
Links can be written on the form \caption like this:
This is text that has a [link](https://google.com) in it.Images
Images can be written on the form \!\caption like this:
This is text that has an image  in it.Inheritance Tree of Elements
MarkinElement
- DocumentElement - BlockElement - BlockQuoteElement - CodeBlockElement - HeaderElement - HorizontalRuleElement - ListElement - ParagraphElement - TableOfContentsElement - InlineElement - BoldElement - CodeElement - ImageElement - ItalicElement - LinkElement - TextElement
Experimental SwiftUI Renderer
You can try the experimental SwiftUI renderer by adding a MarkinView to your view.
struct ContentView: View {
let document: DocumentElement
var body: some View {
MarkinView(document: document)
}
}Package Metadata
Repository: apparata/Markin
Stars: 8
Forks: 0
Open issues: 0
Default branch: master
Primary language: swift
License: 0BSD
Topics: swift-package
README: README.md