@@ -5,77 +5,19 @@ import WordPressShared
55/// and convert HTML into plain text.
66///
77extension 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}
0 commit comments