Skip to content

Commit c0a6bbe

Browse files
authored
Merge pull request #17 from practical-python-org/SEO
google search console and SEO things
2 parents 89f9f61 + a4dd790 commit c0a6bbe

5 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
entry: uv run zensical build --strict --clean
2727
language: system
2828
pass_filenames: false
29-
files: ^(docs/|zensical\.toml|pyproject\.toml|uv\.lock)
29+
files: ^(docs/|overrides/|zensical\.toml|pyproject\.toml|uv\.lock)
3030

3131
- repo: https://github.com/swiss-ai-center/mdwrap
3232
rev: 0.2.5

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ uv run --dev pre-commit run --all-files
4343
docs/
4444
├── index.md landing page
4545
├── CNAME custom domain, copied to the site root on build
46+
├── robots.txt Tells crawlers and bots where to go
4647
├── stylesheets/ colour, font and hero overrides
4748
├── static/ logo and images
4849
├── getting-started/ the entry point for new members
@@ -53,6 +54,8 @@ docs/
5354
│ ├── our-projects/ software the community maintains
5455
│ └── build-something/ project challenges for members
5556
└── code-adventure/ landing page for the Code Adventure puzzles!
57+
overrides/
58+
└── main.html theme override adding Open Graph and other SEO tags
5659
scripts/
5760
└── refresh_member_count.py updates the Discord member count before a deploy build
5861
zensical.toml all site configuration, including the nav

‎docs/robots.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
User-agent: *
2+
Allow: /
3+
4+
Sitemap: https://practicalpython.org/sitemap.xml

‎overrides/main.html‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{#-
2+
Theme override: extends Zensical's base template to add the SEO tags it
3+
doesn't emit on its own. Wired up by custom_dir in zensical.toml.
4+
-#}
5+
{% extends "base.html" %}
6+
7+
{#- page.is_homepage is never set by Zensical, so compare URLs instead. -#}
8+
{% set is_homepage = page.canonical_url == config.site_url %}
9+
10+
{#- The nav labels the homepage "Home", which would otherwise become its title. -#}
11+
{% block htmltitle %}
12+
{% if is_homepage %}
13+
<title>{{ config.site_name }}</title>
14+
{% else %}
15+
{{ super() }}
16+
{% endif %}
17+
{% endblock %}
18+
19+
{% block extrahead %}
20+
{% if is_homepage %}
21+
{% set title = config.site_name %}
22+
{% elif page.meta and page.meta.title %}
23+
{% set title = page.meta.title %}
24+
{% elif page.title %}
25+
{% set title = page.title | striptags %}
26+
{% else %}
27+
{% set title = config.site_name %}
28+
{% endif %}
29+
{% if page.meta and page.meta.description %}
30+
{% set description = page.meta.description %}
31+
{% else %}
32+
{% set description = config.site_description %}
33+
{% endif %}
34+
{% set is_post = page.meta and page.meta.date %}
35+
36+
{#- Open Graph: the preview card Discord, Slack and friends render for a link. -#}
37+
<meta property="og:type" content="{{ 'article' if is_post else 'website' }}">
38+
<meta property="og:site_name" content="{{ config.site_name }}">
39+
<meta property="og:title" content="{{ title }}">
40+
<meta property="og:description" content="{{ description }}">
41+
{% if page.canonical_url %}
42+
<meta property="og:url" content="{{ page.canonical_url }}">
43+
{% endif %}
44+
{% if is_post %}
45+
<meta property="article:published_time" content="{{ page.meta.date }}">
46+
{% endif %}
47+
<meta name="twitter:card" content="summary">
48+
49+
{#- Tells Google the site's name, shown above results instead of the bare domain. -#}
50+
{% if is_homepage %}
51+
<script type="application/ld+json">
52+
{
53+
"@context": "https://schema.org",
54+
"@type": "WebSite",
55+
"name": {{ config.site_name | tojson }},
56+
"url": {{ config.site_url | tojson }}
57+
}
58+
</script>
59+
{% endif %}
60+
{% endblock %}

‎zensical.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ nav = [
5959

6060
[project.theme]
6161
language = "en"
62+
# Template overrides: overrides/main.html adds Open Graph tags and the like.
63+
custom_dir = "overrides"
6264
features = [
6365
"content.action.edit",
6466
"content.code.annotate",

0 commit comments

Comments
 (0)