Skip to content

Commit 40f7f85

Browse files
authored
Fix crash parsing document tree dump as markdown content (#123)
rdar://108281578
1 parent 528d49c commit 40f7f85

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Sources/Markdown/Parser/BlockDirectiveParser.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ struct PendingBlockDirective {
190190
let leadingSpacingCount = line.untrimmedText.count - textCount - trailingWhiteSpaceCount - 1
191191
innerIndentationColumnCount = leadingSpacingCount // Should we add a new property for this kind of usage?
192192

193-
let startIndex = line.untrimmedText.startIndex
194-
let endIndex = line.untrimmedText.index(startIndex, offsetBy: leadingSpacingCount)
195-
let newLine = line.untrimmedText.replacingCharacters(in: startIndex..<endIndex, with: String(repeating: " ", count: leadingSpacingCount)).dropLast(trailingWhiteSpaceCount + 1)
193+
let newLine = String(repeating: " ", count: leadingSpacingCount) + line.untrimmedText.dropFirst(leadingSpacingCount).dropLast(trailingWhiteSpaceCount + 1)
196194
pendingLine = TrimmedLine(newLine.dropFirst(0), source: line.source, lineNumber: line.lineNumber)
197195
parseState = .done
198196
endLocation = SourceLocation(line: line.lineNumber ?? 0, column: line.untrimmedText.count + 1, source: line.source)

Tests/MarkdownTests/Parsing/BlockDirectiveParserTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,4 +1020,27 @@ class BlockDirectiveArgumentParserTests: XCTestCase {
10201020
"""#
10211021
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
10221022
}
1023+
1024+
func testParsingTreeDumpFollowedByDirective() {
1025+
let source = """
1026+
Document
1027+
├─ Heading level: 1
1028+
│ └─ Text "Title"
1029+
@Comment { Line c This is a single-line comment }
1030+
"""
1031+
let documentation = Document(parsing: source, options: .parseBlockDirectives)
1032+
let expected = """
1033+
Document
1034+
├─ Paragraph
1035+
│ ├─ Text "Document"
1036+
│ ├─ SoftBreak
1037+
│ ├─ Text "├─ Heading level: 1"
1038+
│ ├─ SoftBreak
1039+
│ └─ Text "│ └─ Text “Title”"
1040+
└─ BlockDirective name: "Comment"
1041+
└─ Paragraph
1042+
└─ Text "Line c This is a single-line comment"
1043+
"""
1044+
XCTAssertEqual(expected, documentation.debugDescription())
1045+
}
10231046
}

0 commit comments

Comments
 (0)