Skip to content

Commit 49beeeb

Browse files
authored
Fix whitespace issues with block directive formatting (#177)
Issues: * top-level/starting block directive has unecessary preceding newline * top-level block directive is unexpectedly indented * nested block directive child has unecessary newline before "}" Example snippet: ```swift import Markdown print(Document( BlockDirective(name: "Metadata", children: [ BlockDirective(name: "TitleHeading", argumentText: "Example") ]) ).format()) ``` Expected output: ``` @metadata { @TitleHeading(Example) } ``` Actual output (before this commit): ``` @metadata { @TitleHeading(Example) } ```
1 parent 907674c commit 49beeeb

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ public struct MarkupFormatter: MarkupWalker {
463463
} else if let numeralPrefix = numeralPrefix(for: parentListItem) {
464464
prefix += String(repeating: " ", count: numeralPrefix.count)
465465
}
466-
} else if element is BlockDirective {
466+
}
467+
if element.parent is BlockDirective {
467468
prefix += " "
468469
}
469470
}
@@ -1105,7 +1106,9 @@ public struct MarkupFormatter: MarkupWalker {
11051106
}
11061107

11071108
public mutating func visitBlockDirective(_ blockDirective: BlockDirective) {
1108-
ensurePrecedingNewlineCount(atLeast: 1)
1109+
if blockDirective.indexInParent > 0 {
1110+
ensurePrecedingNewlineCount(atLeast: 2)
1111+
}
11091112
print("@", for: blockDirective)
11101113
print(blockDirective.name, for: blockDirective)
11111114

@@ -1124,13 +1127,13 @@ public struct MarkupFormatter: MarkupWalker {
11241127

11251128
if blockDirective.childCount > 0 {
11261129
print(" {", for: blockDirective)
1130+
queueNewline()
11271131
}
11281132

11291133
descendInto(blockDirective)
11301134

1131-
queueNewline()
1132-
11331135
if blockDirective.childCount > 0 {
1136+
queueNewline()
11341137
print("}", for: blockDirective)
11351138
}
11361139
}

Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@ class MarkupFormatterSingleElementTests: XCTestCase {
376376
)).format()
377377
XCTAssertEqual(expected, printed)
378378
}
379+
380+
func testPrintBlockDirective() {
381+
let expected = #"""
382+
@Metadata {
383+
@TitleHeading(Example)
384+
}
385+
"""#
386+
let printed = BlockDirective(name: "Metadata", children: [
387+
BlockDirective(name: "TitleHeading", argumentText: "Example"),
388+
]).format()
389+
XCTAssertEqual(expected, printed)
390+
}
379391
}
380392

381393
/// Tests that formatting options work correctly.
@@ -1533,10 +1545,65 @@ class MarkupFormatterTableTests: XCTestCase {
15331545
"""
15341546

15351547
XCTAssertEqual(expected, formatted)
1536-
print(formatted)
15371548

15381549
let reparsed = Document(parsing: formatted)
1539-
print(reparsed.debugDescription())
15401550
XCTAssertTrue(document.hasSameStructure(as: reparsed))
15411551
}
15421552
}
1553+
1554+
class MarkupFormatterMixedContentTests: XCTestCase {
1555+
func testMixedContentWithBlockDirectives() {
1556+
let expected = [
1557+
#"""
1558+
# Example title
1559+
1560+
@Metadata {
1561+
@TitleHeading(example)
1562+
}
1563+
"""#,
1564+
#"""
1565+
@Tutorials(name: Foo) {
1566+
@Intro(title: Bar) {
1567+
Foobar
1568+
1569+
@Image(source: foo, alt: bar)
1570+
}
1571+
}
1572+
"""#,
1573+
#"""
1574+
# Example title
1575+
1576+
@Links(visualStyle: list) {
1577+
- ``Foo``
1578+
- ``Bar``
1579+
}
1580+
"""#,
1581+
]
1582+
let printed = [
1583+
Document(
1584+
Heading(level: 1, Text("Example title")),
1585+
BlockDirective(name: "Metadata", children: [
1586+
BlockDirective(name: "TitleHeading", argumentText: "example"),
1587+
])
1588+
).format(),
1589+
Document(
1590+
BlockDirective(name: "Tutorials", argumentText: "name: Foo", children: [
1591+
BlockDirective(name: "Intro", argumentText: "title: Bar", children: [
1592+
Paragraph(Text("Foobar")) as BlockMarkup,
1593+
BlockDirective(name: "Image", argumentText: "source: foo, alt: bar") as BlockMarkup,
1594+
]),
1595+
])
1596+
).format(),
1597+
Document(
1598+
Heading(level: 1, Text("Example title")),
1599+
BlockDirective(name: "Links", argumentText: "visualStyle: list", children: [
1600+
UnorderedList([
1601+
ListItem(Paragraph(SymbolLink(destination: "Foo"))),
1602+
ListItem(Paragraph(SymbolLink(destination: "Bar"))),
1603+
]),
1604+
])
1605+
).format(),
1606+
]
1607+
zip(expected, printed).forEach { XCTAssertEqual($0, $1) }
1608+
}
1609+
}

Tests/MarkdownTests/Visitors/MarkupTreeDumperTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ final class MarkupTreeDumperTests: XCTestCase {
9595
└─ HTMLBlock @42:1-42:90 #71
9696
<!-- Copyright (c) 2021 Apple Inc and the Swift Project authors. All Rights Reserved. -->
9797
"""
98-
print(everythingDocument.debugDescription(options: [.printEverything]))
9998
XCTAssertEqual(expectedDump, everythingDocument.debugDescription(options: [.printEverything]))
10099
}
101100
}

0 commit comments

Comments
 (0)