Created
August 21, 2022 11:36
Using jSoup To Extract JSON+LD Structured Data In ColdFusion 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
/** | |
* I get the JSON+LD structured data for the given post. | |
*/ | |
private struct function getStructuredData( required struct post ) { | |
var dom = jSoupJavaLoader | |
.create( "org.jsoup.Jsoup" ) | |
.parse( post.content ) | |
.body() | |
; | |
// CAUTION: We know that the partial-normalizer has replaced all of the resource | |
// uploads with CDN-based URLs. As such, all of the uploaded images should already | |
// be fully-qualified URLs. | |
var images = dom | |
.select( "img[src*='/resources/uploads/']" ) | |
.map( | |
( node ) => { | |
return( node.attr( "src" ) ); | |
} | |
) | |
; | |
var data = { | |
"@context": "https://schema.org", | |
"@type": "NewsArticle", | |
"headline": "#encodeForHtml( post.name.left( 110 ) )#", | |
"image": images, | |
"datePublished": dateTimeFormat( post.datePosted, "iso" ), | |
"dateModified": dateTimeFormat( post.updatedAt, "iso" ), | |
"author": [ | |
{ | |
"@type": "Person", | |
"name": "Ben Nadel", | |
"jobTitle": "Principal Engineer", | |
"url": "https://www.bennadel.com/about/about-ben-nadel.htm" | |
} | |
] | |
}; | |
return( data ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title> | |
ColdFusion is so Wonderful! | |
</title> | |
<script type="application/ld+json"> | |
{ | |
"@context": "https://schema.org", | |
"@type": "NewsArticle", | |
"headline": "ColdFusion is so Wonderful!", | |
"image": [], | |
"datePublished": "2022-08-21T07:05:00Z", | |
"author": [ | |
{ | |
"@type": "Person", | |
"name": "Ben Nadel", | |
"jobTitle": "Principal Engineer", | |
"url": "https://www.bennadel.com/about/about-ben-nadel.htm" | |
} | |
] | |
} | |
</script> | |
</head> | |
<body> | |
<!--- Body of your document. ---> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- Reset the output buffer. ---> | |
<cfcontent type="text/html; charset=utf-8" /> | |
<cfoutput> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title> | |
#encodeForHtml( request.template.metaTitle )# | |
</title> | |
<script type="application/ld+json"> | |
#serializeJson( request.template.structuredData )# | |
</script> | |
<!--- Truncated for demo. ---> | |
</head> | |
<body> | |
<!--- Truncated for demo. ---> | |
</body> | |
</html> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment