Select Page

Using jSoup To Fix Post-Marriage Name Changes In ColdFusion 2021

Published: August 31, 2022

At the beginning of this year, I started using jSoup on my ColdFusion blog. This opened up all kinds of possibilities from extracting Open Graph / Twitter Card data to generating blog post previews to injecting anchor links on my section titles. And now, this morning, I realized that I could use it to fix post-marriage name changes; at least, until I update the underlying content.

As you may know, I co-host a podcast call Working Code. On each episode write-up, I list out the names and links for all of our co-hosts (and the occasional guest). For the first 80+ episodes, Carol has been “Carol Hamilton“. But now, in a post-nuptial era, Carol has become “Carol Weiler” – both here and around the web.

A side effect of this is that her vanity LinkedIn URL has changed. And – shame on LinkedIn for not handling this more gracefully – it means that all of her LinkedIn links on my 90 episode blog-posts have broken.

So now, here comes ColdFusion 2021 and jSoup to the rescue. As part of my “content normalization” process, I can paper over this change using some just-in-time DOM (Document Object Model) manipulation. Now, as I’m preparing my blog-post content for caching, I run it through the following “Carol Link Fixer” method. Please note that this ColdFusion component has been heavily truncated for the demo:

component {
	* I fix Carol's LinkedIn URLs and name (after her name change).
	private void function cleanUpCarolLinks( required string content ) {
		// The jSoup library allows us to parse, traverse, and mutate HTML on the
		// ColdFusion server using a familiar, luxurious jQuery-inspired syntax.
		var dom = jSoupJavaLoader
			.create( "org.jsoup.Jsoup" )
			.parse( content )
		// Find all the embedded anchor tags in the content that currently point to
		// Carol's old LinkedIn profile (using a partial match on the LinkedIn slug in
		// order to make the selector a tiny bit more flexible).
		for ( var node in "a[href*='carol-hamilton-5a869257']" ) ) {
			// Update the link to point to her new LinkedIn profile.
			node.attr( "href", "" );
			// This is likely not going to be true in all cases; but, in many cases, the
			// link to Carol's LinkedIn profile is preceded by her name (such as in the
			// list of co-hosts on the Working Code podcast). In such a scenario, let's
			// also try to back-up in the DOM tree and update her name as well.
			var labelNodes = node
				.select( "strong:contains(Carol Hamilton)" )
			if ( labelNodes.len() ) {
				labelNodes[ 1 ].text( "Carol Weiler" );

As you can see, I start to locating anchor links that point to her old LinkedIn URL. Then, I update the href attribute for those links. And then, I try to find her name and update that as well. And now, when I render my blog content, Carol Hamilton has magically become Carol Weiler:

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

I believe it was Archimedes who said, “Give me a programming language strong enough and I will change the world”. It turns out, the combination of ColdFusion and jSoup may have been just the combination of strength and flexibility that he was talking about. It seems, with ColdFusion, the only limit is my imagination.

Want to use code from this post?
Check out the license.