Skip to content

Commit

Permalink
#39 - Featured Article component does not pull title and description …
Browse files Browse the repository at this point in the history
…from the referenced article

Move getMetadata override which accepts a document as 2nd parameter to featured-article.js to avoid changing lib-franklin.js
  • Loading branch information
blefebvre committed Sep 22, 2023
1 parent 6bb0988 commit 6726745
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
16 changes: 12 additions & 4 deletions blocks/featured-article/featured-article.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
getMetadata,
} from '../../scripts/lib-franklin.js';

/**
* Loads a fragment.
* @param {string} path The path to the fragment
Expand All @@ -18,6 +14,18 @@ async function loadFragment(path) {
return null;
}

/**
* Retrieves the content of metadata tags.
* @param {string} name The metadata name (or property)
* @param doc Document object to query for the metadata. Defaults to the window's document
* @returns {string} The metadata value(s)
*/
function getMetadata(name, doc = document) {
const attr = name && name.includes(':') ? 'property' : 'name';
const meta = [...doc.head.querySelectorAll(`meta[${attr}="${name}"]`)].map((m) => m.content).join(', ');
return meta || '';
}

/**
* @param {HTMLElement} $block The header block element
*/
Expand Down
9 changes: 3 additions & 6 deletions scripts/lib-franklin.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export function sampleRUM(checkpoint, data = {}) {
/**
* Loads a CSS file.
* @param {string} href The path to the CSS file
* @param {(value: *) => void} callback Function to call onload or onerror
*/
export function loadCSS(href, callback) {
if (!document.querySelector(`head > link[href="${href}"]`)) {
Expand All @@ -99,12 +98,11 @@ export function loadCSS(href, callback) {
/**
* Retrieves the content of metadata tags.
* @param {string} name The metadata name (or property)
* @param doc Document object to query for the metadata. Defaults to the window's document
* @returns {string} The metadata value(s)
*/
export function getMetadata(name, doc = document) {
export function getMetadata(name) {
const attr = name && name.includes(':') ? 'property' : 'name';
const meta = [...doc.head.querySelectorAll(`meta[${attr}="${name}"]`)].map((m) => m.content).join(', ');
const meta = [...document.head.querySelectorAll(`meta[${attr}="${name}"]`)].map((m) => m.content).join(', ');
return meta || '';
}

Expand Down Expand Up @@ -261,7 +259,7 @@ export function readBlockConfig(block) {

/**
* Decorates all sections in a container element.
* @param {Element} main The container element
* @param {Element} $main The container element
*/
export function decorateSections(main) {
main.querySelectorAll(':scope > div').forEach((section) => {
Expand Down Expand Up @@ -434,7 +432,6 @@ export async function loadBlocks(main) {
/**
* Returns a picture element with webp and fallbacks
* @param {string} src The image URL
* @param {string} alt Alternative text to describe the image
* @param {boolean} eager load image eager
* @param {Array} breakpoints breakpoints and corresponding params (eg. width)
*/
Expand Down

0 comments on commit 6726745

Please sign in to comment.