Skip to content

Commit e217826

Browse files
committed
Add support for <br> tags to GutenbergExcerptGenerator
1 parent e5e348a commit e217826

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

Modules/Sources/WordPressShared/Utility/GutenbergExcerptGenerator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public struct GutenbergExcerptGenerator {
1414
return ""
1515
}
1616

17-
// Extract content
17+
// Extract content while convering <br>, <br/>, <br /> to newlines first
1818
let rawText = String(content[tagEnd.upperBound..<pEnd.lowerBound])
19+
.replacingOccurrences(of: "<br\\s*/?>", with: "\n", options: .regularExpression)
1920

2021
// Remove HTML tags AND shortcodes in one pass
2122
let range = NSRange(rawText.startIndex..., in: rawText)

Modules/Tests/WordPressSharedTests/GutenbergExcerptGeneratorTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ struct GutenbergPostExcerptGeneratorTests {
3131
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
3232
#expect(summary == "Before")
3333
}
34+
35+
@Test func testPostWithBRTags() {
36+
let content = #"<p class="wp-block-paragraph">Yes,<br>look behind<br>in remembrance and with gratitude.</p><p class="wp-block-paragraph">Then,<br>stay present,<br>or miss memories in the making.</p>"#
37+
38+
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
39+
print(summary)
40+
#expect(summary == "Yes, look behind in remembrance and with gratitude.")
41+
}
3442
}

0 commit comments

Comments
 (0)