Skip to content

Commit

Permalink
feat: nested date support in sitemap (#73)
Browse files Browse the repository at this point in the history
Adds sitemap support for nested dates, like a review.datePublished field of a Product
  • Loading branch information
johnfraney authored Apr 13, 2024
1 parent aedd714 commit 7685c4b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
12 changes: 8 additions & 4 deletions blurry/sitemap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

import dpath

from blurry.settings import get_build_directory
from blurry.types import MarkdownFileData

Expand All @@ -20,10 +22,12 @@
def generate_sitemap_for_file_data_list(file_data_list: list[MarkdownFileData]) -> str:
sitemap_url_data = []
for file_data in file_data_list:
lastmod = file_data.front_matter.get(
"dateModified"
) or file_data.front_matter.get("datePublished")
lastmod_tag = f"<lastmod>{lastmod}</lastmod>" if lastmod else ""
lastmod = (
dpath.values(file_data.front_matter, "**/dateModified")
or dpath.values(file_data.front_matter, "**/datePublished")
or dpath.values(file_data.front_matter, "**/dateCreated")
)
lastmod_tag = f"<lastmod>{lastmod[0]}</lastmod>" if len(lastmod) else ""
url = file_data.front_matter.get("url")
sitemap_url_data.append({"lastmod_tag": lastmod_tag, "url": url})

Expand Down
37 changes: 22 additions & 15 deletions docs/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ toml = "^0.10.2"
typer = "^0.6.1"
htmlmin2 = "^0.1.13"
pydantic2-schemaorg = "^0.1.1"
dpath = "^2.1.6"

[tool.poetry.scripts]
blurry = 'blurry:main'
Expand Down
23 changes: 17 additions & 6 deletions tests/test_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@
MarkdownFileData(
front_matter=dict(datePublished=date(2021, 1, 1), url="/blog/a-post-1/"),
body="",
path=blog_path / "a-post-1",
path=blog_path / "a-post-1.md",
),
MarkdownFileData(
front_matter=dict(datePublished=date(2021, 3, 1), url="/blog/b-post-3/"),
body="",
path=blog_path / "b-post-3",
path=blog_path / "b-post-3.md",
),
MarkdownFileData(
front_matter=dict(dateCreated=date(2021, 2, 1), url="/blog/c-post-2/"),
body="",
path=blog_path / "c-post-2",
path=blog_path / "c-post-2.md",
),
# Nested dateCreated and dateModified
MarkdownFileData(
front_matter=dict(
dateCreated=date(2022, 1, 10),
dateModified=date(2022, 1, 13),
review=dict(
dateCreated=date(2022, 1, 10),
dateModified=date(2022, 1, 13),
),
url="/blog/c-post-4/",
),
body="",
path=blog_path / "c-post-4",
path=blog_path / "c-post-4.md",
),
]

Expand All @@ -39,6 +42,14 @@ def test_generate_sitemap_for_file_data_list():
)
assert sitemap_content.startswith('<?xml version="1.0" encoding="UTF-8"?>')
assert sitemap_content.endswith("</urlset>")
assert (
"<url><loc>/blog/a-post-1/</loc><lastmod>2021-01-01</lastmod></url>"
in sitemap_content
)
assert (
"<url><loc>/blog/c-post-2/</loc><lastmod>2021-02-01</lastmod></url>"
in sitemap_content
)
assert (
"<url><loc>/blog/c-post-4/</loc><lastmod>2022-01-13</lastmod></url>"
in sitemap_content
Expand Down

0 comments on commit 7685c4b

Please sign in to comment.