-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathcontent.js
More file actions
31 lines (30 loc) · 864 Bytes
/
content.js
File metadata and controls
31 lines (30 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Fetch and parse a markdown document with optional JSON FrontMatter.
* @returns {Promise<import('./../types.d.ts').ContentData>}
*/
export async function getContent([lang, name]) {
let path = `/content/${lang}`,
url = `${path}/${name.replace(/^\//, '')}`,
ext = (url.match(/\.([a-z]+)$/i) || [])[1];
if (!ext)
url = url.endsWith('/') ? url.replace(/\/$/, '.json') : url + '.json';
let fallback = false;
return await fetch(url, { credentials: 'include', mode: 'no-cors' })
.then(r => {
// fall back to english
if (!r.ok && lang != 'en') {
fallback = true;
return fetch(url.replace(/content\/[^/]+\//, 'content/en/'));
}
return r;
})
.then(r => {
if (r.ok) return r;
return fetch(`${path}/${r.status}.json`);
})
.then(r => r.json())
.then(data => {
data.meta.isFallback = fallback;
return data;
});
}