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

Add ld+json structured data option #43

Merged
merged 4 commits into from
Feb 3, 2024
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
25 changes: 21 additions & 4 deletions plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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_json_ld", config_options.Type(bool, default=True)), # Add JSON-LD structured data
)

@staticmethod
Expand Down Expand Up @@ -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 = '<div class="git-info" style="font-size: 0.8em; text-align: right; margin-bottom: 10px;"><br>'

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 += "<br>"
authors_str = ", ".join([f"<a href='{a[1]}'>{a[0]}</a> ({a[2]})" for a in info["authors"]])
authors_str = ", ".join([f"<a href='{a[1]}'>{a[0]}</a> ({a[2]})" for a in git_info["authors"]])
div += f"Authors: {authors_str}"

div += "</div>"
Expand Down Expand Up @@ -226,4 +228,19 @@ 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 <head>
if self.config["add_json_ld"]:
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): Ensure that the JSON-LD content is properly escaped to prevent injection attacks or issues when rendering the script tag content.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sourcery-ai how can I do this?

soup.head.append(ld_json_script)

return str(soup)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down