Skip to content
Open
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
9 changes: 7 additions & 2 deletions Sources/WordPressData/Swift/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ public class Post: AbstractPost {
if let preview = PostPreviewCache.shared.excerpt[excerpt] {
return preview
}
let preview = excerpt.makePlainText().withCollapsedNewlines()
let preview = excerpt.makePlainText().withCollapsedNewlines().trimmedForPreview()
PostPreviewCache.shared.excerpt[excerpt] = preview
return preview
} else if let content {
if let preview = PostPreviewCache.shared.content[content] {
return preview
}
let preview = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 200).withCollapsedNewlines()
let preview = GutenbergExcerptGenerator.firstParagraph(from: content, maxLength: 200).withCollapsedNewlines().trimmedForPreview()
PostPreviewCache.shared.content[content] = preview
return preview
} else {
Expand All @@ -253,6 +253,11 @@ private extension String {
func withCollapsedNewlines() -> String {
replacingOccurrences(of: "[\n]{2,}", with: "\n", options: .regularExpression)
}

// Remove leading/trailing line breaks to avoid rendering blank trailing lines in preview labels.
func trimmedForPreview() -> String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
}

private final class PostPreviewCache {
Expand Down
9 changes: 9 additions & 0 deletions Tests/KeystoneTests/Tests/Models/PostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ class PostTests: CoreDataTestCase {
XCTAssertEqual(post.contentPreviewForDisplay(), "some contents\u{A0}go here")
}

func testThatContentPreviewForDisplayTrimsLeadingAndTrailingNewlines() {
let post = newTestPost()

post.content = "<p>Paragraph 1</p><p>Paragraph 2</p>"
let preview = post.contentPreviewForDisplay()

XCTAssertEqual("Paragraph 1\nParagraph 2", preview)
}

func testThatEnablingDisablingPublicizeConnectionsWorks() {
let post = newTestPost()

Expand Down