Skip to content

Commit

Permalink
MWPW-135320 Init article header (#16)
Browse files Browse the repository at this point in the history
* MWPW-135320 Init article header

* remove unused

* pr

* Fix author image

* Fix eyebrow
  • Loading branch information
meganthecoder authored Sep 22, 2023
1 parent 3fb4caa commit 2fad9b0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
45 changes: 45 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,58 @@ 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 category = getMetadata('category');
const author = getMetadata('author') || 'Adobe Communications Team';
const { locale } = getConfig();
const authorURL = getMetadata('author-url') || (author ? `${locale.contentRoot}/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;
}
}

0 comments on commit 2fad9b0

Please sign in to comment.