Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash parsing document tree dump as markdown content #123

Merged
merged 1 commit into from Apr 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/Markdown/Parser/BlockDirectiveParser.swift
Expand Up @@ -190,9 +190,7 @@ struct PendingBlockDirective {
let leadingSpacingCount = line.untrimmedText.count - textCount - trailingWhiteSpaceCount - 1
innerIndentationColumnCount = leadingSpacingCount // Should we add a new property for this kind of usage?

let startIndex = line.untrimmedText.startIndex
let endIndex = line.untrimmedText.index(startIndex, offsetBy: leadingSpacingCount)
let newLine = line.untrimmedText.replacingCharacters(in: startIndex..<endIndex, with: String(repeating: " ", count: leadingSpacingCount)).dropLast(trailingWhiteSpaceCount + 1)
let newLine = String(repeating: " ", count: leadingSpacingCount) + line.untrimmedText.dropFirst(leadingSpacingCount).dropLast(trailingWhiteSpaceCount + 1)
pendingLine = TrimmedLine(newLine.dropFirst(0), source: line.source, lineNumber: line.lineNumber)
parseState = .done
endLocation = SourceLocation(line: line.lineNumber ?? 0, column: line.untrimmedText.count + 1, source: line.source)
Expand Down
23 changes: 23 additions & 0 deletions Tests/MarkdownTests/Parsing/BlockDirectiveParserTests.swift
Expand Up @@ -1020,4 +1020,27 @@ class BlockDirectiveArgumentParserTests: XCTestCase {
"""#
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
}

func testParsingTreeDumpFollowedByDirective() {
let source = """
Document
├─ Heading level: 1
│ └─ Text "Title"
@Comment { Line c This is a single-line comment }
"""
let documentation = Document(parsing: source, options: .parseBlockDirectives)
let expected = """
Document
├─ Paragraph
│ ├─ Text "Document"
│ ├─ SoftBreak
│ ├─ Text "├─ Heading level: 1"
│ ├─ SoftBreak
│ └─ Text "│ └─ Text “Title”"
└─ BlockDirective name: "Comment"
└─ Paragraph
└─ Text "Line c This is a single-line comment"
"""
XCTAssertEqual(expected, documentation.debugDescription())
}
}