Skip to content
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
107 changes: 107 additions & 0 deletions .github/scripts/check_doc_metadata_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from html.parser import HTMLParser
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from xml.etree import ElementTree


class Metadata(HTMLParser):
def __init__(self, html):
super().__init__()
self.canonical = None
self.robots = ""
self.describedby = None
self.schema = None
self.json_text = None
self.feed(html)

def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "link" and attrs.get("rel") == "canonical":
self.canonical = attrs.get("href")
if tag == "link" and attrs.get("rel") == "describedby":
self.describedby = attrs.get("href")
if tag == "meta" and attrs.get("name") == "robots":
self.robots = attrs.get("content", "")
if tag == "script" and attrs.get("type") == "application/ld+json":
self.json_text = ""

def handle_data(self, data):
if self.json_text is not None:
self.json_text += data

def handle_endtag(self, tag):
if tag == "script" and self.json_text is not None:
self.schema = json.loads(self.json_text)
self.json_text = None


class DocumentationMetadataTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory()
cls.addClassCleanup(cls.temp.cleanup)
cls.sites = {}
root = Path(__file__).resolve().parents[2]
for version in ("1.1.1", "dev", "1.2.0-rc1"):
site = Path(cls.temp.name) / version
result = subprocess.run(
[sys.executable, "-m", "mkdocs", "build", "--site-dir", str(site)],
cwd=root,
env={**os.environ, "MIKE_DOCS_VERSION": version},
capture_output=True,
text=True,
timeout=60,
)
if result.returncode:
raise RuntimeError(result.stdout + result.stderr)
cls.sites[version] = site

def test_release_has_versioned_canonicals_schema_and_populated_sitemap(self):
site = self.sites["1.1.1"]
url = "https://documentdb.io/documentdb-kubernetes-operator/1.1.1/v1/"
page = Metadata((site / "v1/index.html").read_text(encoding="utf-8"))
self.assertEqual(page.canonical, url)
self.assertEqual(page.schema["url"], url)
self.assertEqual(page.schema["@type"], "TechArticle")
self.assertEqual(page.schema["about"]["softwareVersion"], "1.1.1")
self.assertEqual(
page.describedby,
"https://documentdb.io/documentdb-kubernetes-operator/1.1.1/llms.txt",
)
self.assertNotIn("noindex", page.robots)
locations = [
node.text
for node in ElementTree.parse(site / "sitemap.xml").iter(
"{http://www.sitemaps.org/schemas/sitemap/0.9}loc"
)
]
self.assertIn(url, locations)
self.assertTrue(all(location.startswith(url.rsplit("v1/", 1)[0]) for location in locations))
self.assertFalse(any("/preview/" in location for location in locations))

def test_unreleased_snapshots_are_labeled_noindex_and_not_in_sitemaps(self):
for version, label in (("dev", "Development"), ("1.2.0-rc1", "Prerelease")):
with self.subTest(version=version):
site = self.sites[version]
html = (site / "v1/index.html").read_text(encoding="utf-8")
page = Metadata(html)
self.assertIn("noindex", page.robots)
self.assertIn(f"{label} documentation", html)
self.assertIn(f"/{version}/v1/", page.canonical)
self.assertEqual(
list(ElementTree.parse(site / "sitemap.xml").iter(
"{http://www.sitemaps.org/schemas/sitemap/0.9}loc"
)),
[],
)
dev = Metadata((self.sites["dev"] / "v1/index.html").read_text(encoding="utf-8"))
self.assertNotIn("softwareVersion", dev.schema["about"])


if __name__ == "__main__":
unittest.main()
10 changes: 10 additions & 0 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ jobs:
- name: Install dependencies
run: pip install "mkdocs-material==9.5.19" "mike==2.1.3" "mkdocs-redirects==1.2.2"

- name: Select documentation snapshot for build checks
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
run: |
if [[ "${{ github.event_name }}" == "release" ]]; then
echo "MIKE_DOCS_VERSION=$RELEASE_TAG" >> "$GITHUB_ENV"
else
echo "MIKE_DOCS_VERSION=dev" >> "$GITHUB_ENV"
fi

- name: Configure Git for mike
run: |
git config user.name "github-actions[bot]"
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ on:
- README.md
- mkdocs.yml
- 'docs/operator-public-documentation/**'
- '.github/scripts/check_doc_links*.py'
- 'docs/overrides/**'
- '.github/scripts/check_doc_*.py'
- '.github/workflows/*docs.yml'
push:
branches: [main]
paths:
- README.md
- mkdocs.yml
- 'docs/operator-public-documentation/**'
- '.github/scripts/check_doc_links*.py'
- 'docs/overrides/**'
- '.github/scripts/check_doc_*.py'
- '.github/workflows/*docs.yml'
workflow_dispatch:

Expand All @@ -37,6 +39,8 @@ jobs:
- name: Test link checker
run: python -m unittest discover -s .github/scripts -p '*_test.py'
- name: Build and check candidate entrypoints
env:
MIKE_DOCS_VERSION: dev
run: |
mkdocs build
python .github/scripts/check_doc_links.py --site-dir site
Expand Down
50 changes: 50 additions & 0 deletions docs/overrides/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% extends "base.html" %}

{% block extrahead %}
{{ super() }}
{% set version = config.extra.docs_version %}
{% if version == "dev" or "-" in version %}
<meta name="robots" content="noindex, follow">
{% endif %}
<link rel="describedby" href="{{ config.site_url.rstrip('/') ~ '/llms.txt' }}">
{% if page and page.file.src_uri != "index.md" %}
{% set software = {
"@type": "SoftwareApplication",
"name": "DocumentDB Kubernetes Operator",
"url": config.repo_url,
"applicationCategory": "DeveloperApplication",
"operatingSystem": "Kubernetes",
"license": "https://opensource.org/license/mit/"
} %}
{% if version != "dev" %}
{% set _ = software.update({"softwareVersion": version}) %}
{% endif %}
<script type="application/ld+json">
{{ {
"@context": "https://schema.org",
"@type": "TechArticle",
"headline": page.title,
"description": page.meta.get("description", config.site_description),
"url": page.canonical_url,
"inLanguage": "en",
"isPartOf": {
"@type": "WebSite",
"name": config.site_name,
"url": config.site_url.rstrip('/') ~ '/'
},
"about": software
} | tojson }}
</script>
{% endif %}
{% endblock %}

{% block content %}
{% set version = config.extra.docs_version %}
{% if version == "dev" or "-" in version %}
<div class="admonition warning">
<p class="admonition-title">{{ "Development" if version == "dev" else "Prerelease" }} documentation</p>
<p>This snapshot contains unreleased guidance. For stable installation instructions, use the <a href="https://documentdb.io/documentdb-kubernetes-operator/latest/">released documentation</a>.</p>
</div>
{% endif %}
{{ super() }}
{% endblock %}
13 changes: 13 additions & 0 deletions docs/overrides/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% if config.extra.docs_version != "dev" and "-" not in config.extra.docs_version %}
{% for file in pages %}
{% if not file.page.is_link and file.src_uri != "index.md" and file.page.canonical_url %}
<url>
<loc>{{ file.page.canonical_url | e }}</loc>
{% if file.page.update_date %}<lastmod>{{ file.page.update_date }}</lastmod>{% endif %}
</url>
{% endif %}
{% endfor %}
{% endif %}
</urlset>
7 changes: 7 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
site_name: DocumentDB-Kubernetes-Operator
site_url: https://documentdb.io/documentdb-kubernetes-operator/
site_description: Deploy and manage DocumentDB clusters on Kubernetes with the DocumentDB operator and CloudNative-PG.
repo_url: https://github.com/documentdb/documentdb-kubernetes-operator
edit_uri: edit/main/docs/operator-public-documentation/
docs_dir: docs/operator-public-documentation
Expand All @@ -7,6 +9,7 @@ docs_dir: docs/operator-public-documentation
# the readthedocs theme does not support it.
theme:
name: material
custom_dir: docs/overrides
features:
- content.code.copy
- content.code.annotate
Expand All @@ -22,6 +25,7 @@ theme:
# to the most recent release. Unreleased changes on main deploy to /dev/ only.
# See .github/workflows/deploy_docs.yml for the deployment logic.
extra:
docs_version: !ENV [MIKE_DOCS_VERSION, dev]
version:
provider: mike
default: latest
Expand Down Expand Up @@ -72,6 +76,9 @@ nav:
- API Reference: v1/api-reference.md

plugins:
# mike appends the deployed snapshot version to site_url. Keep numbered
# snapshots canonical to themselves; latest remains a release alias.
- mike
- search
# Keep links to the pre-1.1 docs layout (preview/...) working after the
# folder moved to v1/ with the documentdb.io/v1 API graduation.
Expand Down
Loading