brease-client 0.4.0 → 0.6.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/README.md +49 -14
- package/dist/chunk-CQZLTZGN.js +79 -0
- package/dist/{chunk-N6C2X6KA.js → chunk-OA7GXHT3.js} +19 -1
- package/dist/cli.cjs +126 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +51 -0
- package/dist/index.cjs +102 -0
- package/dist/index.d.cts +30 -1
- package/dist/index.d.ts +30 -1
- package/dist/index.js +2 -1
- package/dist/next.js +2 -1
- package/package.json +5 -15
- package/dist/react.cjs +0 -110
- package/dist/react.d.cts +0 -43
- package/dist/react.d.ts +0 -43
- package/dist/react.js +0 -81
package/README.md
CHANGED
|
@@ -114,7 +114,7 @@ Brease works with any routing: `getPage()` and friends don't care how a site is
|
|
|
114
114
|
```
|
|
115
115
|
app/
|
|
116
116
|
[[...path]]/
|
|
117
|
-
layout.tsx root layout: <html lang>,
|
|
117
|
+
layout.tsx root layout: <html lang>, the locale context for the page
|
|
118
118
|
page.tsx every page in every locale; the page's content picks its template
|
|
119
119
|
not-found.tsx
|
|
120
120
|
og/[[...path]]/route.tsx share images (image files can't sit inside an optional catch-all)
|
|
@@ -124,8 +124,8 @@ brease/templates.tsx { home: …, article: … }, the site's own page
|
|
|
124
124
|
|
|
125
125
|
```tsx
|
|
126
126
|
// app/[[...path]]/layout.tsx (replaces app/layout.tsx)
|
|
127
|
-
import { BreaseLocaleProvider } from 'brease-client/react'
|
|
128
127
|
import { brease } from '@/lib/brease'
|
|
128
|
+
import { LocaleProvider } from '@/components/locale' // the site's own React context around the value
|
|
129
129
|
|
|
130
130
|
type Props = { children: React.ReactNode; params: Promise<{ path?: string[] }> }
|
|
131
131
|
|
|
@@ -140,7 +140,7 @@ export default async function RootLayout({ children, params }: Props) {
|
|
|
140
140
|
return (
|
|
141
141
|
<html lang={context.locale}>
|
|
142
142
|
<body>
|
|
143
|
-
<
|
|
143
|
+
<LocaleProvider value={context}>{children}</LocaleProvider>
|
|
144
144
|
</body>
|
|
145
145
|
</html>
|
|
146
146
|
)
|
|
@@ -180,22 +180,21 @@ export default async function Page({ params }: Props) {
|
|
|
180
180
|
}
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
The package stays framework-free: it gives the data and the logic, and the site renders them with its own framework.
|
|
184
184
|
|
|
185
|
-
```
|
|
186
|
-
import {
|
|
187
|
-
|
|
188
|
-
const { locale, href } = useLocale()
|
|
189
|
-
href('') // this locale's home: '/', '/en', …
|
|
190
|
-
href('about', 'de') // '/de/about'
|
|
185
|
+
```ts
|
|
186
|
+
import { localeHref, localeLinks } from 'brease-client'
|
|
191
187
|
|
|
192
|
-
|
|
193
|
-
|
|
188
|
+
const context = await brease.localeContext(path) // { locale, settings, alternates, homes }
|
|
189
|
+
localeLinks(context) // [{ locale: 'hu', label: 'Magyar', href: '/rolunk', current: true }, …]
|
|
190
|
+
localeHref(context.settings, 'about', 'de') // '/de/about'
|
|
194
191
|
```
|
|
195
192
|
|
|
196
|
-
|
|
193
|
+
A locale switcher is then a few lines in any framework: a list of `<a href={l.href} hreflang={l.locale}>{l.label}</a>`, with `aria-current` on the current one, hidden while `localeLinks()` returns fewer than two links. Share `context` with the components that need it the framework's own way (a React context, Astro props, a template variable).
|
|
194
|
+
|
|
195
|
+
- `localeLinks()` links each locale's version of the current page, else that locale's home page; locales without content yet are left out.
|
|
197
196
|
- Everything that read a fixed locale (`getPage(…, { locale: 'hu' })`, `getNavigation`, `site.seo.hu`) takes it from the route instead.
|
|
198
|
-
- Links inside content are already locale URLs. Build other internal links with `
|
|
197
|
+
- Links inside content are already locale URLs. Build other internal links with `localeHref(settings, slug, locale)`.
|
|
199
198
|
- Text written into components (button labels, captions) stays as it is; moving it into Brease content is a separate change.
|
|
200
199
|
- `next.config`: `redirects: breaseRedirects()` (from `brease-client/next`) serves the redirects managed in Brease.
|
|
201
200
|
|
|
@@ -244,11 +243,47 @@ The Brease editor's Edit tool finds content through `data-brease` attributes:
|
|
|
244
243
|
|
|
245
244
|
- `data-brease="hero.title"` on an element that renders a page content field (a path into `page.content`)
|
|
246
245
|
- `data-brease="nav:footer"` on the element that wraps a navigation menu; clicking it opens that navigation's editor
|
|
246
|
+
- `data-brease-block="<name>"` on a section's root: which component renders it (a hint for the AI)
|
|
247
|
+
- `data-brease-sections="sections"` and `data-brease-section="<id>"`: the section list and its sections (see Sections)
|
|
247
248
|
|
|
248
249
|
```tsx
|
|
249
250
|
<nav data-brease="nav:footer">{footer.items.map(/* … */)}</nav>
|
|
250
251
|
```
|
|
251
252
|
|
|
253
|
+
## Sections
|
|
254
|
+
|
|
255
|
+
Editors can add, move, duplicate and delete page sections in the Brease editor when a site follows the sections convention:
|
|
256
|
+
|
|
257
|
+
```
|
|
258
|
+
src/brease/sections/ (or brease/sections/)
|
|
259
|
+
index.tsx type -> component map, renders content.sections in order; unknown types render nothing
|
|
260
|
+
testimonials/
|
|
261
|
+
section.json { "name": "Testimonials", "description": "…", "addable": true, "fields": { JSON Schema } }
|
|
262
|
+
Testimonials.tsx
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
- A page's sections are one list, `content.sections: [{ id, type, ...fields }]`, with the same ids in the same order in every locale. Fixed parts (a page title, header, footer, site-wide content) stay outside it.
|
|
266
|
+
- `fields` describes the section's fields; Brease builds new sections from it (lorem ipsum, a placeholder image, `default`s, `enum`s' first values, `minItems` list items). Images are `{ assetId, url, alt }`, links `{ label, href }`, rich text `{ "type": "string", "format": "html" }`.
|
|
267
|
+
- `"shared": true` keeps one content for every page that shows the type, on the site-wide page (`_global`) under `content.shared.<type>`; pages list only `{ id, type }` for it, and its markers use the other-page prefix (`<_global pageId>:shared.<type>.<field>`).
|
|
268
|
+
- Markers: the list's element carries `data-brease-sections="sections"`, each section's root `data-brease-section={id}` and `data-brease-block={type}`, fields inside `data-brease="sections.<index>.<field>"`.
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
import { type BreaseSection, sectionAttrs, sectionsAttrs } from 'brease-client'
|
|
272
|
+
|
|
273
|
+
export function Sections({ sections }: { sections: BreaseSection[] }) {
|
|
274
|
+
return (
|
|
275
|
+
<div {...sectionsAttrs}>
|
|
276
|
+
{sections.map((s, i) => {
|
|
277
|
+
const Component = map[s.type]
|
|
278
|
+
return Component ? <div key={s.id} {...sectionAttrs(s)}><Component section={s} index={i} /></div> : null
|
|
279
|
+
})}
|
|
280
|
+
</div>
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
`npx brease-client check` warns about folders without a valid `section.json`, types missing from the map file, a missing `data-brease-section`, unknown types in `brease-seed/` or `.brease/` content, and locales whose sections differ. Brease runs the same check before publishing.
|
|
286
|
+
|
|
252
287
|
## Images
|
|
253
288
|
|
|
254
289
|
```ts
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/sections.ts
|
|
2
|
+
var sectionsAttrs = { "data-brease-sections": "sections" };
|
|
3
|
+
function sectionAttrs(section) {
|
|
4
|
+
return { "data-brease-section": section.id, "data-brease-block": section.type };
|
|
5
|
+
}
|
|
6
|
+
var SECTION_FOLDERS = ["src/brease/sections", "brease/sections"];
|
|
7
|
+
var TYPE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
8
|
+
function checkSections(files, pages = {}) {
|
|
9
|
+
const warnings = [];
|
|
10
|
+
const folder = SECTION_FOLDERS.find((f) => Object.keys(files).some((p) => p.startsWith(`${f}/`)));
|
|
11
|
+
if (!folder) return warnings;
|
|
12
|
+
const inFolder = Object.entries(files).map(([p, text]) => [p.slice(folder.length + 1), text]);
|
|
13
|
+
const types = [
|
|
14
|
+
...new Set(
|
|
15
|
+
inFolder.map(([p]) => p.split("/")).filter((s) => s.length > 1).map((s) => s[0])
|
|
16
|
+
)
|
|
17
|
+
];
|
|
18
|
+
const map = inFolder.find(([p]) => /^index\.[cm]?[jt]sx?$/.test(p));
|
|
19
|
+
if (!map)
|
|
20
|
+
warnings.push(`${folder} has no map file (index.tsx, index.ts or index.js) rendering the section types`);
|
|
21
|
+
const marked = inFolder.some(
|
|
22
|
+
([, text]) => text.includes("data-brease-section") || text.includes("sectionAttrs(")
|
|
23
|
+
);
|
|
24
|
+
if (!marked)
|
|
25
|
+
warnings.push(
|
|
26
|
+
`No component in ${folder} renders data-brease-section (or sectionAttrs), so editors can't add sections`
|
|
27
|
+
);
|
|
28
|
+
for (const type of types) {
|
|
29
|
+
if (!TYPE_NAME.test(type)) {
|
|
30
|
+
warnings.push(`${folder}/${type}: use lowercase letters, numbers and - in section folder names`);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const json = files[`${folder}/${type}/section.json`];
|
|
34
|
+
if (json === void 0) {
|
|
35
|
+
warnings.push(`${folder}/${type} has no section.json`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const meta = JSON.parse(json);
|
|
40
|
+
if (typeof meta.name !== "string" || !meta.name.trim())
|
|
41
|
+
warnings.push(`${folder}/${type}/section.json has no name`);
|
|
42
|
+
if (meta.fields !== void 0 && (typeof meta.fields !== "object" || meta.fields === null))
|
|
43
|
+
warnings.push(`${folder}/${type}/section.json: fields must be a JSON Schema object`);
|
|
44
|
+
} catch {
|
|
45
|
+
warnings.push(`${folder}/${type}/section.json is not valid JSON`);
|
|
46
|
+
}
|
|
47
|
+
if (map && !map[1].includes(type)) warnings.push(`${type} is in ${folder} but not in its map file`);
|
|
48
|
+
}
|
|
49
|
+
const known = new Set(types);
|
|
50
|
+
for (const [pageId, locales] of Object.entries(pages)) {
|
|
51
|
+
let reference = null;
|
|
52
|
+
for (const [locale, file] of Object.entries(locales)) {
|
|
53
|
+
const list = file.content?.sections;
|
|
54
|
+
if (list === void 0) continue;
|
|
55
|
+
if (!Array.isArray(list)) {
|
|
56
|
+
warnings.push(`${pageId} (${locale}): sections is not a list`);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const ids = /* @__PURE__ */ new Set();
|
|
60
|
+
for (const item of list) {
|
|
61
|
+
if (typeof item?.id !== "string" || typeof item.type !== "string") {
|
|
62
|
+
warnings.push(`${pageId} (${locale}): a section has no id or type`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (ids.has(item.id)) warnings.push(`${pageId} (${locale}): section ${item.id} appears twice`);
|
|
66
|
+
ids.add(item.id);
|
|
67
|
+
if (!known.has(item.type))
|
|
68
|
+
warnings.push(`${pageId} (${locale}): section type "${item.type}" has no folder`);
|
|
69
|
+
}
|
|
70
|
+
const shape = list.map((s) => `${s?.id}:${s?.type}`).join(",");
|
|
71
|
+
if (reference === null) reference = shape;
|
|
72
|
+
else if (shape !== reference)
|
|
73
|
+
warnings.push(`${pageId}: the sections in ${locale} differ from the other locales`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return warnings;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { SECTION_FOLDERS, checkSections, sectionAttrs, sectionsAttrs };
|
|
@@ -73,6 +73,24 @@ function resolvePath(settings, pathname) {
|
|
|
73
73
|
return { slug: clean, locale: settings.defaultLocale };
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
// src/locales.ts
|
|
77
|
+
function localeLinks(context) {
|
|
78
|
+
const { settings, alternates, homes, locale: current } = context;
|
|
79
|
+
return settings.locales.flatMap((l) => {
|
|
80
|
+
const href = alternates[l] ?? homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
81
|
+
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function nativeName(locale) {
|
|
85
|
+
try {
|
|
86
|
+
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
87
|
+
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
88
|
+
} catch {
|
|
89
|
+
return locale;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
var localeHref = (settings, slug, locale) => buildPath(settings, slug, locale);
|
|
93
|
+
|
|
76
94
|
// src/index.ts
|
|
77
95
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
78
96
|
var SITE_TTL_MS = 6e4;
|
|
@@ -201,4 +219,4 @@ function pathOf(url) {
|
|
|
201
219
|
}
|
|
202
220
|
}
|
|
203
221
|
|
|
204
|
-
export { BreaseError, buildPath, createBrease, env, normalizeSlug, resolvePath };
|
|
222
|
+
export { BreaseError, buildPath, createBrease, env, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var fs = require('fs');
|
|
5
|
+
var path = require('path');
|
|
6
|
+
|
|
7
|
+
// src/sections.ts
|
|
8
|
+
var SECTION_FOLDERS = ["src/brease/sections", "brease/sections"];
|
|
9
|
+
var TYPE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
10
|
+
function checkSections(files2, pages = {}) {
|
|
11
|
+
const warnings2 = [];
|
|
12
|
+
const folder2 = SECTION_FOLDERS.find((f) => Object.keys(files2).some((p) => p.startsWith(`${f}/`)));
|
|
13
|
+
if (!folder2) return warnings2;
|
|
14
|
+
const inFolder = Object.entries(files2).map(([p, text]) => [p.slice(folder2.length + 1), text]);
|
|
15
|
+
const types = [
|
|
16
|
+
...new Set(
|
|
17
|
+
inFolder.map(([p]) => p.split("/")).filter((s) => s.length > 1).map((s) => s[0])
|
|
18
|
+
)
|
|
19
|
+
];
|
|
20
|
+
const map = inFolder.find(([p]) => /^index\.[cm]?[jt]sx?$/.test(p));
|
|
21
|
+
if (!map)
|
|
22
|
+
warnings2.push(`${folder2} has no map file (index.tsx, index.ts or index.js) rendering the section types`);
|
|
23
|
+
const marked = inFolder.some(
|
|
24
|
+
([, text]) => text.includes("data-brease-section") || text.includes("sectionAttrs(")
|
|
25
|
+
);
|
|
26
|
+
if (!marked)
|
|
27
|
+
warnings2.push(
|
|
28
|
+
`No component in ${folder2} renders data-brease-section (or sectionAttrs), so editors can't add sections`
|
|
29
|
+
);
|
|
30
|
+
for (const type of types) {
|
|
31
|
+
if (!TYPE_NAME.test(type)) {
|
|
32
|
+
warnings2.push(`${folder2}/${type}: use lowercase letters, numbers and - in section folder names`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const json = files2[`${folder2}/${type}/section.json`];
|
|
36
|
+
if (json === void 0) {
|
|
37
|
+
warnings2.push(`${folder2}/${type} has no section.json`);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const meta = JSON.parse(json);
|
|
42
|
+
if (typeof meta.name !== "string" || !meta.name.trim())
|
|
43
|
+
warnings2.push(`${folder2}/${type}/section.json has no name`);
|
|
44
|
+
if (meta.fields !== void 0 && (typeof meta.fields !== "object" || meta.fields === null))
|
|
45
|
+
warnings2.push(`${folder2}/${type}/section.json: fields must be a JSON Schema object`);
|
|
46
|
+
} catch {
|
|
47
|
+
warnings2.push(`${folder2}/${type}/section.json is not valid JSON`);
|
|
48
|
+
}
|
|
49
|
+
if (map && !map[1].includes(type)) warnings2.push(`${type} is in ${folder2} but not in its map file`);
|
|
50
|
+
}
|
|
51
|
+
const known = new Set(types);
|
|
52
|
+
for (const [pageId, locales] of Object.entries(pages)) {
|
|
53
|
+
let reference = null;
|
|
54
|
+
for (const [locale, file] of Object.entries(locales)) {
|
|
55
|
+
const list = file.content?.sections;
|
|
56
|
+
if (list === void 0) continue;
|
|
57
|
+
if (!Array.isArray(list)) {
|
|
58
|
+
warnings2.push(`${pageId} (${locale}): sections is not a list`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const ids = /* @__PURE__ */ new Set();
|
|
62
|
+
for (const item of list) {
|
|
63
|
+
if (typeof item?.id !== "string" || typeof item.type !== "string") {
|
|
64
|
+
warnings2.push(`${pageId} (${locale}): a section has no id or type`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (ids.has(item.id)) warnings2.push(`${pageId} (${locale}): section ${item.id} appears twice`);
|
|
68
|
+
ids.add(item.id);
|
|
69
|
+
if (!known.has(item.type))
|
|
70
|
+
warnings2.push(`${pageId} (${locale}): section type "${item.type}" has no folder`);
|
|
71
|
+
}
|
|
72
|
+
const shape = list.map((s) => `${s?.id}:${s?.type}`).join(",");
|
|
73
|
+
if (reference === null) reference = shape;
|
|
74
|
+
else if (shape !== reference)
|
|
75
|
+
warnings2.push(`${pageId}: the sections in ${locale} differ from the other locales`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return warnings2;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/cli.ts
|
|
82
|
+
function walk(dir, out = []) {
|
|
83
|
+
for (const name of fs.readdirSync(dir)) {
|
|
84
|
+
if (name === "node_modules") continue;
|
|
85
|
+
const path$1 = path.join(dir, name);
|
|
86
|
+
if (fs.statSync(path$1).isDirectory()) walk(path$1, out);
|
|
87
|
+
else out.push(path$1);
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
function pagesIn(root) {
|
|
92
|
+
const pages = {};
|
|
93
|
+
const dir = path.join(root, "pages");
|
|
94
|
+
if (!fs.existsSync(dir)) return pages;
|
|
95
|
+
for (const pageId of fs.readdirSync(dir)) {
|
|
96
|
+
for (const file of fs.readdirSync(path.join(dir, pageId))) {
|
|
97
|
+
if (!file.endsWith(".json") || file === "page.json") continue;
|
|
98
|
+
const locales = pages[pageId] ?? {};
|
|
99
|
+
pages[pageId] = locales;
|
|
100
|
+
try {
|
|
101
|
+
locales[file.slice(0, -5)] = JSON.parse(fs.readFileSync(path.join(dir, pageId, file), "utf8"));
|
|
102
|
+
} catch {
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return pages;
|
|
107
|
+
}
|
|
108
|
+
var [command] = process.argv.slice(2);
|
|
109
|
+
if (command !== "check") {
|
|
110
|
+
console.log("Usage: brease-client check");
|
|
111
|
+
process.exit(command ? 1 : 0);
|
|
112
|
+
}
|
|
113
|
+
var cwd = process.cwd();
|
|
114
|
+
var folder = SECTION_FOLDERS.find((f) => fs.existsSync(path.join(cwd, f)));
|
|
115
|
+
if (!folder) {
|
|
116
|
+
console.log("No sections folder (src/brease/sections or brease/sections): nothing to check.");
|
|
117
|
+
process.exit(0);
|
|
118
|
+
}
|
|
119
|
+
var files = Object.fromEntries(
|
|
120
|
+
walk(path.join(cwd, folder)).map((p) => [path.relative(cwd, p).split("\\").join("/"), fs.readFileSync(p, "utf8")])
|
|
121
|
+
);
|
|
122
|
+
var content = ["brease-seed", ".brease"].map((d) => path.join(cwd, d)).find((d) => fs.existsSync(d));
|
|
123
|
+
var warnings = checkSections(files, content ? pagesIn(content) : {});
|
|
124
|
+
for (const w of warnings) console.log(`warning: ${w}`);
|
|
125
|
+
console.log(warnings.length ? `${warnings.length} warning(s)` : "Sections look right.");
|
|
126
|
+
process.exit(warnings.length ? 1 : 0);
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { SECTION_FOLDERS, checkSections } from './chunk-CQZLTZGN.js';
|
|
3
|
+
import './chunk-PKBMQBKP.js';
|
|
4
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'fs';
|
|
5
|
+
import { join, relative } from 'path';
|
|
6
|
+
|
|
7
|
+
function walk(dir, out = []) {
|
|
8
|
+
for (const name of readdirSync(dir)) {
|
|
9
|
+
if (name === "node_modules") continue;
|
|
10
|
+
const path = join(dir, name);
|
|
11
|
+
if (statSync(path).isDirectory()) walk(path, out);
|
|
12
|
+
else out.push(path);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function pagesIn(root) {
|
|
17
|
+
const pages = {};
|
|
18
|
+
const dir = join(root, "pages");
|
|
19
|
+
if (!existsSync(dir)) return pages;
|
|
20
|
+
for (const pageId of readdirSync(dir)) {
|
|
21
|
+
for (const file of readdirSync(join(dir, pageId))) {
|
|
22
|
+
if (!file.endsWith(".json") || file === "page.json") continue;
|
|
23
|
+
const locales = pages[pageId] ?? {};
|
|
24
|
+
pages[pageId] = locales;
|
|
25
|
+
try {
|
|
26
|
+
locales[file.slice(0, -5)] = JSON.parse(readFileSync(join(dir, pageId, file), "utf8"));
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return pages;
|
|
32
|
+
}
|
|
33
|
+
var [command] = process.argv.slice(2);
|
|
34
|
+
if (command !== "check") {
|
|
35
|
+
console.log("Usage: brease-client check");
|
|
36
|
+
process.exit(command ? 1 : 0);
|
|
37
|
+
}
|
|
38
|
+
var cwd = process.cwd();
|
|
39
|
+
var folder = SECTION_FOLDERS.find((f) => existsSync(join(cwd, f)));
|
|
40
|
+
if (!folder) {
|
|
41
|
+
console.log("No sections folder (src/brease/sections or brease/sections): nothing to check.");
|
|
42
|
+
process.exit(0);
|
|
43
|
+
}
|
|
44
|
+
var files = Object.fromEntries(
|
|
45
|
+
walk(join(cwd, folder)).map((p) => [relative(cwd, p).split("\\").join("/"), readFileSync(p, "utf8")])
|
|
46
|
+
);
|
|
47
|
+
var content = ["brease-seed", ".brease"].map((d) => join(cwd, d)).find((d) => existsSync(d));
|
|
48
|
+
var warnings = checkSections(files, content ? pagesIn(content) : {});
|
|
49
|
+
for (const w of warnings) console.log(`warning: ${w}`);
|
|
50
|
+
console.log(warnings.length ? `${warnings.length} warning(s)` : "Sections look right.");
|
|
51
|
+
process.exit(warnings.length ? 1 : 0);
|
package/dist/index.cjs
CHANGED
|
@@ -77,6 +77,102 @@ function resolvePath(settings, pathname) {
|
|
|
77
77
|
return { slug: clean, locale: settings.defaultLocale };
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
// src/locales.ts
|
|
81
|
+
function localeLinks(context) {
|
|
82
|
+
const { settings, alternates, homes, locale: current } = context;
|
|
83
|
+
return settings.locales.flatMap((l) => {
|
|
84
|
+
const href = alternates[l] ?? homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
85
|
+
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function nativeName(locale) {
|
|
89
|
+
try {
|
|
90
|
+
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
91
|
+
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
92
|
+
} catch {
|
|
93
|
+
return locale;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var localeHref = (settings, slug, locale) => buildPath(settings, slug, locale);
|
|
97
|
+
|
|
98
|
+
// src/sections.ts
|
|
99
|
+
var sectionsAttrs = { "data-brease-sections": "sections" };
|
|
100
|
+
function sectionAttrs(section) {
|
|
101
|
+
return { "data-brease-section": section.id, "data-brease-block": section.type };
|
|
102
|
+
}
|
|
103
|
+
var SECTION_FOLDERS = ["src/brease/sections", "brease/sections"];
|
|
104
|
+
var TYPE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
105
|
+
function checkSections(files, pages = {}) {
|
|
106
|
+
const warnings = [];
|
|
107
|
+
const folder = SECTION_FOLDERS.find((f) => Object.keys(files).some((p) => p.startsWith(`${f}/`)));
|
|
108
|
+
if (!folder) return warnings;
|
|
109
|
+
const inFolder = Object.entries(files).map(([p, text]) => [p.slice(folder.length + 1), text]);
|
|
110
|
+
const types = [
|
|
111
|
+
...new Set(
|
|
112
|
+
inFolder.map(([p]) => p.split("/")).filter((s) => s.length > 1).map((s) => s[0])
|
|
113
|
+
)
|
|
114
|
+
];
|
|
115
|
+
const map = inFolder.find(([p]) => /^index\.[cm]?[jt]sx?$/.test(p));
|
|
116
|
+
if (!map)
|
|
117
|
+
warnings.push(`${folder} has no map file (index.tsx, index.ts or index.js) rendering the section types`);
|
|
118
|
+
const marked = inFolder.some(
|
|
119
|
+
([, text]) => text.includes("data-brease-section") || text.includes("sectionAttrs(")
|
|
120
|
+
);
|
|
121
|
+
if (!marked)
|
|
122
|
+
warnings.push(
|
|
123
|
+
`No component in ${folder} renders data-brease-section (or sectionAttrs), so editors can't add sections`
|
|
124
|
+
);
|
|
125
|
+
for (const type of types) {
|
|
126
|
+
if (!TYPE_NAME.test(type)) {
|
|
127
|
+
warnings.push(`${folder}/${type}: use lowercase letters, numbers and - in section folder names`);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const json = files[`${folder}/${type}/section.json`];
|
|
131
|
+
if (json === void 0) {
|
|
132
|
+
warnings.push(`${folder}/${type} has no section.json`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const meta = JSON.parse(json);
|
|
137
|
+
if (typeof meta.name !== "string" || !meta.name.trim())
|
|
138
|
+
warnings.push(`${folder}/${type}/section.json has no name`);
|
|
139
|
+
if (meta.fields !== void 0 && (typeof meta.fields !== "object" || meta.fields === null))
|
|
140
|
+
warnings.push(`${folder}/${type}/section.json: fields must be a JSON Schema object`);
|
|
141
|
+
} catch {
|
|
142
|
+
warnings.push(`${folder}/${type}/section.json is not valid JSON`);
|
|
143
|
+
}
|
|
144
|
+
if (map && !map[1].includes(type)) warnings.push(`${type} is in ${folder} but not in its map file`);
|
|
145
|
+
}
|
|
146
|
+
const known = new Set(types);
|
|
147
|
+
for (const [pageId, locales] of Object.entries(pages)) {
|
|
148
|
+
let reference = null;
|
|
149
|
+
for (const [locale, file] of Object.entries(locales)) {
|
|
150
|
+
const list = file.content?.sections;
|
|
151
|
+
if (list === void 0) continue;
|
|
152
|
+
if (!Array.isArray(list)) {
|
|
153
|
+
warnings.push(`${pageId} (${locale}): sections is not a list`);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const ids = /* @__PURE__ */ new Set();
|
|
157
|
+
for (const item of list) {
|
|
158
|
+
if (typeof item?.id !== "string" || typeof item.type !== "string") {
|
|
159
|
+
warnings.push(`${pageId} (${locale}): a section has no id or type`);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (ids.has(item.id)) warnings.push(`${pageId} (${locale}): section ${item.id} appears twice`);
|
|
163
|
+
ids.add(item.id);
|
|
164
|
+
if (!known.has(item.type))
|
|
165
|
+
warnings.push(`${pageId} (${locale}): section type "${item.type}" has no folder`);
|
|
166
|
+
}
|
|
167
|
+
const shape = list.map((s) => `${s?.id}:${s?.type}`).join(",");
|
|
168
|
+
if (reference === null) reference = shape;
|
|
169
|
+
else if (shape !== reference)
|
|
170
|
+
warnings.push(`${pageId}: the sections in ${locale} differ from the other locales`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return warnings;
|
|
174
|
+
}
|
|
175
|
+
|
|
80
176
|
// src/index.ts
|
|
81
177
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
82
178
|
var SITE_TTL_MS = 6e4;
|
|
@@ -207,6 +303,12 @@ function pathOf(url) {
|
|
|
207
303
|
|
|
208
304
|
exports.BreaseError = BreaseError;
|
|
209
305
|
exports.buildPath = buildPath;
|
|
306
|
+
exports.checkSections = checkSections;
|
|
210
307
|
exports.createBrease = createBrease;
|
|
308
|
+
exports.localeHref = localeHref;
|
|
309
|
+
exports.localeLinks = localeLinks;
|
|
310
|
+
exports.nativeName = nativeName;
|
|
211
311
|
exports.normalizeSlug = normalizeSlug;
|
|
212
312
|
exports.resolvePath = resolvePath;
|
|
313
|
+
exports.sectionAttrs = sectionAttrs;
|
|
314
|
+
exports.sectionsAttrs = sectionsAttrs;
|
package/dist/index.d.cts
CHANGED
|
@@ -11,6 +11,35 @@ declare class BreaseError extends Error {
|
|
|
11
11
|
static fromResponse(res: Response): Promise<BreaseError>;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
type LocaleLink = {
|
|
15
|
+
locale: string;
|
|
16
|
+
label: string;
|
|
17
|
+
href: string;
|
|
18
|
+
current: boolean;
|
|
19
|
+
};
|
|
20
|
+
declare function localeLinks(context: LocaleContext): LocaleLink[];
|
|
21
|
+
declare function nativeName(locale: string): string;
|
|
22
|
+
declare const localeHref: (settings: UrlSettings, slug: string, locale: string) => string;
|
|
23
|
+
|
|
24
|
+
type BreaseSection<T = Record<string, unknown>> = {
|
|
25
|
+
id: string;
|
|
26
|
+
type: string;
|
|
27
|
+
} & T;
|
|
28
|
+
declare const sectionsAttrs: {
|
|
29
|
+
readonly 'data-brease-sections': "sections";
|
|
30
|
+
};
|
|
31
|
+
declare function sectionAttrs(section: {
|
|
32
|
+
id: string;
|
|
33
|
+
type: string;
|
|
34
|
+
}): {
|
|
35
|
+
'data-brease-section': string;
|
|
36
|
+
'data-brease-block': string;
|
|
37
|
+
};
|
|
38
|
+
type PageLocales = Record<string, Record<string, {
|
|
39
|
+
content?: unknown;
|
|
40
|
+
}>>;
|
|
41
|
+
declare function checkSections(files: Record<string, string>, pages?: PageLocales): string[];
|
|
42
|
+
|
|
14
43
|
declare function normalizeSlug(input: string): string;
|
|
15
44
|
declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
|
|
16
45
|
declare function resolvePath(settings: UrlSettings, pathname: string): {
|
|
@@ -61,4 +90,4 @@ interface Brease {
|
|
|
61
90
|
}
|
|
62
91
|
declare function createBrease(opts?: BreaseOptions): Brease;
|
|
63
92
|
|
|
64
|
-
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, normalizeSlug, resolvePath };
|
|
93
|
+
export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,35 @@ declare class BreaseError extends Error {
|
|
|
11
11
|
static fromResponse(res: Response): Promise<BreaseError>;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
type LocaleLink = {
|
|
15
|
+
locale: string;
|
|
16
|
+
label: string;
|
|
17
|
+
href: string;
|
|
18
|
+
current: boolean;
|
|
19
|
+
};
|
|
20
|
+
declare function localeLinks(context: LocaleContext): LocaleLink[];
|
|
21
|
+
declare function nativeName(locale: string): string;
|
|
22
|
+
declare const localeHref: (settings: UrlSettings, slug: string, locale: string) => string;
|
|
23
|
+
|
|
24
|
+
type BreaseSection<T = Record<string, unknown>> = {
|
|
25
|
+
id: string;
|
|
26
|
+
type: string;
|
|
27
|
+
} & T;
|
|
28
|
+
declare const sectionsAttrs: {
|
|
29
|
+
readonly 'data-brease-sections': "sections";
|
|
30
|
+
};
|
|
31
|
+
declare function sectionAttrs(section: {
|
|
32
|
+
id: string;
|
|
33
|
+
type: string;
|
|
34
|
+
}): {
|
|
35
|
+
'data-brease-section': string;
|
|
36
|
+
'data-brease-block': string;
|
|
37
|
+
};
|
|
38
|
+
type PageLocales = Record<string, Record<string, {
|
|
39
|
+
content?: unknown;
|
|
40
|
+
}>>;
|
|
41
|
+
declare function checkSections(files: Record<string, string>, pages?: PageLocales): string[];
|
|
42
|
+
|
|
14
43
|
declare function normalizeSlug(input: string): string;
|
|
15
44
|
declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
|
|
16
45
|
declare function resolvePath(settings: UrlSettings, pathname: string): {
|
|
@@ -61,4 +90,4 @@ interface Brease {
|
|
|
61
90
|
}
|
|
62
91
|
declare function createBrease(opts?: BreaseOptions): Brease;
|
|
63
92
|
|
|
64
|
-
export { type Brease, BreaseError, type BreaseOptions, type LocaleContext, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, createBrease, normalizeSlug, resolvePath };
|
|
93
|
+
export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { BreaseError, buildPath, createBrease, normalizeSlug, resolvePath } from './chunk-
|
|
1
|
+
export { BreaseError, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-OA7GXHT3.js';
|
|
2
|
+
export { checkSections, sectionAttrs, sectionsAttrs } from './chunk-CQZLTZGN.js';
|
|
2
3
|
import './chunk-PKBMQBKP.js';
|
package/dist/next.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brease-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Brease delivery API client for Next.js, Astro and any fetch-capable runtime",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -46,11 +46,6 @@
|
|
|
46
46
|
"types": "./dist/image.d.ts",
|
|
47
47
|
"import": "./dist/image.js",
|
|
48
48
|
"require": "./dist/image.cjs"
|
|
49
|
-
},
|
|
50
|
-
"./react": {
|
|
51
|
-
"types": "./dist/react.d.ts",
|
|
52
|
-
"import": "./dist/react.js",
|
|
53
|
-
"require": "./dist/react.cjs"
|
|
54
49
|
}
|
|
55
50
|
},
|
|
56
51
|
"files": [
|
|
@@ -61,24 +56,16 @@
|
|
|
61
56
|
"node": ">=18"
|
|
62
57
|
},
|
|
63
58
|
"peerDependencies": {
|
|
64
|
-
"next": ">=14"
|
|
65
|
-
"react": ">=18"
|
|
59
|
+
"next": ">=14"
|
|
66
60
|
},
|
|
67
61
|
"peerDependenciesMeta": {
|
|
68
62
|
"next": {
|
|
69
63
|
"optional": true
|
|
70
|
-
},
|
|
71
|
-
"react": {
|
|
72
|
-
"optional": true
|
|
73
64
|
}
|
|
74
65
|
},
|
|
75
66
|
"devDependencies": {
|
|
76
67
|
"@biomejs/biome": "^2.5.14",
|
|
77
68
|
"@types/node": "^22.20.4",
|
|
78
|
-
"@types/react": "^19.3.0",
|
|
79
|
-
"@types/react-dom": "^19.3.0",
|
|
80
|
-
"react": "^19.3.0",
|
|
81
|
-
"react-dom": "^19.3.0",
|
|
82
69
|
"tsup": "^8.5.1",
|
|
83
70
|
"typescript": "^5.9.3",
|
|
84
71
|
"vitest": "^5.0.1"
|
|
@@ -87,6 +74,9 @@
|
|
|
87
74
|
"access": "public",
|
|
88
75
|
"provenance": true
|
|
89
76
|
},
|
|
77
|
+
"bin": {
|
|
78
|
+
"brease-client": "./dist/cli.js"
|
|
79
|
+
},
|
|
90
80
|
"scripts": {
|
|
91
81
|
"build": "tsup",
|
|
92
82
|
"dev": "tsup --watch",
|
package/dist/react.cjs
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
"use strict";
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/react.tsx
|
|
22
|
-
var react_exports = {};
|
|
23
|
-
__export(react_exports, {
|
|
24
|
-
BreaseLocaleProvider: () => BreaseLocaleProvider,
|
|
25
|
-
LocaleSwitcher: () => LocaleSwitcher,
|
|
26
|
-
localeLinks: () => localeLinks,
|
|
27
|
-
nativeName: () => nativeName,
|
|
28
|
-
useLocale: () => useLocale,
|
|
29
|
-
useLocaleLinks: () => useLocaleLinks
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(react_exports);
|
|
32
|
-
var import_react = require("react");
|
|
33
|
-
|
|
34
|
-
// src/urls.ts
|
|
35
|
-
function normalizeSlug(input) {
|
|
36
|
-
return input.trim().replace(/^\/+|\/+$/g, "").replace(/\/{2,}/g, "/");
|
|
37
|
-
}
|
|
38
|
-
function buildPath(settings, slug, locale) {
|
|
39
|
-
const clean = normalizeSlug(slug);
|
|
40
|
-
if (settings.localeStrategy === "CUSTOM" && settings.urlTemplate) {
|
|
41
|
-
const path = settings.urlTemplate.replace("{locale}", locale).replace("{slug}", clean);
|
|
42
|
-
return `/${normalizeSlug(path)}`;
|
|
43
|
-
}
|
|
44
|
-
const prefixed = settings.localeStrategy === "PREFIX_ALL" || locale !== settings.defaultLocale;
|
|
45
|
-
const parts = [prefixed ? locale : "", clean].filter(Boolean);
|
|
46
|
-
return `/${parts.join("/")}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/react.tsx
|
|
50
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
|
-
var Context = (0, import_react.createContext)(null);
|
|
52
|
-
function BreaseLocaleProvider({ value, children }) {
|
|
53
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children });
|
|
54
|
-
}
|
|
55
|
-
function useLocale() {
|
|
56
|
-
const value = (0, import_react.useContext)(Context);
|
|
57
|
-
if (!value) throw new Error("useLocale() needs a <BreaseLocaleProvider> above it");
|
|
58
|
-
return {
|
|
59
|
-
...value,
|
|
60
|
-
locales: value.settings.locales,
|
|
61
|
-
href: (slug, locale = value.locale) => buildPath(value.settings, slug, locale)
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function useLocaleLinks() {
|
|
65
|
-
const { locale, settings, alternates, homes } = useLocale();
|
|
66
|
-
return localeLinks(settings, { alternates, homes }, locale);
|
|
67
|
-
}
|
|
68
|
-
function localeLinks(settings, paths, current) {
|
|
69
|
-
return settings.locales.flatMap((l) => {
|
|
70
|
-
const href = paths.alternates[l] ?? paths.homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
71
|
-
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
function nativeName(locale) {
|
|
75
|
-
try {
|
|
76
|
-
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
77
|
-
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
78
|
-
} catch {
|
|
79
|
-
return locale;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function LocaleSwitcher({
|
|
83
|
-
className,
|
|
84
|
-
linkClassName,
|
|
85
|
-
label = "Language"
|
|
86
|
-
}) {
|
|
87
|
-
const links = useLocaleLinks();
|
|
88
|
-
if (links.length < 2) return null;
|
|
89
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("nav", { "aria-label": label, className, children: links.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
90
|
-
"a",
|
|
91
|
-
{
|
|
92
|
-
href: l.href,
|
|
93
|
-
hrefLang: l.locale,
|
|
94
|
-
lang: l.locale,
|
|
95
|
-
"aria-current": l.current ? "page" : void 0,
|
|
96
|
-
className: linkClassName,
|
|
97
|
-
children: l.label
|
|
98
|
-
},
|
|
99
|
-
l.locale
|
|
100
|
-
)) });
|
|
101
|
-
}
|
|
102
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
-
0 && (module.exports = {
|
|
104
|
-
BreaseLocaleProvider,
|
|
105
|
-
LocaleSwitcher,
|
|
106
|
-
localeLinks,
|
|
107
|
-
nativeName,
|
|
108
|
-
useLocale,
|
|
109
|
-
useLocaleLinks
|
|
110
|
-
});
|
package/dist/react.d.cts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
3
|
-
|
|
4
|
-
type LocaleStrategy = 'PREFIX_EXCEPT_DEFAULT' | 'PREFIX_ALL' | 'CUSTOM';
|
|
5
|
-
type UrlSettings = {
|
|
6
|
-
locales: string[];
|
|
7
|
-
defaultLocale: string;
|
|
8
|
-
localeStrategy: LocaleStrategy;
|
|
9
|
-
urlTemplate?: string | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type LocaleContext = {
|
|
13
|
-
locale: string;
|
|
14
|
-
settings: UrlSettings;
|
|
15
|
-
alternates: Record<string, string>;
|
|
16
|
-
homes: Record<string, string>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
declare function BreaseLocaleProvider({ value, children }: {
|
|
20
|
-
value: LocaleContext;
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
}): react.JSX.Element;
|
|
23
|
-
type LocaleInfo = LocaleContext & {
|
|
24
|
-
locales: string[];
|
|
25
|
-
href: (slug: string, locale?: string) => string;
|
|
26
|
-
};
|
|
27
|
-
declare function useLocale(): LocaleInfo;
|
|
28
|
-
type LocaleLink = {
|
|
29
|
-
locale: string;
|
|
30
|
-
label: string;
|
|
31
|
-
href: string;
|
|
32
|
-
current: boolean;
|
|
33
|
-
};
|
|
34
|
-
declare function useLocaleLinks(): LocaleLink[];
|
|
35
|
-
declare function localeLinks(settings: UrlSettings, paths: Pick<LocaleContext, 'alternates' | 'homes'>, current: string): LocaleLink[];
|
|
36
|
-
declare function nativeName(locale: string): string;
|
|
37
|
-
declare function LocaleSwitcher({ className, linkClassName, label }: {
|
|
38
|
-
className?: string;
|
|
39
|
-
linkClassName?: string;
|
|
40
|
-
label?: string;
|
|
41
|
-
}): react.JSX.Element | null;
|
|
42
|
-
|
|
43
|
-
export { BreaseLocaleProvider, type LocaleInfo, type LocaleLink, LocaleSwitcher, localeLinks, nativeName, useLocale, useLocaleLinks };
|
package/dist/react.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
3
|
-
|
|
4
|
-
type LocaleStrategy = 'PREFIX_EXCEPT_DEFAULT' | 'PREFIX_ALL' | 'CUSTOM';
|
|
5
|
-
type UrlSettings = {
|
|
6
|
-
locales: string[];
|
|
7
|
-
defaultLocale: string;
|
|
8
|
-
localeStrategy: LocaleStrategy;
|
|
9
|
-
urlTemplate?: string | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type LocaleContext = {
|
|
13
|
-
locale: string;
|
|
14
|
-
settings: UrlSettings;
|
|
15
|
-
alternates: Record<string, string>;
|
|
16
|
-
homes: Record<string, string>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
declare function BreaseLocaleProvider({ value, children }: {
|
|
20
|
-
value: LocaleContext;
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
}): react.JSX.Element;
|
|
23
|
-
type LocaleInfo = LocaleContext & {
|
|
24
|
-
locales: string[];
|
|
25
|
-
href: (slug: string, locale?: string) => string;
|
|
26
|
-
};
|
|
27
|
-
declare function useLocale(): LocaleInfo;
|
|
28
|
-
type LocaleLink = {
|
|
29
|
-
locale: string;
|
|
30
|
-
label: string;
|
|
31
|
-
href: string;
|
|
32
|
-
current: boolean;
|
|
33
|
-
};
|
|
34
|
-
declare function useLocaleLinks(): LocaleLink[];
|
|
35
|
-
declare function localeLinks(settings: UrlSettings, paths: Pick<LocaleContext, 'alternates' | 'homes'>, current: string): LocaleLink[];
|
|
36
|
-
declare function nativeName(locale: string): string;
|
|
37
|
-
declare function LocaleSwitcher({ className, linkClassName, label }: {
|
|
38
|
-
className?: string;
|
|
39
|
-
linkClassName?: string;
|
|
40
|
-
label?: string;
|
|
41
|
-
}): react.JSX.Element | null;
|
|
42
|
-
|
|
43
|
-
export { BreaseLocaleProvider, type LocaleInfo, type LocaleLink, LocaleSwitcher, localeLinks, nativeName, useLocale, useLocaleLinks };
|
package/dist/react.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
// src/react.tsx
|
|
4
|
-
import { createContext, useContext } from "react";
|
|
5
|
-
|
|
6
|
-
// src/urls.ts
|
|
7
|
-
function normalizeSlug(input) {
|
|
8
|
-
return input.trim().replace(/^\/+|\/+$/g, "").replace(/\/{2,}/g, "/");
|
|
9
|
-
}
|
|
10
|
-
function buildPath(settings, slug, locale) {
|
|
11
|
-
const clean = normalizeSlug(slug);
|
|
12
|
-
if (settings.localeStrategy === "CUSTOM" && settings.urlTemplate) {
|
|
13
|
-
const path = settings.urlTemplate.replace("{locale}", locale).replace("{slug}", clean);
|
|
14
|
-
return `/${normalizeSlug(path)}`;
|
|
15
|
-
}
|
|
16
|
-
const prefixed = settings.localeStrategy === "PREFIX_ALL" || locale !== settings.defaultLocale;
|
|
17
|
-
const parts = [prefixed ? locale : "", clean].filter(Boolean);
|
|
18
|
-
return `/${parts.join("/")}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// src/react.tsx
|
|
22
|
-
import { jsx } from "react/jsx-runtime";
|
|
23
|
-
var Context = createContext(null);
|
|
24
|
-
function BreaseLocaleProvider({ value, children }) {
|
|
25
|
-
return /* @__PURE__ */ jsx(Context.Provider, { value, children });
|
|
26
|
-
}
|
|
27
|
-
function useLocale() {
|
|
28
|
-
const value = useContext(Context);
|
|
29
|
-
if (!value) throw new Error("useLocale() needs a <BreaseLocaleProvider> above it");
|
|
30
|
-
return {
|
|
31
|
-
...value,
|
|
32
|
-
locales: value.settings.locales,
|
|
33
|
-
href: (slug, locale = value.locale) => buildPath(value.settings, slug, locale)
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function useLocaleLinks() {
|
|
37
|
-
const { locale, settings, alternates, homes } = useLocale();
|
|
38
|
-
return localeLinks(settings, { alternates, homes }, locale);
|
|
39
|
-
}
|
|
40
|
-
function localeLinks(settings, paths, current) {
|
|
41
|
-
return settings.locales.flatMap((l) => {
|
|
42
|
-
const href = paths.alternates[l] ?? paths.homes[l] ?? (l === current ? buildPath(settings, "", l) : null);
|
|
43
|
-
return href ? [{ locale: l, label: nativeName(l), href, current: l === current }] : [];
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
function nativeName(locale) {
|
|
47
|
-
try {
|
|
48
|
-
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
49
|
-
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
50
|
-
} catch {
|
|
51
|
-
return locale;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function LocaleSwitcher({
|
|
55
|
-
className,
|
|
56
|
-
linkClassName,
|
|
57
|
-
label = "Language"
|
|
58
|
-
}) {
|
|
59
|
-
const links = useLocaleLinks();
|
|
60
|
-
if (links.length < 2) return null;
|
|
61
|
-
return /* @__PURE__ */ jsx("nav", { "aria-label": label, className, children: links.map((l) => /* @__PURE__ */ jsx(
|
|
62
|
-
"a",
|
|
63
|
-
{
|
|
64
|
-
href: l.href,
|
|
65
|
-
hrefLang: l.locale,
|
|
66
|
-
lang: l.locale,
|
|
67
|
-
"aria-current": l.current ? "page" : void 0,
|
|
68
|
-
className: linkClassName,
|
|
69
|
-
children: l.label
|
|
70
|
-
},
|
|
71
|
-
l.locale
|
|
72
|
-
)) });
|
|
73
|
-
}
|
|
74
|
-
export {
|
|
75
|
-
BreaseLocaleProvider,
|
|
76
|
-
LocaleSwitcher,
|
|
77
|
-
localeLinks,
|
|
78
|
-
nativeName,
|
|
79
|
-
useLocale,
|
|
80
|
-
useLocaleLinks
|
|
81
|
-
};
|