Yet another (and 100% unnecessary) Markdown Parser written in Swift, with a particularly simple public API. This is a proof of concept Markdown Parser showcasing how to use my own library Syntax.
You can install MarkdownSyntax via Swift Package Manager by adding the following line to your Package.swift
:
import PackageDescription
let package = Package(
[...]
dependencies: [
.package(url: "https://github.com/nerdsupremacist/MarkdownSyntax.git", from: "0.1.0")
]
)
let markdown = """
# Text
A view that displays one or more lines of read-only text.
A **text** view draws a string in your app's user interface using a
`Font/body` font that's appropriate for the current platform. You can
choose a different standard font, like `Font/title` or `Font/caption`,
using the `View/font(_:)` view modifier.
```swift
Text("Hamlet")
.font(.title)
```
![A text view showing the name "Hamlet" in a title font.](SwiftUI-Text-title.png)
"""
let document = try Markdown.parse(markdown)
You can inspect everything in the document via .elements
:
for element in markdown.elements {
switch element {
case .html(let html):
case .heading(let heading):
case .horizontalRule:
case .paragraph(let paragraph):
case .blockquote(let quote):
case .orderedList(let startIndex, let elements):
case .unorderedList(let elements):
case .image(let altText, let link):
case .codeblock(let language, let code):
case .reference(let label, let content):
}
}
MarkdownSyntax does not yet fully comply with the CommonMark Spec. Here's an overview of the features of Markdown that have been implemented. Note there might be quite a lot of special cases that are not handled yet.
- Headlines
- Levels 1-4
- Special handling for Levels 1-2 with
===
and---
lines below
- Text
- Bold
- Italics
- Links
- Inline Code
- Multibackticks to escape backticks in code
- Images
- References
- Tables
- Inline HTML
- Blockquotes
- Code Blocks
- Multibackticks to escape backticks in code
- Ordered Lists
- Unordered Lists
Contributions are welcome and encouraged!
MarkdownSyntax is available under the MIT license. See the LICENSE file for more info.