I am in search of a filter that can replace the "href" attribute within a TextEntity from an API. The "text" may contain 3 different types of URLs. Once the replacement is done, I want to open the corrected URLs in a new separate window.
Here is the information I receive from the text value:
1. <a href="http://someurl.tdl">link</a> - this example includes all kinds of external links such as mysite.com/mypage.html or any other valid URL starting with http://, https://, ftp://.
2. <a href="singpage.html">internal page</a> - This may include various files like mypdf.pdf or mydoc.doc without a full domain in the URL.
3. <a href="mailto: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="177a6e7a767e7b5773787a767e793963737b">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204d594d41494c60444f4d41494e0e54444c">[email protected]</a></a>
I have attempted something but it did not work.
.filter('parseText', function ($sce, $sanitize) {
var mydomain = 'http://www.mydomain.tdl';
return function (text) {
var newStringUrlReplace = $sanitize(text).replace('href="','href="'+mydomain);
var regex = /href="([\S]+)"/g;
var newString = newStringUrlReplace.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('$1', '_blank', 'location=yes')\"");
return $sce.trustAsHtml(newString);
}
});
I need the output of the filtered "text" to be as follows:
1. <a href="http://someurl.tdl" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl', '_blank', 'location=yes')">link</a>
2. <a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')">internal page</a>
3. <a href="mailto: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660b1f0b070f0a2602090b070f084812020a">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2337232f27220e2a21232f2720603a2a22">[email protected]</a></a>
To clarify further:
I require a function that transforms these types of URLs.
<a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo">URL TO A EXTERNAL PAGE</a>
<a href="singpage.html">internal page of the CMS</a>
into
<a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl/whichcanincludeanything.html?bar=foo', '_blank', 'location=yes')">URL TO AN EXTERNAL PAGE</a>
<a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')">internal page</a>