diff --git a/src/snippet/index.jsx b/src/snippet/index.jsx index 27fc784..141620c 100644 --- a/src/snippet/index.jsx +++ b/src/snippet/index.jsx @@ -4,13 +4,27 @@ import { connect } from 'react-redux'; import Markdown from '../markdown'; import { render } from 'mustache'; +function getTemplate(content, primary, fallback) { + const keysToTry = [primary, ...(Array.isArray(fallback) ? fallback : [fallback])]; + + for (let key of keysToTry) { + const template = get(content, key); + if (template != undefined) { + return template; + } + } + + return undefined; +} + export const Snippet = ({ content, children, optional, fallback, ...props }) => { - const str = get(content, children, get(content, fallback)); + const str = getTemplate(content, children, fallback); if (str === undefined && optional) { return null; + } if (str === undefined) { - throw new Error(`Failed to lookup content snippet: ${children}`); + return `Failed to lookup content snippet: ${children}`; } if (typeof str !== 'string') { throw new Error(`Invalid content snippet for key ${children}: ${JSON.stringify(str)}`); diff --git a/src/snippet/index.spec.jsx b/src/snippet/index.spec.jsx index 10fe931..04d2ff1 100644 --- a/src/snippet/index.spec.jsx +++ b/src/snippet/index.spec.jsx @@ -42,4 +42,16 @@ two` expect(wrapper.html()).toEqual(paragraphs); }); + test('can accept single fallback', () => { + const wrapper = render(
non.existent
); + expect(wrapper.find('p').length).toEqual(2); + expect(wrapper.html()).toEqual(paragraphs); + }); + + test('can accept multiple fallbacks', () => { + const wrapper = render(
non.existent
); + expect(wrapper.find('p').length).toEqual(2); + expect(wrapper.html()).toEqual(paragraphs); + }); + });