- Notifications
You must be signed in to change notification settings - Fork12
Create, parse, and render Markdown text according to the CommonMark specification
License
SwiftDocOrg/CommonMark
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Swift package for working withCommonMark text.It's built on top oflibcmarkand fully compliant with theCommonMark Spec.
import CommonMarkletmarkdown=#"""# [Universal Declaration of Human Rights][udhr]## Article 1.All human beings are born free and equal in dignity and rights.They are endowed with reason and conscienceand should act towards one another in a spirit of brotherhood.[udhr]: https://www.un.org/en/universal-declaration-human-rights/ "View full version""""#letdocument=tryDocument(markdown)
document.children.count // 3letheading= document.children[0]as!Headingheading.headerLevel // 1heading.children.count // 1letlink= heading.children[0]as!Linklink.urlString // "https://www.un.org/en/universal-declaration-human-rights/")link.title // "View full version"letsubheading= document.children[1]as!Headingsubheading.headerLevel // 2subheading.children.count // 1letsubheadingText= subheading.children[0]as!TextsubheadingText.literal // "Article 1."letparagraph= document.children[2]as!Paragraphparagraph.description // "All human beings [ ... ]"paragraph.range.lowerBound // (line: 5, column: 1)paragraph.range.upperBound // (line: 7, column: 62)
lethtml= document.render(format:.html) // <h1> [ ... ]letxml= document.render(format:.xml) // <?xml [ ... ]letlatex= document.render(format:.latex) // \section{ [ ... ]letmanpage= document.render(format:.manpage) // .SH [ ... ]// To get back CommonMark text, // you can either render with the `.commonmark` format...document.render(format:.commonmark) // # [Universal [ ... ]// ...or call `description`// (individual nodes also return their CommonMark representation as their description)document.description // # [Universal [ ... ]
In Swift 5.4 and later,you can create CommonMark documents using@resultBuilder
initializers.
import CommonMarkletdocument=Document{Heading{Link(urlString:"https://www.un.org/en/universal-declaration-human-rights/", title:"View full version"){"Universal Declaration of Human Rights"}}Section{ // sections increase the level of contained headingsHeading{"Article 1."} // this is a second-level heading} // block-level strings are parsed as CommonMark literals""" **All** human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood."""}
The following code produces the same result as the preceding example,using conventional Swift initializers.
letlink=Link(urlString:"https://www.un.org/en/universal-declaration-human-rights/", title:"View full version", text:"Universal Declaration of Human Rights")letheading=Heading(level:1, children:[link])letsubheading=Heading(level:2, text:"Article 1.")letparagraph=Paragraph(children:#"""All human beings are born free and equal in dignity and rights.They are endowed with reason and conscienceand should act towards one another in a spirit of brotherhood."""#.split(separator:"\n").flatMap{[Text(String($0)),SoftLineBreak()]})Document(children:[heading, subheading, paragraph]).description== document.description // true
This package passes all of the 649 test casesin the latest version (0.29) of theCommonMark Spec:
$swifttest Executed 649 tests, with 0 failures (0 unexpected) in 0.178 (0.201) seconds
- Swift 5.1+
Add the CommonMark package to your target dependencies inPackage.swift
:
import PackageDescriptionletpackage=Package( name:"YourProject", dependencies:[.package( url:"https://github.com/SwiftDocOrg/CommonMark", from:"0.5.1"),])
Then run theswift build
command to build your project.
MIT
Mattt (@mattt)
About
Create, parse, and render Markdown text according to the CommonMark specification
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.