From d10620caf8d8bd5ec212ceb845499691af4669e6 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 3 Feb 2024 13:50:08 +0100 Subject: [PATCH 1/3] Add ld+json structured data option --- plugin/main.py | 29 +++++++++++++++++++++++++---- pyproject.toml | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/plugin/main.py b/plugin/main.py index 9ddb25c..b3d91ce 100644 --- a/plugin/main.py +++ b/plugin/main.py @@ -2,6 +2,7 @@ from subprocess import check_output from pathlib import Path +import json from bs4 import BeautifulSoup from mkdocs.config import config_options @@ -23,6 +24,7 @@ class MetaPlugin(BasePlugin): ("add_share_buttons", config_options.Type(bool, default=True)), # Add new argument ("add_dates", config_options.Type(bool, default=True)), # Add dates section ("add_authors", config_options.Type(bool, default=True)), # Add authors section + ("add_ld+json", config_options.Type(bool, default=False)), # Add structured data ) @staticmethod @@ -170,18 +172,18 @@ def on_post_page(self, output, page, config): soup.head.append(twitter_image_tag) # Add git information (dates and authors) to the footer, if enabled + git_info = self.get_git_info(page.file.abs_src_path) if self.config["add_dates"] or self.config["add_authors"]: - info = self.get_git_info(page.file.abs_src_path) - if info["creation_date"]: # otherwise page is missing git info and step should be skipped + if git_info["creation_date"]: # otherwise page is missing git info and step should be skipped div = '

' if self.config["add_dates"]: - div += f"Created {info['creation_date'][:10]}, Updated {info['last_modified_date'][:10]}" + div += f"Created {git_info['creation_date'][:10]}, Updated {git_info['last_modified_date'][:10]}" if self.config["add_authors"]: if self.config["add_dates"]: div += "
" - authors_str = ", ".join([f"{a[0]} ({a[2]})" for a in info["authors"]]) + authors_str = ", ".join([f"{a[0]} ({a[2]})" for a in git_info["authors"]]) div += f"Authors: {authors_str}" div += "
" @@ -226,4 +228,23 @@ def on_post_page(self, output, page, config): share_buttons = BeautifulSoup(share_buttons, "html.parser") self.insert_content(soup, share_buttons) + # Check if LD+JSON is enabled and add structured data to the + if self.config["add_ld+json"]: + ld_json_script = soup.new_tag("script", type="application/ld+json") + ld_json_content = { + "@context": "https://schema.org", + "@type": "Article", + "headline": page.title, + "image": [page.meta["image"]] if "image" in page.meta else [], + "datePublished": git_info["creation_date"], + "dateModified": git_info["last_modified_date"], + "author": [{ + "@type": "Organization", + "name": "Ultralytics", + "url": "https://ultralytics.com/" + }] + } + ld_json_script.string = json.dumps(ld_json_content) + soup.head.append(ld_json_script) + return str(soup) diff --git a/pyproject.toml b/pyproject.toml index d9d61e5..7f20f4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "mkdocs-ultralytics-plugin" -version = "0.0.42" +version = "0.0.43" description = "An MkDocs plugin that provides Ultralytics Docs customizations at https://docs.ultralytics.com." readme = "README.md" requires-python = ">=3.8" From da968e6eca1234cadb20c3c23214599cbc938cfd Mon Sep 17 00:00:00 2001 From: UltralyticsAssistant Date: Sat, 3 Feb 2024 12:50:39 +0000 Subject: [PATCH 2/3] Auto-format by https://ultralytics.com/actions --- plugin/main.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugin/main.py b/plugin/main.py index b3d91ce..bf7421c 100644 --- a/plugin/main.py +++ b/plugin/main.py @@ -238,11 +238,7 @@ def on_post_page(self, output, page, config): "image": [page.meta["image"]] if "image" in page.meta else [], "datePublished": git_info["creation_date"], "dateModified": git_info["last_modified_date"], - "author": [{ - "@type": "Organization", - "name": "Ultralytics", - "url": "https://ultralytics.com/" - }] + "author": [{"@type": "Organization", "name": "Ultralytics", "url": "https://ultralytics.com/"}], } ld_json_script.string = json.dumps(ld_json_content) soup.head.append(ld_json_script) From 369d229aac880e75fb9800f7b4eb7558b8737ee3 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 3 Feb 2024 13:55:53 +0100 Subject: [PATCH 3/3] Add ld+json structured data option --- plugin/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/main.py b/plugin/main.py index bf7421c..acb6504 100644 --- a/plugin/main.py +++ b/plugin/main.py @@ -24,7 +24,7 @@ class MetaPlugin(BasePlugin): ("add_share_buttons", config_options.Type(bool, default=True)), # Add new argument ("add_dates", config_options.Type(bool, default=True)), # Add dates section ("add_authors", config_options.Type(bool, default=True)), # Add authors section - ("add_ld+json", config_options.Type(bool, default=False)), # Add structured data + ("add_json_ld", config_options.Type(bool, default=True)), # Add JSON-LD structured data ) @staticmethod @@ -229,7 +229,7 @@ def on_post_page(self, output, page, config): self.insert_content(soup, share_buttons) # Check if LD+JSON is enabled and add structured data to the - if self.config["add_ld+json"]: + if self.config["add_json_ld"]: ld_json_script = soup.new_tag("script", type="application/ld+json") ld_json_content = { "@context": "https://schema.org",