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

Make visitLineBreak return new line instead of space #633

Merged
merged 2 commits into from
Jun 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ struct RenderContentCompiler: MarkupVisitor {
}

mutating func visitLineBreak(_ lineBreak: LineBreak) -> [RenderContent] {
return [RenderInlineContent.text(" ")]
return [RenderInlineContent.text("\n")]
}

mutating func visitEmphasis(_ emphasis: Emphasis) -> [RenderContent] {
Expand Down
65 changes: 65 additions & 0 deletions Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,69 @@ class RenderContentCompilerTests: XCTestCase {
XCTAssertEqual(paragraph, RenderBlockContent.Paragraph(inlineContent: [link]))
}
}

func testLineBreak() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift))

let source = #"""
Kyle-Ye marked this conversation as resolved.
Show resolved Hide resolved
Backslash before new line\
is an explicit hard line break.

Two spaces before new line
is a hard line break.

Paragraph can't end with hard line break.\

# Headings can't end with hard line break.\

Code blocks ignore\
hard line breaks.

A single space before new line
is a soft line break.
"""#
let document = Document(parsing: source)
let expectedDump = #"""
Document
├─ Paragraph
│ ├─ Text "Backslash before new line"
│ ├─ LineBreak
│ └─ Text "is an explicit hard line break."
├─ Paragraph
│ ├─ Text "Two spaces before new line"
│ ├─ LineBreak
│ └─ Text "is a hard line break."
├─ Paragraph
│ └─ Text "Paragraph can’t end with hard line break.\"
├─ Heading level: 1
│ └─ Text "Headings can’t end with hard line break.\"
├─ CodeBlock language: none
│ Code blocks ignore\
│ hard line breaks.
└─ Paragraph
├─ Text "A single space before new line"
├─ SoftBreak
└─ Text "is a soft line break."
"""#
XCTAssertEqual(document.debugDescription(), expectedDump)
let result = document.children.flatMap { compiler.visit($0) }
XCTAssertEqual(result.count, 6)
do {
guard case let .paragraph(paragraph) = result[0] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("\n")
XCTAssertEqual(paragraph.inlineContent[1], text)
}
do {
guard case let .paragraph(paragraph) = result[1] as? RenderBlockContent else {
XCTFail("RenderCotent result is not the expected RenderBlockContent.paragraph(Paragraph)")
return
}
let text = RenderInlineContent.text("\n")
XCTAssertEqual(paragraph.inlineContent[1], text)
}
}
}