Skip to content

Commit

Permalink
Add hreflang
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jun 25, 2024
1 parent 171eb21 commit 49d1485
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions book/theme/head.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,43 @@
// See these pages for details:
// https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls
// https://developers.google.com/search/docs/crawling-indexing/javascript/javascript-seo-basics
let base = "https://doc.veryl-lang.org/book";
function gen_canonical_href(lang) {
let base = "https://doc.veryl-lang.org/book";
let canonical_href;
if (lang == "en") {
canonical_href = `${base}/{{ path }}`;
} else {
canonical_href = `${base}/${lang}/{{ path }}`;
}
canonical_href = canonical_href.slice(0, -"md".length) + "html";
if (canonical_href.endsWith("/index.html")) {
canonical_href = canonical_href.slice(0, -"index.html".length);
}
return canonical_href;
}
{{#if (eq language "en")}}
let canonical_href = `${base}/{{ path }}`;
const canonical_href = gen_canonical_href("en");
{{else}}
let canonical_href = `${base}/{{ language }}/{{ path }}`;
const canonical_href = gen_canonical_href("{{ language }}");
{{/if}}
// mdbook gives us a string ending in ".md", we replace it with ".html":
canonical_href = canonical_href.slice(0, -"md".length) + "html";
if (canonical_href.endsWith("/index.html")) {
canonical_href = canonical_href.slice(0, -"index.html".length);
}
let link = document.createElement("link");
link.rel = "canonical";
link.href = canonical_href;
document.head.appendChild(link);
const langs = ["en", "ja"];
for (const lang of langs) {
const canonical_href = gen_canonical_href(lang);
let link = document.createElement("link");
link.rel = "alternate";
link.hreflang = lang;
link.href = canonical_href;
document.head.appendChild(link);
}
})()
</script>

Expand Down

0 comments on commit 49d1485

Please sign in to comment.