@@ -96,4 +96,61 @@ component extends="wheels.Controller" {
9696 function saveRedirectUrl(url ) {
9797 session .redirectAfterLogin = arguments .url ;
9898 }
99+
100+ public string function generateMetaDescription (required string htmlContent ) {
101+ // Remove headings and non-paragraph wrappers
102+ var contentWithoutHeadings = reReplaceNoCase (arguments .htmlContent , " <h[1-6][^>]*>.*?</h[1-6]>" , " " , " all" );
103+
104+ // Extract only paragraph blocks
105+ var paragraphMatches = reMatchNoCase (" <p[^>]*>(.*?)</p>" , contentWithoutHeadings );
106+ var cleanText = " " ;
107+
108+ for (var para in paragraphMatches ) {
109+ // Strip inner tags from paragraph content
110+ var plainPara = trim (reReplace (para , " <[^>]*>" , " " , " all" ));
111+
112+ // Skip if the paragraph is too short or looks like a heading
113+ if (len (plainPara ) GTE 40 ) {
114+ cleanText = plainPara ;
115+ break ;
116+ }
117+ }
118+
119+ // Fallback: use stripped full content if no usable paragraph found
120+ if (cleanText == " " ) {
121+ cleanText = trim (reReplace (arguments .htmlContent , " <[^>]*>" , " " , " all" ));
122+ }
123+
124+ // Extract up to 2 sentences, avoiding decimals like 3.0 or 2.5
125+ var pattern = " [^.!?]+[.!?]" ;
126+ var pos = 1 ;
127+ var meta = " " ;
128+ var sentences = [];
129+
130+ while (pos LTE len (cleanText )) {
131+ var found = reFind (pattern , cleanText , pos , true );
132+ if (! found .len [1 ]) break ;
133+
134+ var sentence = trim (mid (cleanText , found .pos [1 ], found .len [1 ]));
135+
136+ // Skip sentence if it contains decimal numbers (e.g., 3.0)
137+ if (! reFind (" \d+\.\d+" , sentence )) {
138+ arrayAppend (sentences , sentence );
139+ }
140+
141+ pos + = found .len [1 ];
142+
143+ // Stop after 2 sentences or if length looks good
144+ if (arrayLen (sentences ) >= 2 || len (arrayToList (sentences , " " )) >= 160 ) break ;
145+ }
146+
147+ meta = arrayToList (sentences , " " );
148+
149+ // Final fallback if no valid sentences
150+ if (len (meta ) == 0 ) {
151+ meta = left (cleanText , 160 );
152+ }
153+
154+ return left (meta , 160 );
155+ }
99156}
0 commit comments