|
| 1 | +# CommonMark |
| 2 | + |
| 3 | +A Swift package for parsing and rendering [CommonMark][commonmark]. |
| 4 | +It's built on top of [libcmark][cmark] |
| 5 | +and fully compliant with the [CommonMark Spec][commonmark]. |
| 6 | + |
| 7 | +```swift |
| 8 | +import CommonMark |
| 9 | + |
| 10 | +let markdown = #""" |
| 11 | +# [Universal Declaration of Human Rights][uhdr] |
| 12 | +
|
| 13 | +## Article 1. |
| 14 | +
|
| 15 | +All human beings are born free and equal in dignity and rights. |
| 16 | +They are endowed with reason and conscience |
| 17 | +and should act towards one another in a spirit of brotherhood. |
| 18 | +
|
| 19 | +[uhdr]: https://www.un.org/en/universal-declaration-human-rights/ "View full version" |
| 20 | +"""# |
| 21 | + |
| 22 | +let document = try Document(markdown) |
| 23 | + |
| 24 | + |
| 25 | +// Rendering to HTML, XML, LaTeX, and Manpage |
| 26 | +let html = document.render(format: .html) // <h1> [ ... ] |
| 27 | +let xml = document.render(format: .xml) // <?xml [ ... ] |
| 28 | +let latex = document.render(format: .latex) // \section{ [ ... ] |
| 29 | +let manpage = document.render(format: .manpage) // .SH [ ... ] |
| 30 | + |
| 31 | +// To get back CommonMark text, |
| 32 | +// you can either render with the `.commonmark` format... |
| 33 | +document.render(format: .commonmark) // # [Universal [ ... ] |
| 34 | +// ...or call `description` |
| 35 | +// (individual nodes also return their CommonMark representation as their description) |
| 36 | +document.description // # [Universal [ ... ] |
| 37 | + |
| 38 | + |
| 39 | +// Inspecting the document tree |
| 40 | +document.children.count // 3 |
| 41 | + |
| 42 | +let heading = document.children[0] as! Heading |
| 43 | +heading.headerLevel // 1 |
| 44 | +heading.children.count // 1 |
| 45 | + |
| 46 | +let link = heading.children[0] as! Link |
| 47 | +link.urlString // "https://www.un.org/en/universal-declaration-human-rights/") |
| 48 | +link.title // "View full version" |
| 49 | + |
| 50 | +let subheading = document.children[1] as! Heading |
| 51 | +subheading.headerLevel // 2 |
| 52 | +subheading.children.count // 1 |
| 53 | + |
| 54 | +let subheadingText = subheading.children[0] as! Text |
| 55 | +subheadingText.literal // "Article 1." |
| 56 | + |
| 57 | +let paragraph = document.children[2] as! Paragraph |
| 58 | +paragraph.description // "All human beings [ ... ]" |
| 59 | +paragraph.range.lowerBound // (line: 5, column: 1) |
| 60 | +paragraph.range.upperBound // (line: 7, column: 62) |
| 61 | +``` |
| 62 | + |
| 63 | +### CommonMark Spec Compliance |
| 64 | + |
| 65 | +This package passes all of the 649 test cases |
| 66 | +in the latest version (0.29) of the [CommonMark Spec][commonmark spec]: |
| 67 | + |
| 68 | +```console |
| 69 | +$ swift test |
| 70 | + Executed 649 tests, with 0 failures (0 unexpected) in 0.178 (0.201) seconds |
| 71 | +``` |
| 72 | + |
| 73 | +## License |
| 74 | + |
| 75 | +MIT |
| 76 | + |
| 77 | +## Contact |
| 78 | + |
| 79 | +Mattt ([@mattt](https://twitter.com/mattt)) |
| 80 | + |
| 81 | +[cmark]: https://github.com/commonmark/cmark |
| 82 | +[commonmark]: https://commonmark.org |
| 83 | +[commonmark spec]: https://spec.commonmark.org |
0 commit comments