Skip to content

Commit

Permalink
apply new line break logic to links and images
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Mar 29, 2023
1 parent 685dc9d commit 90f7dd5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ public struct MarkupFormatter: MarkupWalker {
// Image elements' source URLs can't be split. If wrapping the alt text
// of an image still put us over the line, prefer to print it on the
// next line to give as much opportunity to keep the alt text contents on one line.
if image.indexInParent > 0 && (isOverPreferredLineLimit || state.lineNumber > savedState.lineNumber) {
if image.indexInParent > 0 && (isOverPreferredLineLimit || state.effectiveLineNumber > savedState.effectiveLineNumber) {
restoreState(to: savedState)
queueNewline()
printImage()
Expand Down Expand Up @@ -842,7 +842,7 @@ public struct MarkupFormatter: MarkupWalker {
// Link elements' destination URLs can't be split. If wrapping the link text
// of a link still put us over the line, prefer to print it on the
// next line to give as much opportunity to keep the link text contents on one line.
if link.indexInParent > 0 && (isOverPreferredLineLimit || state.lineNumber > savedState.lineNumber) {
if link.indexInParent > 0 && (isOverPreferredLineLimit || state.effectiveLineNumber > savedState.effectiveLineNumber) {
restoreState(to: savedState)
queueNewline()
printRegularLink()
Expand Down
36 changes: 36 additions & 0 deletions Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,42 @@ class MarkupFormatterSimpleRoundTripTests: XCTestCase {
checkCharacterEquivalence(for: source)
}

func testRoundTripHardBreakWithImage() {
let source = """
This is some text.\(" ")
![This is an image.](image.png "")
"""
checkRoundTrip(for: source)
checkCharacterEquivalence(for: source)
}

func testRoundTripSoftBreakWithImage() {
let source = """
This is some text.
![This is an image.](image.png "")
"""
checkRoundTrip(for: source)
checkCharacterEquivalence(for: source)
}

func testRoundTripHardBreakWithLink() {
let source = """
This is some text.\(" ")
[This is a link.](https://swift.org)
"""
checkRoundTrip(for: source)
checkCharacterEquivalence(for: source)
}

func testRoundTripSoftBreakWithLink() {
let source = """
This is some text.
[This is a link.](https://swift.org)
"""
checkRoundTrip(for: source)
checkCharacterEquivalence(for: source)
}

/// Why not?
func testRoundTripReadMe() throws {
let readMeURL = URL(fileURLWithPath: #file)
Expand Down

0 comments on commit 90f7dd5

Please sign in to comment.