Don't return more than the first line on short titleless posts

This commit is contained in:
Angelo Stavrow 2020-11-06 16:51:41 -05:00
parent d989709319
commit cd6f8adbbb
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -96,9 +96,7 @@ private extension PostListModel {
}
func extractLede(from string: String) -> String {
if string.utf16.count <= 80 {
return string
}
if string.isEmpty { return string }
let truncatedString = string.prefix(80)
let terminatingCharacters = CharacterSet(charactersIn: ".。?").union(.newlines)
@ -109,13 +107,13 @@ private extension PostListModel {
let sentences = truncatedString.components(separatedBy: terminatingCharacters)
let firstSentence = sentences.filter { !$0.isEmpty }[0]
if firstSentence == truncatedString {
if firstSentence == truncatedString && string.utf16.count > 80 {
let endOfStringIndex = truncatedString.lastIndex(of: " ")
lede = String(
truncatedString[..<(endOfStringIndex ?? truncatedString.index(truncatedString.endIndex, offsetBy: -2))]
) + ""
} else {
lede = String(truncatedString[...firstSentence.endIndex])
lede = String(truncatedString[..<firstSentence.endIndex])
}
return lede