Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Sources/SwiftParser/Languages/MarkdownLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,17 @@ public struct MarkdownLanguage: CodeLanguage {
func add(_ t: Token) { tokens.append(t) }
while index < input.endIndex {
let ch = input[index]
if ch == "#" {
if ch == "\\" {
let start = index
advance()
if index < input.endIndex {
let escaped = input[index]
advance()
add(.text(String(escaped), start..<index))
} else {
add(.text("\\", start..<index))
}
} else if ch == "#" {
let start = index
advance()
add(.hash(start..<index))
Expand Down Expand Up @@ -215,7 +225,7 @@ public struct MarkdownLanguage: CodeLanguage {
let start = index
while index < input.endIndex &&
input[index] != "\n" &&
!"#-*+_`[].()<>!~|;&=".contains(input[index]) &&
!"#-*+_`[].()<>!~|;&=\\".contains(input[index]) &&
!input[index].isNumber {
advance()
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/SwiftParserTests/SwiftParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ final class SwiftParserTests: XCTestCase {
XCTAssertEqual(result.root.children.first?.type as? MarkdownLanguage.Element, .image)
}

func testMarkdownEscapedCharacters() {
let parser = SwiftParser()
let source = "\\*not italic\\*"
let result = parser.parse(source, language: MarkdownLanguage())
XCTAssertEqual(result.errors.count, 0)
XCTAssertEqual(result.root.children.count, 1)
XCTAssertEqual(result.root.children.first?.value, "*not italic*")
}

func testPrattExpression() {
let parser = SwiftParser()
let source = "x = 1 + 2 * 3"
Expand Down