Skip to content

Commit

Permalink
Add fallback for unstructured Jamie Oliver recipes
Browse files Browse the repository at this point in the history
Note: misses proper white-space handling.
  • Loading branch information
harmenjanssen committed Oct 23, 2018
1 parent 969945d commit 1a44b99
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
39 changes: 38 additions & 1 deletion services/jamie-oliver/properties/recipeInstructions.js
@@ -1,6 +1,41 @@
const createQuerySelectorAll = require('utils/createQuerySelectorAll');
const withFallback = require('utils/withFallback');
const sanitizeHTML = require('sanitize-html');

module.exports = createQuerySelectorAll(
// Tested with: https://www.jamieoliver.com/news-and-features/features/make-roast-parsnips/
const unstructuredRecipe = createQuerySelectorAll(
'.entry-content',
container => {
// Clean up some known noise element.
const galleries = container.querySelector('.galleries');
if (galleries) {
// Remove galleries and all nodes after the galleries.
while (galleries.nextSibling) {
galleries.nextSibling.parentNode.removeChild(galleries.nextSibling);
}
galleries.parentNode.removeChild(galleries);
}

const shareBar = container.querySelector('#share-bar-hz');
if (shareBar) {
shareBar.parentNode.removeChild(shareBar);
}

return sanitizeHTML(
container.innerHTML
// Strip <p> tags
.replace(/\<p.*?\>/gi, '')
// Replace </p> with <br>, will later be converted to \n in cleanup
.replace(/\<\/p\>/g, '<br>'),
{
// Allow <br> tags, see above
allowedTags: ['br']
}
);
}
);

const structuredRecipe = createQuerySelectorAll(
'.recipeSteps > li',
instructions => {
if (
Expand All @@ -15,3 +50,5 @@ module.exports = createQuerySelectorAll(
return instructions.innerHTML;
}
);

module.exports = withFallback(unstructuredRecipe)(structuredRecipe);
8 changes: 8 additions & 0 deletions utils/withFallback.js
@@ -0,0 +1,8 @@
/**
* Higher-order function, creating a function that will execute its given fallback in case it yields
* no results.
*/
module.exports = fallbackFn => primaryFn => (...args) => {
const result = primaryFn(...args);
return result.length ? result : fallbackFn(...args);
};

0 comments on commit 1a44b99

Please sign in to comment.