generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix document path generation for article import, add published date t…
…o metadata.
- Loading branch information
1 parent
16791d9
commit 15f73e8
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters