Skip to content

Commit 90f7dd5

Browse files
apply new line break logic to links and images
1 parent 685dc9d commit 90f7dd5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ public struct MarkupFormatter: MarkupWalker {
805805
// Image elements' source URLs can't be split. If wrapping the alt text
806806
// of an image still put us over the line, prefer to print it on the
807807
// next line to give as much opportunity to keep the alt text contents on one line.
808-
if image.indexInParent > 0 && (isOverPreferredLineLimit || state.lineNumber > savedState.lineNumber) {
808+
if image.indexInParent > 0 && (isOverPreferredLineLimit || state.effectiveLineNumber > savedState.effectiveLineNumber) {
809809
restoreState(to: savedState)
810810
queueNewline()
811811
printImage()
@@ -842,7 +842,7 @@ public struct MarkupFormatter: MarkupWalker {
842842
// Link elements' destination URLs can't be split. If wrapping the link text
843843
// of a link still put us over the line, prefer to print it on the
844844
// next line to give as much opportunity to keep the link text contents on one line.
845-
if link.indexInParent > 0 && (isOverPreferredLineLimit || state.lineNumber > savedState.lineNumber) {
845+
if link.indexInParent > 0 && (isOverPreferredLineLimit || state.effectiveLineNumber > savedState.effectiveLineNumber) {
846846
restoreState(to: savedState)
847847
queueNewline()
848848
printRegularLink()

Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,42 @@ class MarkupFormatterSimpleRoundTripTests: XCTestCase {
708708
checkCharacterEquivalence(for: source)
709709
}
710710

711+
func testRoundTripHardBreakWithImage() {
712+
let source = """
713+
This is some text.\(" ")
714+
![This is an image.](image.png "")
715+
"""
716+
checkRoundTrip(for: source)
717+
checkCharacterEquivalence(for: source)
718+
}
719+
720+
func testRoundTripSoftBreakWithImage() {
721+
let source = """
722+
This is some text.
723+
![This is an image.](image.png "")
724+
"""
725+
checkRoundTrip(for: source)
726+
checkCharacterEquivalence(for: source)
727+
}
728+
729+
func testRoundTripHardBreakWithLink() {
730+
let source = """
731+
This is some text.\(" ")
732+
[This is a link.](https://swift.org)
733+
"""
734+
checkRoundTrip(for: source)
735+
checkCharacterEquivalence(for: source)
736+
}
737+
738+
func testRoundTripSoftBreakWithLink() {
739+
let source = """
740+
This is some text.
741+
[This is a link.](https://swift.org)
742+
"""
743+
checkRoundTrip(for: source)
744+
checkCharacterEquivalence(for: source)
745+
}
746+
711747
/// Why not?
712748
func testRoundTripReadMe() throws {
713749
let readMeURL = URL(fileURLWithPath: #file)

0 commit comments

Comments
 (0)