@@ -199,33 +199,30 @@ function getMeta(doc: Document, name: string): string | undefined {
199199 return el ?. getAttribute ( "content" ) ?. trim ( ) || undefined ;
200200}
201201
202+ function firstMeta ( doc : Document , names : string [ ] ) : string | undefined {
203+ for ( const name of names ) {
204+ const value = getMeta ( doc , name ) ;
205+ if ( value ) return value ;
206+ }
207+ return undefined ;
208+ }
209+
202210export function extractMetadata ( html : string , url : string ) : PageMetadata {
203211 const { document } = parseHTML ( html ) ;
204212 const result : PageMetadata = { } ;
205213
206- const title = getMeta ( document , "og:title" )
207- || getMeta ( document , "twitter:title" )
214+ const title = firstMeta ( document , [ "og:title" , "twitter:title" ] )
208215 || document . querySelector ( "title" ) ?. textContent ?. trim ( )
209216 || undefined ;
210217 if ( title ) result . title = title ;
211218
212- const author = getMeta ( document , "author" )
213- || getMeta ( document , "article:author" )
214- || getMeta ( document , "og:article:author" )
215- || undefined ;
219+ const author = firstMeta ( document , [ "author" , "article:author" , "og:article:author" ] ) ;
216220 if ( author ) result . author = author ;
217221
218- const publishedDate = getMeta ( document , "article:published_time" )
219- || getMeta ( document , "og:article:published_time" )
220- || getMeta ( document , "date" )
221- || getMeta ( document , "pubdate" )
222- || undefined ;
222+ const publishedDate = firstMeta ( document , [ "article:published_time" , "og:article:published_time" , "date" , "pubdate" ] ) ;
223223 if ( publishedDate ) result . publishedDate = publishedDate ;
224224
225- const description = getMeta ( document , "description" )
226- || getMeta ( document , "og:description" )
227- || getMeta ( document , "twitter:description" )
228- || undefined ;
225+ const description = firstMeta ( document , [ "description" , "og:description" , "twitter:description" ] ) ;
229226 if ( description ) result . description = description ;
230227
231228 const siteName = getMeta ( document , "og:site_name" ) || undefined ;
0 commit comments