fossbook 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/lib/mod/page_base.js +41 -0
- package/package.json +1 -1
- package/themes/archie/layouts/all_posts.html +1 -0
- package/themes/archie/layouts/home.html +1 -0
- package/themes/archie/layouts/page.html +1 -0
- package/themes/archie/layouts/post.html +1 -0
- package/themes/archie/layouts/tag.html +1 -0
- package/themes/archie/layouts/tag_list.html +1 -0
package/README.md
CHANGED
|
@@ -176,6 +176,12 @@ Fossbook generates a separate static page for each available language. The
|
|
|
176
176
|
browser loads only the selected language page; changing languages follows a
|
|
177
177
|
normal link and does not require JavaScript.
|
|
178
178
|
|
|
179
|
+
When a reader uses the language switcher, Fossbook remembers that choice in
|
|
180
|
+
the browser. A later visit to the default-language homepage redirects to the
|
|
181
|
+
remembered language's homepage. Direct links to posts and other pages are
|
|
182
|
+
never redirected, and language links continue to work when browser storage is
|
|
183
|
+
unavailable.
|
|
184
|
+
|
|
179
185
|
### Configuration
|
|
180
186
|
|
|
181
187
|
Add the languages supported by the site to `fossbook.config.js`:
|
package/lib/mod/page_base.js
CHANGED
|
@@ -48,6 +48,47 @@ module.exports = class PageBase {
|
|
|
48
48
|
return `<nav class="language-switcher" aria-label="Languages">${links.join("")}</nav>`;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
languagePreference(redirectFromDefaultHome = false) {
|
|
52
|
+
if (this.translations.length < 2) return "";
|
|
53
|
+
|
|
54
|
+
const supportedLanguages = this.translations.map((translation) => translation.language);
|
|
55
|
+
const languagePaths = Object.fromEntries(
|
|
56
|
+
this.translations.map((translation) => [
|
|
57
|
+
translation.language,
|
|
58
|
+
translation.path || translation.url,
|
|
59
|
+
]),
|
|
60
|
+
);
|
|
61
|
+
const shouldRedirect =
|
|
62
|
+
redirectFromDefaultHome && this.config.language === this.config.defaultLanguage;
|
|
63
|
+
|
|
64
|
+
return `<script>
|
|
65
|
+
(() => {
|
|
66
|
+
const storageKey = "fossbook-language";
|
|
67
|
+
const supportedLanguages = ${JSON.stringify(supportedLanguages).replace(/</g, "\\u003c")};
|
|
68
|
+
const languagePaths = ${JSON.stringify(languagePaths).replace(/</g, "\\u003c")};
|
|
69
|
+
try {
|
|
70
|
+
${shouldRedirect ? `const preferredLanguage = localStorage.getItem(storageKey);
|
|
71
|
+
if (preferredLanguage && preferredLanguage !== ${JSON.stringify(this.config.language)} && supportedLanguages.includes(preferredLanguage)) {
|
|
72
|
+
location.replace(languagePaths[preferredLanguage]);
|
|
73
|
+
return;
|
|
74
|
+
}` : ""}
|
|
75
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
76
|
+
document.querySelectorAll('.language-switcher a[hreflang]').forEach((link) => {
|
|
77
|
+
link.addEventListener("click", () => {
|
|
78
|
+
const language = link.getAttribute("hreflang");
|
|
79
|
+
if (supportedLanguages.includes(language)) {
|
|
80
|
+
try {
|
|
81
|
+
localStorage.setItem(storageKey, language);
|
|
82
|
+
} catch (error) {}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {}
|
|
88
|
+
})();
|
|
89
|
+
</script>`;
|
|
90
|
+
}
|
|
91
|
+
|
|
51
92
|
googleAnalytics(trackingId) {
|
|
52
93
|
return `<script async src="https://www.googletagmanager.com/gtag/js?id=${trackingId}"></script>
|
|
53
94
|
<script>
|
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
<meta name="description" content="${page.config.blogDescription}" />
|
|
13
13
|
<link rel="canonical" href="${page.url}" />
|
|
14
14
|
${page.alternateLanguageLinks()}
|
|
15
|
+
${page.languagePreference(data.prev === null)}
|
|
15
16
|
${page.openGraph("website")}
|
|
16
17
|
<!-- Twitter Card -->
|
|
17
18
|
${page.twitterCard("summary")}
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
<meta name="description" content="${page.config.blogDescription}" />
|
|
14
14
|
<link rel="canonical" href="${page.url}" />
|
|
15
15
|
${page.alternateLanguageLinks()}
|
|
16
|
+
${page.languagePreference()}
|
|
16
17
|
${page.openGraph("website")}
|
|
17
18
|
|
|
18
19
|
<meta name="twitter:card" content="summary_large_image" />
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
<!-- Google tag (gtag.js) -->
|
|
11
11
|
${page.config.googleAnalyticsID ? page.googleAnalytics(page.config.googleAnalyticsID) : ""}
|
|
12
12
|
<title>${page.config.blogName}: Entries tagged - ${data.tag}</title>
|
|
13
|
+
${page.languagePreference()}
|
|
13
14
|
${page.openGraph("website")}
|
|
14
15
|
</head>
|
|
15
16
|
<body>
|