Skip to content

Commit

Permalink
Fix document path generation for article import, add published date t…
Browse files Browse the repository at this point in the history
…o metadata.
  • Loading branch information
tonyklapatch committed May 21, 2024
1 parent 16791d9 commit 15f73e8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
90 changes: 90 additions & 0 deletions tools/importer/import-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const formatUrl = (originalUrl) => {
// Create a URL object based on the original URL
const url = new URL(originalUrl);

// Set the new domain (including protocol)
url.hostname = 'main--best-cigars-guide--famous-smoke.hlx.page';
url.protocol = 'https:';
url.port = '';

return url;
};

const createMetadataBlock = (main, document) => {
const meta = {};

// find the <title> element
const title = document.querySelector('title');
if (title) {
meta.Title = title.innerHTML.replace(/[\n\t]/gm, '');
}

// find the <meta property="og:description"> element
const desc = document.querySelector('[property="og:description"]');
if (desc) {
meta.Description = desc.content;
}

// set the <meta property="og:image"> element
meta.image = 'https://main--best-cigars-guide--famous-smoke.hlx.page/best-cigars-guide/icons/best-cigars-guide.png';

// helper to create the metadata block
const block = WebImporter.Blocks.getMetadataBlock(document, meta);

// append the block to the main element
main.append(block);

// returning the meta object might be usefull to other rules
return meta;
};

const createArticleListBlock = (main, document) => {
const categories = [['Article-list']];
const categoriesList = document.querySelector('.categories-list');
const divElements = categoriesList.querySelectorAll('div');

divElements.forEach((element) => {
const title = element.querySelector('h3').textContent.trim();
const description = element.querySelector('p').textContent.trim();
const image = element.querySelector('img');

const linkElement = element.querySelector('a.button');
linkElement.class = 'button';
const link = linkElement ? linkElement.href : '';

const card = [
[image],
[title],
[description],
[formatUrl(link)],
];

categories.push(card);
});

const articleList = WebImporter.DOMUtils.createTable(categories, document);

main.append(articleList);

// remove .categories-list from main because we just added it manually
WebImporter.DOMUtils.remove(main, ['.categories-list']);

return articleList;
};

const removeSectionsNotForImport = (main, document) => {
// remove any section from main not needed for import
WebImporter.DOMUtils.remove(main, ['.category-dropdown', '.breadcrumb']);
};

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

createMetadataBlock(main, document);
createArticleListBlock(main, document);
removeSectionsNotForImport(main, document);

return main;
},
};
8 changes: 8 additions & 0 deletions tools/importer/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const createMetadataBlock = (post, document) => {
el.src = img.src;
meta.Image = el;
}

// Get the published date
const published = new Date(document.querySelector('[property="article:published_time"]').content);
meta.publishedDate = `${published.getDate()} ${published.toLocaleDateString('default', { month: 'long'})} ${published.getFullYear()}`;
// end modified

// helper to create the metadata block
Expand Down Expand Up @@ -89,4 +93,8 @@ export default {
createMetadataBlock(post, document);
return post;
},

generateDocumentPath: ({ document, url, html, params }) => {
return document.querySelector("link[rel='canonical']").getAttribute("href");
}
};

0 comments on commit 15f73e8

Please sign in to comment.