Skip to content

Commit

Permalink
feat: add absolute path check and skip API prefix if absolute path (#90)
Browse files Browse the repository at this point in the history
* feat: add absolute path check and skip API prefix if its absolute path
* feat: add handle for same page anchor tag
  • Loading branch information
adong authored Aug 1, 2019
1 parent 8f5c8ed commit d14c5e8
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions helpers/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const apiUrl = config.get('ecosystem.api');
const apiVersion = 'v4';

const iframeScript = `
function isAbsolutePath(href) {
const absolutePath = new RegExp('^((http|https)://|#)');
return absolutePath.test(href);
};
function replaceHref(apiHref='${apiUrl}') {
const currentIframeHref = new URL(document.location.href);
const urlOrigin = currentIframeHref.origin;
Expand All @@ -21,6 +26,9 @@ const iframeScript = `
const anchors = document.getElementsByTagName('a');
for (let anchor of anchors) {
let originalHref = anchor.attributes.href.value;
if (isAbsolutePath(originalHref)) {
continue;
}
const newUrl = apiUrlOrigin + '/' + apiVersion + '/' + urlFileDir + '/' + originalHref;
anchor.href = newUrl.replace('ARTIFACTS', 'artifacts') + '?type=preview';
anchor.addEventListener('click', function(e) {
Expand Down

0 comments on commit d14c5e8

Please sign in to comment.