restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
@testable import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct MarkdownInlineParserTests {
|
||||
@Test("a link destination splits into URL and optional title", arguments: [
|
||||
("https://x.com", "https://x.com", nil),
|
||||
("https://x.com \"Home\"", "https://x.com", "Home"),
|
||||
("<a b.png> 'T'", "a b.png", "T"),
|
||||
("x.png (Paren)", "x.png", "Paren"),
|
||||
(" spaced ", "spaced", nil),
|
||||
] as [(String, String, String?)])
|
||||
func destination(raw: String, url: String, title: String?) {
|
||||
let parsed = MarkdownInlineParser.destination(Substring(raw))
|
||||
#expect(parsed.url == url)
|
||||
#expect(parsed.title == title)
|
||||
}
|
||||
|
||||
@Test("alt text flattens nested inline nodes")
|
||||
func plainText() {
|
||||
let nodes: [MarkdownInline] = [.text("a "), .strong([.code("b")]), .link(text: [.text(" c")], url: "#", title: nil)]
|
||||
#expect(MarkdownInlineParser.plainText(nodes) == "a b c")
|
||||
}
|
||||
|
||||
@Test("placeholder look-alikes in the source stay literal text")
|
||||
func placeholderLookAlike() {
|
||||
let text = "x \u{E000}99\u{E001} y"
|
||||
#expect(MarkdownInlineParser.parse(text, baseDir: "") == [.text(text)])
|
||||
}
|
||||
|
||||
@Test("unmatched markers stay literal")
|
||||
func unmatchedMarkers() {
|
||||
#expect(MarkdownInlineParser.parse("a ** b ` c [d](", baseDir: "") == [.text("a ** b ` c [d](")])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct MarkdownParserTests {
|
||||
@Test("headings, emphasis and links")
|
||||
func headingsAndInline() {
|
||||
let blocks = MarkdownParser.parse("# Title\n\nSome **bold** and *italic* and [a](https://x.com).")
|
||||
#expect(blocks == [
|
||||
.heading(level: 1, content: [.text("Title")]),
|
||||
.paragraph([
|
||||
.text("Some "), .strong([.text("bold")]), .text(" and "), .emphasis([.text("italic")]),
|
||||
.text(" and "), .link(text: [.text("a")], url: "https://x.com", title: nil), .text("."),
|
||||
]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("heading levels run from 1 to 6 and need a space", arguments: [("###### six", 6), ("## two ", 2)])
|
||||
func headingLevels(line: String, level: Int) {
|
||||
guard case let .heading(parsed, _)? = MarkdownParser.parse(line).first else {
|
||||
Issue.record("not a heading")
|
||||
return
|
||||
}
|
||||
#expect(parsed == level)
|
||||
#expect(MarkdownParser.parse("#nospace") == [.paragraph([.text("#nospace")])])
|
||||
#expect(MarkdownParser.parse("####### seven") == [.paragraph([.text("####### seven")])])
|
||||
}
|
||||
|
||||
@Test("code spans survive next to digits and are never re-read")
|
||||
func codeSpans() {
|
||||
let blocks = MarkdownParser.parse("I have 0 cats and `**code**` and 1 dog.")
|
||||
#expect(blocks == [.paragraph([.text("I have 0 cats and "), .code("**code**"), .text(" and 1 dog.")])])
|
||||
}
|
||||
|
||||
@Test("raw HTML stays literal text")
|
||||
func rawHTML() {
|
||||
#expect(MarkdownParser.parse("A <script>alert(1)</script> tag.") == [.paragraph([.text("A <script>alert(1)</script> tag.")])])
|
||||
}
|
||||
|
||||
@Test("dangerous link schemes are dropped but the text stays")
|
||||
func unsafeLinks() {
|
||||
#expect(MarkdownParser.parse("[click](javascript:alert(1))") == [.paragraph([.text("click)")])])
|
||||
#expect(MarkdownParser.parse("[click](javascript:x)") == [.paragraph([.text("click")])])
|
||||
}
|
||||
|
||||
@Test("fenced code keeps its text and resolves its language")
|
||||
func fencedCode() {
|
||||
#expect(MarkdownParser.parse("```js\nconst a = 1\n\n b\n```\nafter") == [
|
||||
.code(language: .javascript, text: "const a = 1\n\n b"),
|
||||
.paragraph([.text("after")]),
|
||||
])
|
||||
#expect(MarkdownParser.parse("```\nx") == [.code(language: nil, text: "x")])
|
||||
#expect(MarkdownParser.parse("```brainfuck\n+\n```") == [.code(language: nil, text: "+")])
|
||||
}
|
||||
|
||||
@Test("bullet and numbered lists")
|
||||
func lists() {
|
||||
#expect(MarkdownParser.parse("- a\n* b\n+ c") == [.list(ordered: false, items: [[.text("a")], [.text("b")], [.text("c")]])])
|
||||
#expect(MarkdownParser.parse("1. a\n2) b") == [.list(ordered: true, items: [[.text("a")], [.text("b")]])])
|
||||
}
|
||||
|
||||
@Test("rules need three marks of one kind")
|
||||
func rules() {
|
||||
#expect(MarkdownParser.parse("---") == [.rule])
|
||||
#expect(MarkdownParser.parse("* * *") == [.rule])
|
||||
#expect(MarkdownParser.parse("__ _") == [.rule])
|
||||
#expect(MarkdownParser.parse("-*-") != [.rule])
|
||||
}
|
||||
|
||||
@Test("block quotes nest and parse their own blocks")
|
||||
func quotes() {
|
||||
#expect(MarkdownParser.parse("> # Hi\n> > deep\nout") == [
|
||||
.quote([.heading(level: 1, content: [.text("Hi")]), .quote([.paragraph([.text("deep")])])]),
|
||||
.paragraph([.text("out")]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("paragraph lines join with a space; a blank line ends a paragraph")
|
||||
func paragraphs() {
|
||||
#expect(MarkdownParser.parse("a\n b \n\nc") == [.paragraph([.text("a b")]), .paragraph([.text("c")])])
|
||||
}
|
||||
|
||||
@Test("CRLF and bare CR read as newlines")
|
||||
func lineEndings() {
|
||||
#expect(MarkdownParser.parse("# A\r\nb\rc") == [.heading(level: 1, content: [.text("A")]), .paragraph([.text("b c")])])
|
||||
}
|
||||
|
||||
@Test("an empty document has no blocks")
|
||||
func empty() {
|
||||
#expect(MarkdownParser.parse("").isEmpty)
|
||||
#expect(MarkdownParser.parse("\n\n \n").isEmpty)
|
||||
}
|
||||
|
||||
// MARK: tables
|
||||
|
||||
@Test("a GFM table has a header, alignments and body rows with inline markup")
|
||||
func table() {
|
||||
#expect(MarkdownParser.parse("| a | b |\n|---|---|\n| 1 | `x` |\n| 2 | **y** |") == [
|
||||
.table(
|
||||
header: [[.text("a")], [.text("b")]],
|
||||
alignments: [nil, nil],
|
||||
rows: [[[.text("1")], [.code("x")]], [[.text("2")], [.strong([.text("y")])]]]
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("column alignment comes from the delimiter row")
|
||||
func alignment() {
|
||||
guard case let .table(_, alignments, _)? = MarkdownParser.parse("| l | c | r |\n| :-- | :-: | --: |\n| 1 | 2 | 3 |").first else {
|
||||
Issue.record("not a table")
|
||||
return
|
||||
}
|
||||
#expect(alignments == [.left, .center, .right])
|
||||
}
|
||||
|
||||
@Test("escaped pipes, ragged rows and prose after the table")
|
||||
func raggedRows() {
|
||||
#expect(MarkdownParser.parse("| a | b |\n|---|---|\n| x \\| y | 2 | extra |\n| short |\n\nAfter.") == [
|
||||
.table(
|
||||
header: [[.text("a")], [.text("b")]],
|
||||
alignments: [nil, nil],
|
||||
rows: [[[.text("x | y")], [.text("2")]], [[.text("short")], []]]
|
||||
),
|
||||
.paragraph([.text("After.")]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a pipe-bearing paragraph without a delimiter row is not a table")
|
||||
func notATable() {
|
||||
#expect(MarkdownParser.parse("a | b\nnot a table") == [.paragraph([.text("a | b not a table")])])
|
||||
}
|
||||
|
||||
// MARK: images
|
||||
|
||||
@Test("a relative image resolves against the document folder")
|
||||
func localImage() {
|
||||
#expect(MarkdownParser.parse("", baseDir: "docs/how-to") == [
|
||||
.paragraph([.image(alt: "Forge", source: .local(path: "docs/how-to/forge-app-config.png"), title: nil)]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("an image title stays out of the path, and .. walks up a folder")
|
||||
func imageTitle() {
|
||||
#expect(MarkdownParser.parse("", baseDir: "docs/how-to") == [
|
||||
.paragraph([.image(alt: "c", source: .local(path: "docs/img/a b.png"), title: "A title")]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a leading slash means the project root")
|
||||
func rootImage() {
|
||||
#expect(MarkdownParser.parse("", baseDir: "docs/how-to") == [
|
||||
.paragraph([.image(alt: "c", source: .local(path: "img/a.png"), title: nil)]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a remote image stays remote")
|
||||
func remoteImage() {
|
||||
#expect(MarkdownParser.parse("", baseDir: "docs") == [
|
||||
.paragraph([.image(alt: "c", source: .remote(URL(string: "https://x.com/a.png")!), title: nil)]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("a link title stays out of the URL")
|
||||
func linkTitle() {
|
||||
#expect(MarkdownParser.parse("[a](https://x.com \"Home\")") == [
|
||||
.paragraph([.link(text: [.text("a")], url: "https://x.com", title: "Home")]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("an image inside a link, and a link inside strong text")
|
||||
func nesting() {
|
||||
#expect(MarkdownParser.parse("[](https://x.com)") == [
|
||||
.paragraph([.link(text: [.image(alt: "i", source: .local(path: "i.png"), title: nil)], url: "https://x.com", title: nil)]),
|
||||
])
|
||||
#expect(MarkdownParser.parse("**see [a](#top)**") == [
|
||||
.paragraph([.strong([.text("see "), .link(text: [.text("a")], url: "#top", title: nil)])]),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("underscores inside words are not emphasis")
|
||||
func intrawordUnderscore() {
|
||||
#expect(MarkdownParser.parse("snake_case_name and _this_") == [
|
||||
.paragraph([.text("snake_case_name and "), .emphasis([.text("this")])]),
|
||||
])
|
||||
#expect(MarkdownParser.parse("__bold__ ~~gone~~") == [.paragraph([.strong([.text("bold")]), .text(" "), .strikethrough([.text("gone")])])])
|
||||
}
|
||||
|
||||
// MARK: helpers
|
||||
|
||||
@Test("safeURL keeps web, mail, anchors and relative paths only", arguments: [
|
||||
("https://x", "https://x"), ("mailto:a@b", "mailto:a@b"), ("#top", "#top"),
|
||||
("./a", "./a"), ("../a", "../a"), ("img/x.png", "img/x.png"), (" /a ", "/a"),
|
||||
])
|
||||
func safeURLs(url: String, kept: String) {
|
||||
#expect(MarkdownParser.safeURL(url) == kept)
|
||||
}
|
||||
|
||||
@Test("other schemes are unsafe", arguments: ["javascript:alert(1)", "JAVASCRIPT:x", "data:text/html,x", "file:///etc/passwd"])
|
||||
func unsafeURLs(url: String) {
|
||||
#expect(MarkdownParser.safeURL(url) == nil)
|
||||
}
|
||||
|
||||
@Test("fence info strings map to grammars")
|
||||
func fenceLanguages() {
|
||||
#expect(MarkdownParser.fenceLanguage("TS") == .typescript)
|
||||
#expect(MarkdownParser.fenceLanguage("shell") == .bash)
|
||||
#expect(MarkdownParser.fenceLanguage("") == nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user