From 3306b9b58c39fa35769cf3f725cf594ec292cbbf Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 23 Feb 2023 23:45:03 +0800 Subject: [PATCH 1/2] Make visitLineBreak return new line instead of space --- .../Rendering/RenderContentCompiler.swift | 2 +- .../RenderContentCompilerTests.swift | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift b/Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift index d3a717b266..f47d46a622 100644 --- a/Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift +++ b/Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift @@ -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] { diff --git a/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift b/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift index 87488caa22..91fa95aa73 100644 --- a/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift +++ b/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift @@ -131,4 +131,52 @@ 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 = #""" + This text should\ + appear on two lines. + + This text should + appear on two lines. + + This is the second paragraph. + """# + let document = Document(parsing: source) + let expectedDump = """ + Document + ├─ Paragraph + │ ├─ Text "This text should" + │ ├─ LineBreak + │ └─ Text "appear on two lines." + ├─ Paragraph + │ ├─ Text "This text should" + │ ├─ LineBreak + │ └─ Text "appear on two lines." + └─ Paragraph + └─ Text "This is the second paragraph." + """ + XCTAssertEqual(document.debugDescription(), expectedDump) + let result = document.children.flatMap { compiler.visit($0) } + XCTAssertEqual(result.count, 3) + 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) + } + } } From 16b212e15af49aef188902304298087336e9507e Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 23 Jun 2023 15:43:07 +0800 Subject: [PATCH 2/2] Update test case for more human-readable --- .../RenderContentCompilerTests.swift | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift b/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift index 91fa95aa73..e4bdd2e6a2 100644 --- a/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift +++ b/Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift @@ -137,31 +137,48 @@ class RenderContentCompilerTests: XCTestCase { var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/path", fragment: nil, sourceLanguage: .swift)) let source = #""" - This text should\ - appear on two lines. + Backslash before new line\ + is an explicit hard line break. - This text should - appear on two lines. + Two spaces before new line + is a hard line break. - This is the second paragraph. + 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 = """ + let expectedDump = #""" Document ├─ Paragraph - │ ├─ Text "This text should" + │ ├─ Text "Backslash before new line" │ ├─ LineBreak - │ └─ Text "appear on two lines." + │ └─ Text "is an explicit hard line break." ├─ Paragraph - │ ├─ Text "This text should" + │ ├─ Text "Two spaces before new line" │ ├─ LineBreak - │ └─ Text "appear on two lines." + │ └─ 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 "This is the second 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, 3) + 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)")