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

Add showCaptionInArticleView option to append Caption components to embeds #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions lib/block-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const types = {
header4: ({children}) => renderHeading(4, children),
header5: ({children}) => renderHeading(5, children),
header6: ({children}) => renderHeading(6, children),
embed: item => renderEmbed(item, {
embed: (item, opts) => renderEmbed(item, {
layout: 'embedLayout',
style: 'embedStyle',
mediaLayout: 'embedMediaLayout',
mediaStyle: 'embedMediaStyle',
captionLayout: 'embedCaptionLayout',
captionTextStyle: 'embedCaptionTextStyle'
captionTextStyle: 'embedCaptionTextStyle',
showCaptionInArticleView: opts.showCaptionInArticleView
})
};

Expand All @@ -29,7 +30,7 @@ const hasContent = ({children}) =>
children.some(child => child.type !== 'linebreak' &&
(child.content && child.content.trim()));

function renderItem (item) {
const renderItem = opts => item => {
const {type} = item;
if (!types[type]) {
return null;
Expand All @@ -39,11 +40,11 @@ function renderItem (item) {
return null;
}

return types[type](item);
}
return types[type](item, opts);
};

export default function ({body}) {
export default function ({body}, opts) {
return body
.map(renderItem)
.map(renderItem(opts))
.filter(Boolean);
}
16 changes: 15 additions & 1 deletion lib/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const embeds = {
})
};

const renderCaption = (item, opts) => {
const captionToRender = item.attribution
? item.caption.concat([{ type: 'text', content: ' ' }], item.attribution)
: item.caption;

return renderTextItems(null, captionToRender, opts);
};

const render = (item, opts) => {
opts = opts || {};
const embed = embeds[item.embedType](item);
Expand Down Expand Up @@ -82,7 +90,13 @@ const render = (item, opts) => {
captionOpts.textStyle = opts.captionTextStyle;
}

embed.caption = renderTextItems(null, item.caption, captionOpts);
const captionComponent = renderCaption(item, captionOpts);
captionComponent.role = 'caption';

embed.caption = captionComponent;
if (opts.showCaptionInArticleView) {
result.components.push(captionComponent);
}
}

return result;
Expand Down
3 changes: 2 additions & 1 deletion lib/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const componentsWithEmbed = (article, opts) =>
mediaLayout: 'headerEmbedMediaLayout',
mediaStyle: 'headerEmbedMediaStyle',
captionLayout: 'headerCaptionLayout',
captionTextStyle: 'headerCaptionTextStyle'
captionTextStyle: 'headerCaptionTextStyle',
showCaptionInArticleView: opts.showCaptionInArticleView
})]
.concat(componentsWithoutEmbed(article, opts));

Expand Down