Skip to content
Merged
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