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

MWPW-135320 Init article header #16

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 46 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,59 @@ function buildTagsBlock() {
}
}

function getImageCaption(picture) {
// Check if the parent element has a caption
const parentEl = picture.parentNode;
let caption = parentEl.querySelector('em');
if (caption) return caption;

// If the parent element doesn't have a caption, check if the next sibling does
const parentSiblingEl = parentEl.nextElementSibling;
if (!parentSiblingEl || !parentSiblingEl.querySelector('picture')) return undefined;
const firstChildEl = parentSiblingEl.firstChild;
caption = firstChildEl?.tagName === 'EM' ? firstChildEl : undefined;
return caption;
}

async function buildArticleHeader(el) {
const miloLibs = getLibs();
const { getMetadata, getConfig } = await import(`${miloLibs}/utils/utils.js`);
const { loadTaxonomy, getLinkForTopic, getTaxonomyModule } = await import(`${miloLibs}/blocks/article-feed/article-helpers.js`);
if (!getTaxonomyModule()) {
await loadTaxonomy();
}
const div = document.createElement('div');
const h1 = el.querySelector('h1');
const picture = el.querySelector('picture');
const caption = getImageCaption(picture);
const figure = document.createElement('div');
figure.append(picture, caption);
const tag = getMetadata('article:tag');
const category = tag || 'News';
const author = getMetadata('author') || 'Adobe Communications Team';
const { codeRoot } = getConfig();
const authorURL = getMetadata('author-url') || (author ? `${codeRoot}/authors/${author.replace(/[^0-9a-z]/gi, '-').toLowerCase()}` : null);
const publicationDate = getMetadata('publication-date');
const categoryTag = getLinkForTopic(category);
const articleHeaderBlockEl = buildBlock('article-header', [
[`<p>${categoryTag}</p>`],
[h1],
[`<p>${authorURL ? `<a href="${authorURL}">${author}</a>` : author}</p>
<p>${publicationDate}</p>`],
[figure],
]);
div.append(articleHeaderBlockEl);
el.prepend(div);
}

export async function buildAutoBlocks() {
const miloLibs = getLibs();
const { getMetadata } = await import(`${miloLibs}/utils/utils.js`);
const mainEl = document.querySelector('main');
try {
if (getMetadata('publication-date') && !mainEl.querySelector('.article-header')) {
buildTagsBlock(mainEl);
await buildArticleHeader(mainEl);
}
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
27 changes: 25 additions & 2 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,20 @@ main p picture img {
max-width: var(--body-max-width);
margin-left: auto;
margin-right: auto;
}
}

.article-header .article-feature-image {
padding: 0;
margin: auto;
width: 100%;
max-width: 1000px;
}

.article-header .article-feature-image .figure-feature img {
width: 100%;
max-height: 440px;
object-fit: cover;
}

@media screen and (min-width: 600px) {
body main {
Expand All @@ -103,5 +116,15 @@ main p picture img {
max-width: var(--body-max-width);
margin-left: auto;
margin-right: auto;
}
}

main .article-header .article-category,
main .article-header .article-title,
main .article-header .article-byline {
max-width: 600px;
margin-left: auto;
margin-right: auto;
padding-left: 0;
padding-right: 0;
}
}
Loading