Thursday, December 5, 2024
HomeJavaScriptUtilizing jSoup To Repair Submit-Marriage Title Modifications In ColdFusion 2021

Utilizing jSoup To Repair Submit-Marriage Title Modifications In ColdFusion 2021


At the start of this 12 months, I began utilizing jSoup on my ColdFusion weblog. This opened up every kind of potentialities from extracting Open Graph / Twitter Card information to producing weblog submit previews to injecting anchor hyperlinks on my part titles. And now, this morning, I spotted that I might use it to repair post-marriage identify adjustments; a minimum of, till I replace the underlying content material.

As you might know, I co-host a podcast name Working Code. On every episode write-up, I record out the names and hyperlinks for all of our co-hosts (and the occasional visitor). For the primary 80+ episodes, Carol has been “Carol Hamilton“. However now, in a post-nuptial period, Carol has turn into “Carol Weiler” – each right here and across the internet.

A facet impact of that is that her self-importance LinkedIn URL has modified. And – disgrace on LinkedIn for not dealing with this extra gracefully – it implies that all of her LinkedIn hyperlinks on my 90 episode blog-posts have damaged.

So now, right here comes ColdFusion 2021 and jSoup to the rescue. As a part of my “content material normalization” course of, I can paper over this transformation utilizing some just-in-time DOM (Doc Object Mannequin) manipulation. Now, as I am getting ready my blog-post content material for caching, I run it via the next “Carol Hyperlink Fixer” technique. Please notice that this ColdFusion element has been closely truncated for the demo:

element {
	
	/**
	* I repair Carol's LinkedIn URLs and identify (after her identify change).
	*/
	personal void operate cleanUpCarolLinks( required string content material ) {

		// The jSoup library permits us to parse, traverse, and mutate HTML on the
		// ColdFusion server utilizing a well-recognized, luxurious jQuery-inspired syntax.
		var dom = jSoupJavaLoader
			.create( "org.jsoup.Jsoup" )
			.parse( content material )
			.physique()
		;

		// Discover all of the embedded anchor tags within the content material that presently level to
		// Carol's outdated LinkedIn profile (utilizing a partial match on the LinkedIn slug in
		// order to make the selector a tiny bit extra versatile).
		for ( var node in dom.choose( "a[href*='carol-hamilton-5a869257']" ) ) {

			// Replace the hyperlink to level to her new LinkedIn profile.
			node.attr( "href", "https://www.linkedin.com/in/carol-weiler-5a869257/" );

			// That is seemingly not going to be true in all instances; however, in lots of instances, the
			// hyperlink to Carol's LinkedIn profile is preceded by her identify (equivalent to within the
			// record of co-hosts on the Working Code podcast). In such a state of affairs, let's
			// additionally attempt to back-up within the DOM tree and replace her identify as properly.
			var labelNodes = node
				.mum or dad()
				.choose( "robust:incorporates(Carol Hamilton)" )
			;

			if ( labelNodes.len() ) {

				labelNodes[ 1 ].textual content( "Carol Weiler" );

			}

		}

	}

}

As you may see, I begin to finding anchor hyperlinks that time to her outdated LinkedIn URL. Then, I replace the href attribute for these hyperlinks. After which, I attempt to discover her identify and replace that as properly. And now, once I render my weblog content material, Carol Hamilton has magically turn into Carol Weiler:

Chrome Dev Tools showing that both Carol's name and LinkedIn URL have been changed using jSoup and ColdFusion 2021

I consider it was Archimedes who stated, “Give me a programming language robust sufficient and I’ll change the world”. It seems, the mix of ColdFusion and jSoup could have been simply the mix of power and suppleness that he was speaking about. It appears, with ColdFusion, the one restrict is my creativeness.

Need to use code from this submit?
Take a look at the license.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments