Skip to content

Commit

Permalink
refactor(video-metadata): createVideoObject(record)
Browse files Browse the repository at this point in the history
  • Loading branch information
hparra committed Apr 27, 2024
1 parent dac9c7e commit 0a6be43
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions libs/blocks/video-metadata/video-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ function addSeekToActionField(videoObj, blockKey, blockValue) {
}
}

export function createVideoObject(blockMap) {
export function createVideoObject(record) {
const video = {};
Object.entries(blockMap).forEach(([key, val]) => {
const blockVal = val.content && val.content.textContent.trim();
Object.entries(record).forEach(([key, blockVal]) => {
if (!blockVal) return;
const blockKey = key && key.replaceAll(' ', '-');
switch (true) {
Expand Down Expand Up @@ -110,10 +109,21 @@ export function createVideoObject(blockMap) {
return null;
}

export function blockMapToRecord(blockMap) {
return blockMap && Object.entries(blockMap).reduce((rec, kv) => {
const [key, value] = kv;
const val = value?.content?.textContent?.trim();
if (!val) return rec;
rec[key] = val;
return rec;
}, {});
}

export default function init(el) {
const metadata = getMetadata(el);
el.remove();
const obj = createVideoObject(metadata);
const record = blockMapToRecord(metadata);
const obj = createVideoObject(record);
if (!obj) return;
const script = createTag('script', { type: 'application/ld+json' }, JSON.stringify(obj));
document.head.append(script);
Expand Down

0 comments on commit 0a6be43

Please sign in to comment.