@waldjs/content 0.1.0 → 0.2.0
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/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +79 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
export type Entry = {
|
|
2
2
|
slug: string;
|
|
3
|
+
locale?: string;
|
|
3
4
|
data: Record<string, unknown>;
|
|
4
5
|
body: string;
|
|
5
6
|
};
|
|
6
|
-
export
|
|
7
|
-
|
|
7
|
+
export type LocaleOptions = {
|
|
8
|
+
locale?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function readCollection(name: string, contentDir: string, options?: LocaleOptions): Promise<Entry[]>;
|
|
11
|
+
export declare function readEntry(collection: string, slug: string, contentDir: string, options?: LocaleOptions): Promise<Entry>;
|
|
12
|
+
export declare function readTranslations(collection: string, slug: string, contentDir: string): Promise<Record<string, Entry>>;
|
|
8
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,KAAK,GAAG;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA;AAgCD,wBAAsB,cAAc,CAClC,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,KAAK,EAAE,CAAC,CAkBlB;AAED,wBAAsB,SAAS,CAC7B,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,KAAK,CAAC,CA0BhB;AAED,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAQhC"}
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,92 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
1
2
|
import { readdir, readFile } from 'node:fs/promises';
|
|
2
3
|
import { join, basename } from 'node:path';
|
|
3
4
|
import matter from 'gray-matter';
|
|
4
5
|
import { marked } from 'marked';
|
|
5
|
-
|
|
6
|
+
// A collection subdirectory with a name like `nl`, `en` or `en-GB` holds that
|
|
7
|
+
// locale's entries. Anything else stays ignored, as before locales existed.
|
|
8
|
+
const LOCALE_PATTERN = /^[a-z]{2}(-[A-Z]{2})?$/;
|
|
9
|
+
function assertLocale(locale) {
|
|
10
|
+
if (!LOCALE_PATTERN.test(locale)) {
|
|
11
|
+
throw new Error(`Invalid locale "${locale}" — expected a code like "nl", "en" or "en-GB"`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function assertSlug(slug) {
|
|
15
|
+
if (slug === '' || slug.includes('/') || slug.includes('\\') || slug.startsWith('.')) {
|
|
16
|
+
throw new Error(`Invalid slug "${slug}" — expected a file name without path separators`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function listLocales(collection, contentDir) {
|
|
20
|
+
const dir = join(contentDir, collection);
|
|
21
|
+
if (!existsSync(dir))
|
|
22
|
+
return [];
|
|
23
|
+
const dirents = await readdir(dir, { withFileTypes: true });
|
|
24
|
+
return dirents
|
|
25
|
+
.filter(d => d.isDirectory() && LOCALE_PATTERN.test(d.name))
|
|
26
|
+
.map(d => d.name)
|
|
27
|
+
.sort();
|
|
28
|
+
}
|
|
29
|
+
async function listMarkdown(dir) {
|
|
30
|
+
return (await readdir(dir)).filter(f => f.endsWith('.md')).sort();
|
|
31
|
+
}
|
|
32
|
+
export async function readCollection(name, contentDir, options = {}) {
|
|
6
33
|
const dir = join(contentDir, name);
|
|
7
|
-
|
|
8
|
-
|
|
34
|
+
if (options.locale !== undefined) {
|
|
35
|
+
const { locale } = options;
|
|
36
|
+
assertLocale(locale);
|
|
37
|
+
const localeDir = join(dir, locale);
|
|
38
|
+
if (!existsSync(localeDir))
|
|
39
|
+
return [];
|
|
40
|
+
const files = await listMarkdown(localeDir);
|
|
41
|
+
return Promise.all(files.map(file => parseEntry(join(localeDir, file), locale)));
|
|
42
|
+
}
|
|
43
|
+
const files = await listMarkdown(dir);
|
|
44
|
+
const entries = await Promise.all(files.map(file => parseEntry(join(dir, file))));
|
|
45
|
+
for (const locale of await listLocales(name, contentDir)) {
|
|
46
|
+
entries.push(...(await readCollection(name, contentDir, { locale })));
|
|
47
|
+
}
|
|
48
|
+
return entries;
|
|
49
|
+
}
|
|
50
|
+
export async function readEntry(collection, slug, contentDir, options = {}) {
|
|
51
|
+
assertSlug(slug);
|
|
52
|
+
const dir = join(contentDir, collection);
|
|
53
|
+
const locales = await listLocales(collection, contentDir);
|
|
54
|
+
if (options.locale !== undefined) {
|
|
55
|
+
const { locale } = options;
|
|
56
|
+
assertLocale(locale);
|
|
57
|
+
const file = join(dir, locale, `${slug}.md`);
|
|
58
|
+
if (!existsSync(file)) {
|
|
59
|
+
const available = locales.length > 0 ? locales.join(', ') : 'none';
|
|
60
|
+
throw new Error(`Entry "${slug}" not found in collection "${collection}" for locale "${locale}" (available locales: ${available})`);
|
|
61
|
+
}
|
|
62
|
+
return parseEntry(file, locale);
|
|
63
|
+
}
|
|
64
|
+
const file = join(dir, `${slug}.md`);
|
|
65
|
+
if (existsSync(file))
|
|
66
|
+
return parseEntry(file);
|
|
67
|
+
if (locales.length > 0) {
|
|
68
|
+
throw new Error(`Collection "${collection}" has locales (${locales.join(', ')}) — pass { locale } to getEntry("${collection}", "${slug}")`);
|
|
69
|
+
}
|
|
70
|
+
throw new Error(`Entry "${slug}" not found in collection "${collection}"`);
|
|
9
71
|
}
|
|
10
|
-
export async function
|
|
11
|
-
|
|
12
|
-
|
|
72
|
+
export async function readTranslations(collection, slug, contentDir) {
|
|
73
|
+
assertSlug(slug);
|
|
74
|
+
const translations = {};
|
|
75
|
+
for (const locale of await listLocales(collection, contentDir)) {
|
|
76
|
+
const file = join(contentDir, collection, locale, `${slug}.md`);
|
|
77
|
+
if (existsSync(file))
|
|
78
|
+
translations[locale] = await parseEntry(file, locale);
|
|
79
|
+
}
|
|
80
|
+
return translations;
|
|
13
81
|
}
|
|
14
|
-
async function parseEntry(filePath) {
|
|
82
|
+
async function parseEntry(filePath, locale) {
|
|
15
83
|
const raw = await readFile(filePath, 'utf8');
|
|
16
84
|
const { data, content } = matter(raw);
|
|
17
85
|
const body = await marked(content);
|
|
18
86
|
const slug = basename(filePath, '.md');
|
|
19
|
-
|
|
87
|
+
const entryData = data;
|
|
88
|
+
return locale === undefined
|
|
89
|
+
? { slug, data: entryData, body }
|
|
90
|
+
: { slug, locale, data: entryData, body };
|
|
20
91
|
}
|
|
21
92
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;
|
|
1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