Skip to content

Commit 26c5c2b

Browse files
committed
GutenbergExcerptGenerator to always add ellipsis even for shorter posts
1 parent d4f95f0 commit 26c5c2b

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

Modules/Sources/WordPressShared/Utility/GutenbergExcerptGenerator.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ public struct GutenbergExcerptGenerator {
1818
let rawText = String(content[tagEnd.upperBound..<pEnd.lowerBound])
1919
.replacingOccurrences(of: "<br\\s*/?>", with: " ", options: .regularExpression)
2020

21-
// Remove HTML tags AND shortcodes in one pass
2221
let range = NSRange(rawText.startIndex..., in: rawText)
23-
let text = (regex?.stringByReplacingMatches(in: rawText, options: [], range: range, withTemplate: "") ?? rawText)
22+
var text = (regex?.stringByReplacingMatches(in: rawText, options: [], range: range, withTemplate: "") ?? rawText)
2423
.stringByDecodingXMLCharacters()
2524
.trimmingCharacters(in: .whitespacesAndNewlines)
2625

27-
// Truncate if needed
28-
if text.count <= maxLength {
29-
return text
26+
if text.count > maxLength {
27+
let truncated = String(text.prefix(maxLength))
28+
text = truncated.lastIndex(of: " ").map { String(truncated[..<$0]) } ?? truncated
3029
}
3130

32-
let truncated = String(text.prefix(maxLength))
33-
if let lastSpace = truncated.lastIndex(of: " ") {
34-
return String(truncated[..<lastSpace]) + ""
31+
if text.hasSuffix(".") {
32+
text = String(text.dropLast())
3533
}
36-
return truncated + ""
34+
return text + ""
3735
}
3836
}

Modules/Tests/WordPressSharedTests/GutenbergExcerptGeneratorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ struct GutenbergPostExcerptGeneratorTests {
1414
let content = "<!-- wp:gallery {\"ids\":[2315,2309,2308]} --><figure class=\"wp-block-gallery columns-3 is-cropped\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0005-1-1.jpg\" data-id=\"2315\" class=\"wp-image-2315\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0111-1-1.jpg\" data-id=\"2309\" class=\"wp-image-2309\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0004-1.jpg\" data-id=\"2308\" class=\"wp-image-2308\"/><figcaption class=\"blocks-gallery-item__caption\">Adsasdasdasd</figcaption></figure></li></ul></figure><!-- /wp:gallery --><p>Some Content</p>"
1515

1616
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
17-
#expect(summary == "Some Content")
17+
#expect(summary == "Some Content")
1818
}
1919

2020
@Test func summaryForContentWithGallery2() {
2121
let content = "<p>Before</p>\n<!-- wp:gallery {\"ids\":[2315,2309,2308]} --><figure class=\"wp-block-gallery columns-3 is-cropped\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0005-1-1.jpg\" data-id=\"2315\" class=\"wp-image-2315\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0111-1-1.jpg\" data-id=\"2309\" class=\"wp-image-2309\"/><figcaption class=\"blocks-gallery-item__caption\">Asdasdasd</figcaption></figure></li><li class=\"blocks-gallery-item\"><figure><img src=\"https://diegotest4.files.wordpress.com/2020/01/img_0004-1.jpg\" data-id=\"2308\" class=\"wp-image-2308\"/><figcaption class=\"blocks-gallery-item__caption\">Adsasdasdasd</figcaption></figure></li></ul></figure><!-- /wp:gallery --><p>After</p>"
2222

2323
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
24-
#expect(summary == "Before")
24+
#expect(summary == "Before")
2525
}
2626

2727
@Test
2828
func testVideoPressBlock() {
2929
let content = "<p>Before</p>\n<!-- wp:videopress/video {\"title\":\"demo\",\"description\":\"\",\"id\":5297,\"guid\":\"AbCDe\",\"videoRatio\":56.333333333333336,\"privacySetting\":2,\"allowDownload\":false,\"rating\":\"G\",\"isPrivate\":true,\"duration\":1673} -->\n<figure class=\"wp-block-videopress-video wp-block-jetpack-videopress jetpack-videopress-player\"><div class=\"jetpack-videopress-player__wrapper\">\nhttps://videopress.com/v/AbCDe?resizeToParent=true&amp;cover=true&amp;preloadContent=metadata&amp;useAverageColor=true\n</div></figure>\n<!-- /wp:videopress/video -->\n<p>After</p>"
3030

3131
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
32-
#expect(summary == "Before")
32+
#expect(summary == "Before")
3333
}
3434

3535
@Test func testPostWithBRTags() {
3636
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>"#
3737

3838
let summary = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 150)
3939
print(summary)
40-
#expect(summary == "Yes, look behind in remembrance and with gratitude.")
40+
#expect(summary == "Yes, look behind in remembrance and with gratitude")
4141
}
4242
}

0 commit comments

Comments
 (0)