Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import/add path to metadata #271

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions import/tools/importer/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ function getRubric(document) {
return '';
}

function createMetadataBlock(document, main) {
/* eslint-disable no-undef */
const block = WebImporter.Blocks.getMetadataBlock(document, {});
main.append(block);
return block;
function getOrCreateMetadataBlock(document, main) {
let metaDataBlock = main.querySelector('.page-metadata');
if (!metaDataBlock) {
/* eslint-disable no-undef */
metaDataBlock = WebImporter.Blocks.getMetadataBlock(document, {});
metaDataBlock.classList.add('page-metadata');
main.append(metaDataBlock);
}
return metaDataBlock;
}

function appendMetadata(metadata, key, value) {
Expand Down Expand Up @@ -484,7 +488,7 @@ function transformArticleDOM(document, templateConfig) {
el.remove();
});

const metadata = createMetadataBlock(document, main);
const metadata = getOrCreateMetadataBlock(document, main);
appendMetadata(metadata, 'Author', author);
appendMetadata(metadata, 'Author URL', authorURL);
appendMetadata(metadata, 'Publication Date', publicationDate);
Expand Down Expand Up @@ -638,7 +642,7 @@ function transformGalleryDOM(document, templateConfig) {
const publicationDate = getPublicationDate(document);
const rubric = getRubric(document);

const metadata = createMetadataBlock(document, main);
const metadata = getOrCreateMetadataBlock(document, main);

appendMetadata(metadata, 'Author', author);
appendMetadata(metadata, 'Author URL', authorURL);
Expand Down Expand Up @@ -687,7 +691,7 @@ function transformProductDOM(document, templateConfig) {
}
});

const metadata = createMetadataBlock(document, main);
const metadata = getOrCreateMetadataBlock(document, main);
appendMetadata(metadata, 'og:type', 'product');
appendMetadata(metadata, 'template', templateConfig.template);
appendMetadata(metadata, 'category', templateConfig.category);
Expand Down Expand Up @@ -771,7 +775,7 @@ function applyMarkupFixes(document) {
fixImages(document);
}

async function trasformDOM(document, url) {
async function trasformDOM(document, url, documentPath) {
const templateConfig = findTemplateConfig(document);

let retObj = {
Expand All @@ -786,6 +790,9 @@ async function trasformDOM(document, url) {
applyMarkupFixes(document);
retObj = templateConfig.transformer(document, templateConfig);
await updateInternalLinks(retObj.element, url, retObj.report);

const metadata = getOrCreateMetadataBlock(document, retObj.element);
appendMetadata(metadata, 'path', documentPath);
} else {
const bodyClass = document.querySelector('body').getAttribute('class');
throw new Error(`Unknown page type. Body class list ${bodyClass}`);
Expand Down Expand Up @@ -817,7 +824,7 @@ function preprocess({ document, url, html, params }) {
*/
async function transform({document, url, html, params}) {
const docPath = mapToDocumentPath(document, url);
const retObj = await trasformDOM(document, url);
const retObj = await trasformDOM(document, url, docPath);
return [{
element: retObj.element,
path: docPath,
Expand Down