Skip to content

Commit c7c2052

Browse files
committed
Integrate GutenbergExcerptGenerator
1 parent 26c5c2b commit c7c2052

2 files changed

Lines changed: 6 additions & 64 deletions

File tree

Modules/Sources/WordPressKitModels/NSString+Summary.swift

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,19 @@ import WordPressShared
55
/// and convert HTML into plain text.
66
///
77
extension NSString {
8-
9-
static let PostDerivedSummaryLength = 150
10-
118
/// Create a summary for the post based on the post's content.
129
///
1310
/// - Returns: A summary for the post.
1411
///
1512
@objc
1613
public func wpkit_summarized() -> String {
17-
let characterSet = CharacterSet(charactersIn: "\n")
18-
19-
return (self as String).strippingGutenbergContentForExcerpt()
20-
.replacingOccurrences(of: "<br\\s*/?>", with: " ", options: .regularExpression)
21-
.strippingShortcodes()
22-
.makePlainText()
23-
.trimmingCharacters(in: characterSet)
24-
.wp_stringByEllipsizing(withMaxLength: NSString.PostDerivedSummaryLength, preserveWords: true)
14+
GutenbergExcerptGenerator.firstParagraph(from: (self as String))
2515
}
26-
}
2716

28-
private extension String {
29-
func makePlainText() -> String {
30-
let characterSet = NSCharacterSet.whitespacesAndNewlines
31-
32-
return self.strippingHTML()
17+
@objc
18+
public func wpkit_makePlainText() -> String {
19+
self.strippingHTML()
3320
.stringByDecodingXMLCharacters()
34-
.trimmingCharacters(in: characterSet)
35-
}
36-
37-
/// Creates a new string by stripping all shortcodes from this string.
38-
///
39-
func strippingShortcodes() -> String {
40-
let pattern = "\\[[^\\]]+\\]"
41-
42-
return removingMatches(pattern: pattern, options: .caseInsensitive)
43-
}
44-
45-
/// This method is the main entry point to generate excerpts for Gutenberg content.
46-
///
47-
func strippingGutenbergContentForExcerpt() -> String {
48-
return strippingGutenbergGalleries().strippingGutenbergVideoPress()
49-
}
50-
51-
/// Strips Gutenberg galleries from strings.
52-
///
53-
func strippingGutenbergGalleries() -> String {
54-
let pattern = "(?s)<!--\\swp:gallery?(.*?)wp:gallery\\s-->"
55-
56-
return removingMatches(pattern: pattern, options: .caseInsensitive)
57-
}
58-
59-
/// Strips VideoPress references from Gutenberg VideoPress and Video blocks.
60-
///
61-
func strippingGutenbergVideoPress() -> String {
62-
let pattern = "(?s)\n?<!--\\swp:video.*?(.*?)wp:video.*?\\s-->"
63-
64-
return removingMatches(pattern: pattern, options: .caseInsensitive)
65-
}
66-
67-
/// Creates a new string by removing all matches of the specified regex.
68-
///
69-
func removingMatches(pattern: String, options: NSRegularExpression.Options = []) -> String {
70-
let range = NSRange(location: 0, length: self.utf16.count)
71-
let regex: NSRegularExpression
72-
73-
do {
74-
regex = try NSRegularExpression(pattern: pattern, options: options)
75-
} catch {
76-
return self
77-
}
78-
79-
return regex.stringByReplacingMatches(in: self, options: .reportCompletion, range: range, withTemplate: "")
21+
.trimmingCharacters(in: .whitespacesAndNewlines)
8022
}
8123
}

Modules/Sources/WordPressKitObjC/RemoteReaderPost.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ - (NSString *)createSummaryFromContent:(NSString *)string
707707
*/
708708
- (NSString *)makePlainText:(NSString *)string
709709
{
710-
return [string wpkit_summarized];
710+
return [string wpkit_makePlainText];
711711
}
712712

713713
/**

0 commit comments

Comments
 (0)