Skip to content

Commit

Permalink
basic import script
Browse files Browse the repository at this point in the history
  • Loading branch information
nc-andreashaller committed Jan 30, 2024
1 parent ba48602 commit b78af62
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tools/importer/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function transformLinks(main) {
main.querySelectorAll('a').forEach((a) => {
if (a.href.startsWith('/') || a.href.startsWith(window.location.origin) || a.href.startsWith('https://guide.raqn.io/')) {
const old = new URL(a.href);
const hlx = new URL(`${old.pathname}${old.search}${old.hash}`, 'https://main--henkel-raqn-guide--hlxsites.hlx.page/');

// Remove .html extension
if (hlx.pathname.endsWith('.html')) {
hlx.pathname = hlx.pathname.slice(0, -5);
}

a.href = hlx.toString();

if (a.textContent === old.toString()) {
a.textContent = a.href;
}
}
});
}

function transformImages(document, main) {
main.querySelectorAll('meta[itemprop="contentUrl"][content^="https://dm.henkel-dam.com"]').forEach((meta) => {
const newImg = document.createElement('img');
newImg.src = `${meta.content}?wid=2560`;
meta.replaceWith(newImg);
});
}

function transformStages(document, main) {
main.querySelectorAll('heliux-stage').forEach((stage) => {
stage.replaceWith(WebImporter.DOMUtils.createTable([
['Hero'],
[[...stage.children], [stage.querySelector('img')]]
], document));
});
}

function transformTeaserLists(document, main) {
main.querySelectorAll('heliux-teaserlist').forEach((teaserList) => {
const cards = [...teaserList.querySelectorAll('li')].map((li) => [...li.children]);
teaserList.replaceWith(WebImporter.DOMUtils.createTable([['Card']].concat(cards), document));
});
}

export default {
transformDOM: ({ document }) => {
const main = document.querySelector('main');

transformLinks(main);
transformImages(document, main);
transformStages(document, main);
transformTeaserLists(document, main);

return main;
},
};

0 comments on commit b78af62

Please sign in to comment.