Skip to content

Commit 900486e

Browse files
committed
feat: tag maintainer visits, track code copies and add social cards
- ?internal=1 stores a flag in localStorage and every Google Analytics event then carries traffic_type=internal, so the Internal Traffic data filter can drop maintainer visits. ?internal=0 removes it. The project docs on the same origin read the same flag. - Copying a code block sends a copy_code event with the section it is in (snippet) and the file name above it (code_title). - Open Graph and Twitter tags on every page, with a 1200x630 card. - The 404 page is titled "Page not found" instead of "cpp-linter". - robots.txt lists the sitemaps of this site and the project docs. - Redirect pages for old URLs that Google Analytics recorded as 404s. - The home page title names what the site is: a C/C++ linter with clang-format and clang-tidy.
1 parent 2ac64d5 commit 900486e

7 files changed

Lines changed: 184 additions & 1 deletion

File tree

‎docs/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hide:
44
- toc
55
- feedback
66
template: home.html
7-
title: C/C++ Linting
7+
title: C/C++ Linter with clang-format and clang-tidy
88
description: clang-format and clang-tidy for C and C++ pull requests, pre-commit hooks and CI, with the LLVM version you pin.
99
---
1010

‎docs/overrides/404.html‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "main.html" %}
2+
3+
{#-
4+
Gives the 404 page a title of its own. With Material's default it was titled
5+
"cpp-linter" like a real page, so missing pages were hard to tell apart in
6+
Google Analytics.
7+
-#}
8+
9+
{% block htmltitle %}
10+
<title>Page not found - {{ config.site_name }}</title>
11+
{% endblock %}
12+
13+
{% block content %}
14+
<h1>404 - Not found</h1>
15+
{% endblock %}

‎docs/overrides/main.html‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,110 @@
11
{% extends "base.html" %}
22

3+
{#-
4+
Visits from the maintainers are tagged so that the Internal Traffic data
5+
filter in Google Analytics can drop them. Opening any page with ?internal=1
6+
stores a flag in localStorage, ?internal=0 removes it, and while the flag is
7+
there every event carries traffic_type=internal. The docs under
8+
/cpp-linter-action/, /clang-tools-pip/, /cpp-linter-rs/ and /cpp-linter/ are
9+
on the same origin and read the same flag. It runs before Material's own
10+
snippet, so the setting is queued ahead of the first page_view.
11+
-#}
12+
13+
{% block analytics %}
14+
{% if config.extra.analytics %}
15+
<script>
16+
try {
17+
var param = new URLSearchParams(location.search).get("internal");
18+
if (param === "1") localStorage.setItem("cpp-linter-internal", "1");
19+
if (param === "0") localStorage.removeItem("cpp-linter-internal");
20+
if (localStorage.getItem("cpp-linter-internal") === "1") {
21+
window.dataLayer = window.dataLayer || [];
22+
(function () { dataLayer.push(arguments); })("set", { traffic_type: "internal" });
23+
}
24+
} catch (e) {}
25+
</script>
26+
{% endif %}
27+
{{ super() }}
28+
{% endblock %}
29+
330
{#-
431
The site's fonts are self-hosted in docs/fonts and declared in shared.css:
532
Instrument Sans for text, JetBrains Mono for code and Bricolage Grotesque for
633
headings. theme.font is false in mkdocs.yml, so Material loads nothing itself.
734
Preloading the latin files for text and headings lets the browser fetch them
835
while it is still reading the stylesheets. The URLs must match the ones in
936
shared.css, and crossorigin is required for fonts even on the same origin.
37+
38+
The Open Graph and Twitter tags give links shared in chats and on social
39+
sites a title, a description and the card in static/og-image.png. Pages with
40+
no URL of their own, like the 404 page, leave og:url out.
1041
-#}
1142

1243
{% block extrahead %}
1344
{{ super() }}
1445
<link rel="preload" href="{{ 'fonts/instrument-sans-latin.woff2' | url }}" as="font" type="font/woff2" crossorigin>
1546
<link rel="preload" href="{{ 'fonts/bricolage-grotesque-latin.woff2' | url }}" as="font" type="font/woff2" crossorigin>
47+
{%- if page and page.meta and page.meta.title %}
48+
{%- set og_title = page.meta.title %}
49+
{%- elif page and page.title and not page.is_homepage %}
50+
{%- set og_title = page.title | striptags %}
51+
{%- else %}
52+
{%- set og_title = config.site_name %}
53+
{%- endif %}
54+
{%- set og_description = (page.meta.description if page and page.meta and page.meta.description) or config.site_description %}
55+
{%- set og_image = config.site_url ~ "static/og-image.png" %}
56+
<meta property="og:type" content="website">
57+
<meta property="og:site_name" content="{{ config.site_name }}">
58+
<meta property="og:title" content="{{ og_title }}">
59+
<meta property="og:description" content="{{ og_description }}">
60+
{%- if page and page.canonical_url %}
61+
<meta property="og:url" content="{{ page.canonical_url }}">
62+
{%- endif %}
63+
<meta property="og:image" content="{{ og_image }}">
64+
<meta property="og:image:width" content="1200">
65+
<meta property="og:image:height" content="630">
66+
<meta property="og:image:alt" content="cpp-linter: clang-format and clang-tidy for C/C++ pull requests, pre-commit hooks and CI">
67+
<meta name="twitter:card" content="summary_large_image">
68+
<meta name="twitter:title" content="{{ og_title }}">
69+
<meta name="twitter:description" content="{{ og_description }}">
70+
<meta name="twitter:image" content="{{ og_image }}">
71+
{% endblock %}
72+
73+
{#-
74+
Copying a code block is how a reader takes cpp-linter into a project, so each
75+
copy is sent to Google Analytics as a copy_code event. snippet names the
76+
section the block is in (the id of the nearest heading above it, or the
77+
block's own id on the home page), and code_title is the file name shown above
78+
the block, when it has one.
79+
-#}
80+
81+
{% block scripts %}
82+
{{ super() }}
83+
{% if config.extra.analytics %}
84+
<script>
85+
document.addEventListener("click", function (event) {
86+
var button = event.target.closest &&
87+
event.target.closest('.md-code__button[data-md-type="copy"], .md-clipboard, .cl-copy');
88+
if (!button) return;
89+
var block = button.closest(".highlight, .cl-window");
90+
var snippet = "";
91+
if (button.classList.contains("cl-copy")) {
92+
snippet = (button.getAttribute("data-clipboard-target") || "").replace(/^#/, "");
93+
} else if (block) {
94+
var headings = document.querySelectorAll("article h2[id], article h3[id]");
95+
for (var i = 0; i < headings.length; i++) {
96+
if (headings[i].compareDocumentPosition(block) & Node.DOCUMENT_POSITION_FOLLOWING) {
97+
snippet = headings[i].id;
98+
}
99+
}
100+
}
101+
var title = block && block.querySelector(".filename, .cl-window__file");
102+
window.dataLayer = window.dataLayer || [];
103+
(function () { dataLayer.push(arguments); })("event", "copy_code", {
104+
snippet: snippet || "(none)",
105+
code_title: title ? title.textContent.trim() : "(none)"
106+
});
107+
});
108+
</script>
109+
{% endif %}
16110
{% endblock %}

‎docs/robots.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The cpp-linter docs share this host: the organization site at the root and
2+
# each project's docs under its own path. This file covers all of them.
3+
User-agent: *
4+
Allow: /
5+
6+
Sitemap: https://cpp-linter.github.io/sitemap.xml
7+
Sitemap: https://cpp-linter.github.io/cpp-linter-action/sitemap.xml
8+
Sitemap: https://cpp-linter.github.io/clang-tools-pip/sitemap.xml
9+
Sitemap: https://cpp-linter.github.io/cpp-linter-rs/sitemap.xml
10+
Sitemap: https://cpp-linter.github.io/cpp-linter/sitemap.xml

‎docs/static/og-image.png‎

63 KB
Loading

‎hooks/redirects.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Write redirect pages for old URLs that still get visits.
2+
3+
GitHub Pages cannot send real redirects, so each old path gets a small page
4+
that forwards the browser with a meta refresh and tells search engines where
5+
the content lives now. The paths are the ones Google Analytics recorded as
6+
404s on this site: pages that moved into the sponsor page, short names for the
7+
getting started guide, and doc pages of cpp-linter-action, cpp-linter and
8+
cpp-linter-rs requested without their project prefix.
9+
10+
A key ending in .html is written as that file; any other key is a directory
11+
and gets an index.html. Targets are relative to site_url.
12+
"""
13+
14+
from __future__ import annotations
15+
16+
import html
17+
from pathlib import Path
18+
19+
REDIRECTS = {
20+
"docs/": "getting-started/",
21+
"merch/": "sponsor/#t-shirt-perk",
22+
"inputs-outputs/": "cpp-linter-action/inputs-outputs/",
23+
"examples/": "cpp-linter-action/examples/",
24+
"permissions/": "cpp-linter-action/permissions/",
25+
"pr-review-caveats/": "cpp-linter-action/pr-review-caveats/",
26+
"cli_args/": "cpp-linter/cli_args.html",
27+
"cpp_linter_rs/": "cpp-linter-rs/",
28+
"cpp_linter_rs/cli.html": "cpp-linter-rs/cli/",
29+
}
30+
31+
PAGE = """<!doctype html>
32+
<html lang="en">
33+
<head>
34+
<meta charset="utf-8">
35+
<title>Redirecting to {url}</title>
36+
<meta name="robots" content="noindex">
37+
<link rel="canonical" href="{url}">
38+
<meta http-equiv="refresh" content="0; url={url}">
39+
<script>location.replace({url_js})</script>
40+
</head>
41+
<body>
42+
<p>This page has moved to <a href="{url}">{url}</a>.</p>
43+
</body>
44+
</html>
45+
"""
46+
47+
48+
def on_post_build(config, **kwargs) -> None:
49+
site_dir = Path(config["site_dir"])
50+
site_url = config["site_url"]
51+
for source, target in REDIRECTS.items():
52+
path = site_dir / source
53+
if not source.endswith(".html"):
54+
path = path / "index.html"
55+
if path.exists():
56+
raise ValueError(f"redirect from {source} would replace a built page")
57+
url = site_url + target
58+
path.parent.mkdir(parents=True, exist_ok=True)
59+
path.write_text(
60+
PAGE.format(url=html.escape(url), url_js=repr(url)), encoding="utf-8"
61+
)

‎mkdocs.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ markdown_extensions:
8888
- toc:
8989
permalink: true
9090

91+
hooks:
92+
- hooks/redirects.py
93+
9194
plugins:
9295
- search
9396
- tags

0 commit comments

Comments
 (0)