Skip to content

Instantly share code, notes, and snippets.

@wbamberg
Created May 19, 2020 04:55
Show Gist options
  • Save wbamberg/899d042fadf69c7576ba53c179820922 to your computer and use it in GitHub Desktop.
Save wbamberg/899d042fadf69c7576ba53c179820922 to your computer and use it in GitHub Desktop.
// get all the ingredients from the recipe that were present in the page, plus "prose.*"
const filteredRecipeIngredients = file.data.recipe.body.filter(
(ingredient) =>
pageIngredientNames.includes(ingredient) || ingredient === "prose.*"
);
// get the index of the "prose.*" ingredient in the recipe
const indexOfProseStar = filteredRecipeIngredients.indexOf("prose.*");
if (indexOfProseStar !== -1) {
// get the ingredients before and after prose.* in the recipe
const ingredientBefore = filteredRecipeIngredients[indexOfProseStar - 1];
const ingredientAfter = filteredRecipeIngredients[indexOfProseStar + 1];
// get the node indices (positions) of the before and after ingredients in the page
const indexBefore = getPageIndex(ingredientBefore, pageIngredients);
const indexAfter = getPageIndex(ingredientAfter, pageIngredients);
// get all the custom prose sections from the page
const customSections = collectProseStar(tree, file);
// all custom prose sections must have a node index between the previous and next
for (const section of customSections) {
if (
(indexBefore !== null && indexBefore >= section.data.pageIndex) ||
(indexAfter !== null && indexAfter <= section.data.pageIndex)
) {
const message = file.message(
`prose.*: ${section.tagName}#${section.properties.id} not expected in this position`,
section,
`${source}:${path.basename(
file.data.recipePath,
".yaml"
)}/prose.*/ingredient-out-of-order`
);
message.fatal = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment