Skip to content

Commit

Permalink
Merge pull request #1998 from j0hannesr0th/master
Browse files Browse the repository at this point in the history
feat: prevent yield calculation for ## as ingredient headline
  • Loading branch information
christianlupus committed Dec 15, 2023
2 parents fe19c54 + 8d4a606 commit 170edf7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

### Fixed
- Prevent yield calculation for ## as ingredient headline [#1998](https://github.com/nextcloud/cookbook/pull/1998) @j0hannesr0th

### Documentation
- Improve structure of `README.md` [#1989](https://github.com/nextcloud/cookbook/pull/1989) @seyfeb

Expand Down
13 changes: 8 additions & 5 deletions src/components/RecipeView/RecipeIngredient.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ const displayIngredient = computed(() => {
return props.ingredient;
});
const formattedIngredient = computed(() =>
isDone.value
const formattedIngredient = computed(() => {
if (isHeader()) {
return displayIngredient.value;
}
return isDone.value
? `~~${displayIngredient.value}~~`
: `**${displayIngredient.value}**`,
);
: `**${displayIngredient.value}**`;
});
</script>

<script>
Expand All @@ -73,7 +77,6 @@ li {
.header {
position: relative;
left: -1.25em;
margin-top: 0.25em;
font-variant: small-caps;
list-style-type: none;
Expand Down
19 changes: 14 additions & 5 deletions src/js/yieldCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ const fractionRegExp = /^((\d+\s+)?(\d+)\s*\/\s*(\d+)).*/;

function isValidIngredientSyntax(ingredient) {
/*
The ingredientSyntaxRegExp checks whether the ingredient string starts with a number,
The ingredientSyntaxRegExp checks whether the ingredient string starts with a number,
possibly followed by a fractional part or a fraction. Then there should be a space
and then any sequence of characters.
*/
const ingredientSyntaxRegExp = /^(?:\d+(?:\.\d+)?(?:\/\d+)?)\s?.*$/;

/*
The ingredientMultipleSeperatorsRegExp is used to check whether the string contains
more than one separators (.,) after a number. This is used to exclude strings that
more than one separators (.,) after a number. This is used to exclude strings that
contain more than one separator from being valid.
*/
const ingredientMultipleSeperatorsRegExp = /^-?\d+(?:[.,]\d+){2,}.*/;
const ingredientMultipleSeparatorsRegExp = /^-?\d+(?:[.,]\d+){2,}.*/;

/*
startsWithDoubleHashRegExp checks if the ingredient string begins with "## " followed by any characters.
*/
const startsWithDoubleHashRegExp = /^## .+$/;

return (
fractionRegExp.test(ingredient) ||
startsWithDoubleHashRegExp.test(ingredient) ||
(ingredientSyntaxRegExp.test(ingredient) &&
!ingredientMultipleSeperatorsRegExp.test(ingredient))
!ingredientMultipleSeparatorsRegExp.test(ingredient))
);
}

Expand All @@ -32,6 +37,10 @@ function isIngredientsArrayValid(ingredients) {

function recalculateIngredients(ingredients, currentYield, originalYield) {
return ingredients.map((ingredient) => {
if (ingredient.startsWith('## ')) {
return ingredient;
}

const matches = ingredient.match(fractionRegExp);

if (matches) {
Expand Down

0 comments on commit 170edf7

Please sign in to comment.