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

feat: prevent yield calculation for ## as ingredient headline #1998

Merged
merged 2 commits into from
Dec 15, 2023
Merged
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
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
Loading