@@ -99,6 +99,45 @@ def md5(input)
9999 end
100100 Liquid ::Template . register_filter ( MDhash )
101101
102+ # Generate a description from page content
103+ module DescriptionGenerator
104+ def describe ( input , max_length = 160 )
105+ return "" if input . nil? || input . to_s . strip . empty?
106+
107+ # Convert Markdown to HTML
108+ html = $site. find_converter_instance ( ::Jekyll ::Converters ::Markdown ) . convert ( input . to_s )
109+
110+ # Parse HTML and extract text
111+ doc = Nokogiri ::HTML5 . fragment ( html )
112+
113+ # Remove the page's title (i.e., first h1 tag)
114+ doc . css ( "h1" ) . first &.remove
115+
116+ # Remove any table of contents
117+ doc . css ( "ul#markdown-toc" ) . first &.remove
118+
119+ # Strip tags
120+ text = doc . text . strip
121+
122+ # Clean up whitespace
123+ text = text . gsub ( /\s +/ , " " ) . strip
124+
125+ # Return empty string if no text extracted
126+ return "" if text . empty?
127+
128+ # Truncate to max_length, breaking at word boundary
129+ if text . length > max_length
130+ text = text [ 0 ...( max_length - 3 ) ]
131+ last_space = text . rindex ( " " )
132+ text = text [ 0 ...last_space ] if last_space && last_space > 0
133+ text += "..."
134+ end
135+
136+ text
137+ end
138+ end
139+ Liquid ::Template . register_filter ( DescriptionGenerator )
140+
102141 module Mixins
103142
104143 def initialize ( tag_name , markup , options )
0 commit comments