From a4547bb15ba0b9cee058330eb80c006835b9c0dc Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Wed, 10 Apr 2024 00:32:03 +0000 Subject: [PATCH 1/5] fix global state and properly add recipe ld+json --- mealie/routes/spa/__init__.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/mealie/routes/spa/__init__.py b/mealie/routes/spa/__init__.py index bd1d6735ee3..a5cafeb605f 100644 --- a/mealie/routes/spa/__init__.py +++ b/mealie/routes/spa/__init__.py @@ -72,8 +72,26 @@ def inject_meta(contents: str, tags: list[MetaTag]) -> str: def inject_recipe_json(contents: str, schema: dict) -> str: - schema_as_html_tag = f"""""" - return contents.replace("", schema_as_html_tag + "\n", 1) + soup = BeautifulSoup(contents, "html.parser") + schema_as_json = json.dumps(jsonable_encoder(schema)) + + script_tags = soup.find_all("script", {"type": "application/ld+json"}) + for script_tag in script_tags: + try: + data = json.loads(script_tag.string) + if data.get("@type") == "Recipe": + # If the script tag exists and its @type is 'Recipe', replace its contents + script_tag.string = schema_as_json + break + except json.JSONDecodeError: + continue + else: + # If no script tag with @type 'Recipe' exists, create a new one and add it to the head + schema_as_html_tag = soup.new_tag("script", type="application/ld+json") + schema_as_html_tag.string = schema_as_json + soup.head.append(schema_as_html_tag) + + return str(soup) def content_with_meta(group_slug: str, recipe: Recipe) -> str: @@ -145,9 +163,11 @@ def content_with_meta(group_slug: str, recipe: Recipe) -> str: ] global __contents - __contents = inject_recipe_json(__contents, as_schema_org) - __contents = inject_meta(__contents, meta_tags) - return __contents + contents = __contents # make a local copy so we don't modify the global contents + contents = inject_recipe_json(contents, as_schema_org) + contents = inject_meta(contents, meta_tags) + + return contents def response_404(): From de53e9d657e6074865918e39e725661439f0c422 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Wed, 10 Apr 2024 00:32:13 +0000 Subject: [PATCH 2/5] fixed missing port forward in devcontainer --- .devcontainer/devcontainer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index fe02ce452ff..32e9cb65a8c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -42,6 +42,7 @@ "forwardPorts": [ 3000, 9000, + 9091, // used by docker production 24678 // used by nuxt when hot-reloading using polling ], // Use 'onCreateCommand' to run commands at the end of container creation. From ed8d642242ab23ba0815ce9d6ec38fc88ac60626 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Wed, 10 Apr 2024 00:38:47 +0000 Subject: [PATCH 3/5] updated parser --- mealie/routes/spa/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mealie/routes/spa/__init__.py b/mealie/routes/spa/__init__.py index a5cafeb605f..9381f27fd3c 100644 --- a/mealie/routes/spa/__init__.py +++ b/mealie/routes/spa/__init__.py @@ -72,7 +72,7 @@ def inject_meta(contents: str, tags: list[MetaTag]) -> str: def inject_recipe_json(contents: str, schema: dict) -> str: - soup = BeautifulSoup(contents, "html.parser") + soup = BeautifulSoup(contents, "lxml") schema_as_json = json.dumps(jsonable_encoder(schema)) script_tags = soup.find_all("script", {"type": "application/ld+json"}) From e9926254545e5184eb3ad8a3587d85ea852f1339 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:29:51 +0000 Subject: [PATCH 4/5] remove redundant comments --- mealie/routes/spa/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mealie/routes/spa/__init__.py b/mealie/routes/spa/__init__.py index 9381f27fd3c..cdc38506bec 100644 --- a/mealie/routes/spa/__init__.py +++ b/mealie/routes/spa/__init__.py @@ -80,13 +80,11 @@ def inject_recipe_json(contents: str, schema: dict) -> str: try: data = json.loads(script_tag.string) if data.get("@type") == "Recipe": - # If the script tag exists and its @type is 'Recipe', replace its contents script_tag.string = schema_as_json break except json.JSONDecodeError: continue else: - # If no script tag with @type 'Recipe' exists, create a new one and add it to the head schema_as_html_tag = soup.new_tag("script", type="application/ld+json") schema_as_html_tag.string = schema_as_json soup.head.append(schema_as_html_tag) From 5d5431bf9382ed5075ef85433c548408a0718281 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Fri, 12 Apr 2024 02:30:26 +0000 Subject: [PATCH 5/5] reverted metadata injection --- mealie/routes/spa/__init__.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/mealie/routes/spa/__init__.py b/mealie/routes/spa/__init__.py index cdc38506bec..3ec82eaea24 100644 --- a/mealie/routes/spa/__init__.py +++ b/mealie/routes/spa/__init__.py @@ -72,24 +72,8 @@ def inject_meta(contents: str, tags: list[MetaTag]) -> str: def inject_recipe_json(contents: str, schema: dict) -> str: - soup = BeautifulSoup(contents, "lxml") - schema_as_json = json.dumps(jsonable_encoder(schema)) - - script_tags = soup.find_all("script", {"type": "application/ld+json"}) - for script_tag in script_tags: - try: - data = json.loads(script_tag.string) - if data.get("@type") == "Recipe": - script_tag.string = schema_as_json - break - except json.JSONDecodeError: - continue - else: - schema_as_html_tag = soup.new_tag("script", type="application/ld+json") - schema_as_html_tag.string = schema_as_json - soup.head.append(schema_as_html_tag) - - return str(soup) + schema_as_html_tag = f"""""" + return contents.replace("", schema_as_html_tag + "\n", 1) def content_with_meta(group_slug: str, recipe: Recipe) -> str: