Skip to content

Commit

Permalink
Import/add path to metadata (#271)
Browse files Browse the repository at this point in the history
* add document path to metadata

* add document path to page metadata
  • Loading branch information
hsaginor authored Sep 26, 2023
1 parent 51058b9 commit 4b432c3
Showing 1 changed file with 17 additions and 10 deletions.
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

0 comments on commit 4b432c3

Please sign in to comment.