mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-08-02 13:00:14 -04:00
fix: handle HowToSection missing itemListElement in clean_instructions (#7616)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
This commit is contained in:
@@ -220,11 +220,14 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N
|
|||||||
# },
|
# },
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
|
# Some sites (e.g. NYT Cooking) emit empty HowToSection placeholders
|
||||||
|
# with no itemListElement key, or use "item" per the schema.org spec.
|
||||||
|
# Use .get() with both fallbacks so those sections are skipped gracefully.
|
||||||
steps_object = typing.cast(list[dict[str, str]], steps_object)
|
steps_object = typing.cast(list[dict[str, str]], steps_object)
|
||||||
return clean_instructions(
|
return clean_instructions(
|
||||||
functools.reduce(
|
functools.reduce(
|
||||||
operator.concat, # type: ignore
|
operator.concat, # type: ignore
|
||||||
[x["itemListElement"] for x in steps_object],
|
[x.get("itemListElement", x.get("item", [])) for x in steps_object],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -224,6 +224,55 @@ instruction_test_cases = (
|
|||||||
],
|
],
|
||||||
expected=None,
|
expected=None,
|
||||||
),
|
),
|
||||||
|
CleanerCase(
|
||||||
|
test_id="how to steps with empty section (e.g. NYT Cooking)",
|
||||||
|
input=[
|
||||||
|
{
|
||||||
|
"@type": "HowToSection",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "HowToSection",
|
||||||
|
"itemListElement": [
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction A",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction B",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction C",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
expected=None,
|
||||||
|
),
|
||||||
|
CleanerCase(
|
||||||
|
test_id="how to steps using 'item' key (schema.org alternate)",
|
||||||
|
input=[
|
||||||
|
{
|
||||||
|
"@type": "HowToSection",
|
||||||
|
"item": [
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction A",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction B",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "HowToStep",
|
||||||
|
"text": "Instruction C",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
expected=None,
|
||||||
|
),
|
||||||
CleanerCase(
|
CleanerCase(
|
||||||
test_id="excessive whitespace str (1)",
|
test_id="excessive whitespace str (1)",
|
||||||
input="Instruction A\n\nInstruction B\n\nInstruction C\n\n",
|
input="Instruction A\n\nInstruction B\n\nInstruction C\n\n",
|
||||||
|
|||||||
Reference in New Issue
Block a user