Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
Alexander Borsuk
f898e5d7e5 Serve localized donation page
Signed-off-by: Alexander Borsuk <me@alex.bio>
2022-11-20 19:16:32 +01:00
Alexander Borsuk
3dc2b85125 Upgrade node deps and install CF page functions prerequisites
Signed-off-by: Alexander Borsuk <me@alex.bio>
2022-11-20 19:13:58 +01:00
6 changed files with 2924 additions and 588 deletions

View file

@ -6,6 +6,8 @@ This website is build with [Zola](https://www.getzola.org/) and Cloudflare Pages
Use `zola serve` for a local preview or `zola build` to generate static site in the `public` folder.
To test Clouldflare Functions locally, use `npx wrangler pages dev public` after doing `zola build`.
`npm i && npm run format` will pretty-format Markdown and scss.
Upgrade npm dependencies with `npm run upgrade`, make sure that you have installed `npm-check-updates` package.

39
functions/donate.ts Normal file
View file

@ -0,0 +1,39 @@
// Redirects users from the default en donate page to the appropriate localized version.
interface Env {
KV: KVNamespace;
}
// TODO: Detect if donation page already has translation in resources automatically,
// without hard-coded list
const localizedLanguages = ['ru', 'it', 'tr'];
// To parse accept-language like this:
// fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
// en-AU,en;q=0.8,be;q=0.6,ru;q=0.4,uk;q=0.2
const langCodeRegex = /[a-z]{2}/g;
// Returns user's preferred languages: ["en", "be", "ru"]
function extractLanguageList(acceptLanguage: string): string[] {
const langs = acceptLanguage.match(langCodeRegex);
return [...new Set(langs)]; // Filter duplicates.
}
// Should be invoked only for /donate or /donate/ routes
export const onRequest: PagesFunction<Env> = async (context) => {
console.log('Donate handler hit url:' + context.request.url);
const acceptLanguage = context.request.headers.get('accept-language');
if (acceptLanguage) {
// Is there a localized version?
const languages = extractLanguageList(acceptLanguage);
const filtered = localizedLanguages.filter(lang => languages.includes(lang));
if (filtered.length) {
const url = new URL(context.request.url);
console.log('Old pathname: ' + url.pathname);
url.pathname = '/' + filtered[0] + url.pathname;
return context.env.ASSETS.fetch(url);
}
}
// Return default en version.
return context.env.ASSETS.fetch(context.request.url);
}

8
functions/tsconfig.json Normal file
View file

@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"]
}
}

3446
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -7,13 +7,15 @@
"doc": "docs"
},
"devDependencies": {
"js-beautify": "^1.14.6",
"node-html-markdown": "^1.2.0",
"node-html-parser": "^6.1.1",
"@cloudflare/workers-types": "^4.20221111.1",
"js-beautify": "^1.14.7",
"node-html-markdown": "^1.2.2",
"node-html-parser": "^6.1.4",
"prettier": "^2.7.1",
"stylelint": "^14.13.0",
"stylelint": "^14.15.0",
"stylelint-config-recommended": "^9.0.0",
"stylelint-config-standard": "^28.0.0"
"stylelint-config-standard": "^29.0.0",
"wrangler": "^2.4.2"
},
"scripts": {
"format": "stylelint --fix ./sass/*.scss && prettier --write *.md content/**/*.md && js-beautify -r templates/base.html",

5
static/_routes.json Normal file
View file

@ -0,0 +1,5 @@
{
"version": 1,
"include": ["/donate", "/donate/"],
"exclude": []
}