@@ -36,7 +36,7 @@ import {
3636 createMarkdownPipeline ,
3737 MarkdownPipeline ,
3838} from "./website-pipeline-md.ts" ;
39- import { findPreviewImg } from "./util/discover-meta.ts" ;
39+ import { findDescription , findPreviewImg } from "./util/discover-meta.ts" ;
4040import { isAbsoluteRef } from "../../../core/http.ts" ;
4141import {
4242 kHtmlEmptyPostProcessResult ,
@@ -45,6 +45,7 @@ import { HtmlPostProcessResult } from "../../../command/render/types.ts";
4545import { imageSize } from "../../../core/image.ts" ;
4646import { writeMetaTag } from "../../../format/html/format-html-shared.ts" ;
4747import { joinUrl } from "../../../core/url.ts" ;
48+ import { truncateText } from "../../../core/text.ts" ;
4849
4950const kCard = "card" ;
5051
@@ -53,6 +54,7 @@ interface SocialMetadataProvider {
5354 prefix : string ;
5455 metadata : Metadata ;
5556 filter ?: ( key : string ) => string ;
57+ resolveValue ?: ( key : string , value : string ) => string ;
5658 resolveDefaults ?: ( finalMetadata : Metadata ) => void ;
5759}
5860
@@ -101,6 +103,14 @@ export function metadataHtmlPostProcessor(
101103 }
102104 return key ;
103105 } ,
106+ resolveValue : ( key : string , value : string ) => {
107+ // Limit to 300 chars for Open Graph
108+ if ( [ kDescription ] . includes ( key ) ) {
109+ return truncateText ( value , 300 ) ;
110+ }
111+
112+ return value ;
113+ } ,
104114 } ;
105115
106116 // The twitter card provider
@@ -122,6 +132,14 @@ export function metadataHtmlPostProcessor(
122132
123133 return key ;
124134 } ,
135+ resolveValue : ( key : string , value : string ) => {
136+ // Limit to 200 chars for Twitter
137+ if ( [ kDescription ] . includes ( key ) ) {
138+ return truncateText ( value , 200 ) ;
139+ }
140+
141+ return value ;
142+ } ,
125143 resolveDefaults : ( finalMetadata : Metadata ) => {
126144 if ( finalMetadata [ kCardStyle ] === undefined ) {
127145 finalMetadata [ kCardStyle ] = finalMetadata [ kImage ]
@@ -156,6 +174,11 @@ export function metadataHtmlPostProcessor(
156174 metadata [ kImage ] = findPreviewImg ( doc ) ;
157175 }
158176
177+ // cook up a description if one is not provided
178+ if ( metadata [ kDescription ] === undefined ) {
179+ metadata [ kDescription ] = findDescription ( doc ) ;
180+ }
181+
159182 // Convert image to absolute href and add height and width
160183 resolveImageMetadata ( source , project , format , metadata ) ;
161184
@@ -167,11 +190,18 @@ export function metadataHtmlPostProcessor(
167190 // Append the metadata
168191 Object . keys ( metadata ) . forEach ( ( key ) => {
169192 if ( metadata [ key ] !== undefined ) {
193+ // Resolve the value
170194 const data = metadata [ key ] as string ;
195+ const value = provider . resolveValue
196+ ? provider . resolveValue ( key , data )
197+ : data ;
198+
199+ // Filter the key
171200 if ( provider . filter ) {
172201 key = provider . filter ( key ) ;
173202 }
174- writeMetaTag ( `${ provider . prefix } :${ key } ` , data , doc ) ;
203+
204+ writeMetaTag ( `${ provider . prefix } :${ key } ` , value , doc ) ;
175205 }
176206 } ) ;
177207 } ) ;
0 commit comments