Getting rid of ?utm_source spam in ChatGPT results

By Archived fry69 (@fry69.altq.net)
Published:

I love copying relevant results from ChatGPT into Obsidian, so I have a permanent copy to become tag-/search-/reference-able in my second brain. Even better is that ChatGPT now adds links/references to its results, which are often useful (not always, but often enough).

The only downside is that those links come with a spammy, superfluous ?utm_source=chatgpt.com at the end, which makes the whole link part at the bottom ugly and hard to read. After a quick search with https://perplexity.ai I found the "Paste Transform" plugin for Obsidian, which almost can do what I want:

GitHub link: https://github.com/rekby/obsidian-paste-transform

To install it just search in the Community Plugins for transform in Obsidian.

Here is the regex pattern to remove the dreaded ?utm_source:

^(.*) https://(.*)[?]utm_source=chatgpt.com (.*)$
Output transform:
$1 https://$2 $3
Some test data:
[1]: https://datatracker.ietf.org/doc/html/rfc7591?utm_source=chatgpt.com "RFC 7591 - OAuth 2.0 Dynamic Client Registration Protocol"
[2]: https://datatracker.ietf.org/doc/html/rfc8628?utm_source=chatgpt.com "RFC 8628 - OAuth 2.0 Device Authorization Grant - IETF Datatracker"
[3]: https://oauth.net/2/device-flow/?utm_source=chatgpt.com "RFC 8628: OAuth 2.0 Device Authorization Grant"
[4]: https://datatracker.ietf.org/doc/draft-ietf-oauth-dpop/16/?utm_source=chatgpt.com "draft-ietf-oauth-dpop-16 - OAuth 2.0 Demonstrating Proof of ..."
[5]: https://oauth.net/2/dpop/?utm_source=chatgpt.com "RFC 9449: OAuth 2.0 Demonstrating Proof-of-Possession (DPoP)"
[6]: https://openid.net/specs/openid-connect-registration-1_0.html?utm_source=chatgpt.com "OpenID Connect Dynamic Client Registration 1.0 incorporating ..."
[7]: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-device-code?utm_source=chatgpt.com "OAuth 2.0 device authorization grant - Microsoft identity platform"
[8]: https://datatracker.ietf.org/doc/draft-ietf-oauth-v2-1/?utm_source=chatgpt.com "draft-ietf-oauth-v2-1-12 - The OAuth 2.1 Authorization Framework"

Unfortunately this will not work out of the box, since the regular expressions are matched on the whole paste, not per line. Thankfully this is easily fixable with a tiny one character patch, taken from this pull request: https://github.com/rekby/obsidian-paste-transform/pull/8

This is the diff for the source in the repository, but that requires cloning, building, etc:

diff --git a/main.ts b/main.ts
index 01fee5a..e452cbc 100644
--- a/main.ts
+++ b/main.ts
@@ -29,7 +29,7 @@ class ReplaceRule {
 	replacer: string;
 
 	constructor(pattern: string, replacer: string) {
-		this.pattern = new RegExp(pattern, 'g');
+		this.pattern = new RegExp(pattern, 'gm');
 		this.replacer = replacer;
 	}
 }

A more brute force approach is to edit the live plugin file on disk (.obsidian/plugins/paste-transform/main.js in the Obsidian Vault root):

--- main.js.orig	2025-05-15 09:31:53
+++ main.js	2025-05-15 09:20:26
@@ -46,7 +46,7 @@
 };
 var ReplaceRule = class {
   constructor(pattern, replacer) {
-    this.pattern = new RegExp(pattern, "g");
+    this.pattern = new RegExp(pattern, "gm");
     this.replacer = replacer;
   }
 };
To reload the file from disk, just turn the plugin off and on again inside Obsidian. The test data from above should not result in this:

[1]: https://datatracker.ietf.org/doc/html/rfc7591 "RFC 7591 - OAuth 2.0 Dynamic Client Registration Protocol"
[2]: https://datatracker.ietf.org/doc/html/rfc8628 "RFC 8628 - OAuth 2.0 Device Authorization Grant - IETF Datatracker"
[3]: https://oauth.net/2/device-flow/ "RFC 8628: OAuth 2.0 Device Authorization Grant"
[4]: https://datatracker.ietf.org/doc/draft-ietf-oauth-dpop/16/ "draft-ietf-oauth-dpop-16 - OAuth 2.0 Demonstrating Proof of ..."
[5]: https://oauth.net/2/dpop/ "RFC 9449: OAuth 2.0 Demonstrating Proof-of-Possession (DPoP)"
[6]: https://openid.net/specs/openid-connect-registration-1_0.html "OpenID Connect Dynamic Client Registration 1.0 incorporating ..."
[7]: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-device-code "OAuth 2.0 device authorization grant - Microsoft identity platform"
[8]: https://datatracker.ietf.org/doc/draft-ietf-oauth-v2-1/ "draft-ietf-oauth-v2-1-12 - The OAuth 2.1 Authorization Framework"
So much better 🎉