create-nextblock 0.15.5 → 0.15.8

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.
@@ -1,311 +1,319 @@
1
- import type { Metadata } from 'next';
2
-
3
- export const DEFAULT_SITE_TITLE = 'NextBlock™ CMS';
4
-
5
- export const DEFAULT_SITE_DESCRIPTION =
6
- 'NextBlock is an open-source CMS on Next.js + Supabase — a visual block editor, blazing-fast multilingual pages, and built-in e-commerce.';
7
-
8
- export const DEFAULT_SITE_KEYWORDS =
9
- 'NextBlock, CMS, Next.js, Supabase, headless CMS, block editor, visual page builder, multilingual, e-commerce, open source';
10
-
11
- /** Bundled fallback Open Graph image (resolved to absolute via metadataBase). */
12
- export const DEFAULT_OG_IMAGE = '/images/metadata_image.webp';
13
-
14
- const DEFAULT_META_DESCRIPTION_LENGTH = 160;
15
-
16
- function normalizeWhitespace(value: string) {
17
- return value.replace(/\s+/g, ' ').trim();
18
- }
19
-
20
- function decodeHtmlEntities(value: string) {
21
- const entities: Record<string, string> = {
22
- amp: '&',
23
- apos: "'",
24
- gt: '>',
25
- lt: '<',
26
- nbsp: ' ',
27
- quot: '"',
28
- };
29
-
30
- return value.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, entity) => {
31
- const key = String(entity);
32
- if (key[0] === '#') {
33
- const isHex = key[1]?.toLowerCase() === 'x';
34
- const codePoint = Number.parseInt(key.slice(isHex ? 2 : 1), isHex ? 16 : 10);
35
- return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;
36
- }
37
-
38
- return entities[key] ?? match;
39
- });
40
- }
41
-
42
- export function stripHtmlToText(value: string) {
43
- return normalizeWhitespace(
44
- decodeHtmlEntities(
45
- value
46
- .replace(/<script[\s\S]*?<\/script>/gi, ' ')
47
- .replace(/<style[\s\S]*?<\/style>/gi, ' ')
48
- .replace(/<[^>]+>/g, ' ')
49
- )
50
- );
51
- }
52
-
53
- function extractParagraphTextFromHtml(value: string) {
54
- const paragraphs = Array.from(value.matchAll(/<p\b[^>]*>([\s\S]*?)<\/p>/gi))
55
- .map((match) => stripHtmlToText(match[1] ?? ''))
56
- .filter(Boolean);
57
-
58
- if (paragraphs.length > 0) {
59
- return paragraphs[0];
60
- }
61
-
62
- return stripHtmlToText(value.replace(/<h[1-6]\b[\s\S]*?<\/h[1-6]>/gi, ' '));
63
- }
64
-
65
- function truncateMetaDescription(value: string, maxLength = DEFAULT_META_DESCRIPTION_LENGTH) {
66
- const normalized = normalizeWhitespace(value);
67
- if (normalized.length <= maxLength) {
68
- return normalized;
69
- }
70
-
71
- const truncated = normalized.slice(0, maxLength + 1);
72
- const lastSpace = truncated.lastIndexOf(' ');
73
- const candidate = lastSpace > 80 ? truncated.slice(0, lastSpace) : normalized.slice(0, maxLength);
74
-
75
- return candidate.replace(/[.,;:!?-]+$/, '').trim();
76
- }
77
-
78
- function normalizeMetaCandidate(value: string | null | undefined) {
79
- if (!value) {
80
- return null;
81
- }
82
-
83
- const normalized = stripHtmlToText(value);
84
- return normalized || null;
85
- }
86
-
87
- function collectIntroTextCandidates(value: unknown, candidates: string[]) {
88
- if (!value) {
89
- return;
90
- }
91
-
92
- if (Array.isArray(value)) {
93
- value.forEach((item) => collectIntroTextCandidates(item, candidates));
94
- return;
95
- }
96
-
97
- if (typeof value !== 'object') {
98
- return;
99
- }
100
-
101
- const block = value as {
102
- block_type?: string;
103
- content?: Record<string, unknown>;
104
- };
105
-
106
- if (block.block_type === 'section' || block.block_type === 'hero') {
107
- collectIntroTextCandidates(block.content?.column_blocks, candidates);
108
- collectIntroTextCandidates(block.content?.slides, candidates);
109
- return;
110
- }
111
-
112
- if (block.block_type === 'text') {
113
- const htmlContent = block.content?.html_content;
114
- const textContent = block.content?.text_content;
115
- const candidate =
116
- typeof htmlContent === 'string'
117
- ? extractParagraphTextFromHtml(htmlContent)
118
- : typeof textContent === 'string'
119
- ? normalizeWhitespace(textContent)
120
- : '';
121
-
122
- if (candidate) {
123
- candidates.push(candidate);
124
- }
125
- }
126
- }
127
-
128
- export function extractIntroExcerptFromBlocks(blocks: unknown) {
129
- const candidates: string[] = [];
130
- collectIntroTextCandidates(blocks, candidates);
131
-
132
- return (
133
- candidates.find((candidate) => candidate.length >= 80) ??
134
- candidates[0] ??
135
- null
136
- );
137
- }
138
-
139
- export function resolveMetaTitle(
140
- manualTitle: string | null | undefined,
141
- fallbackTitle: string | null | undefined
142
- ) {
143
- return (
144
- normalizeMetaCandidate(manualTitle) ??
145
- normalizeMetaCandidate(fallbackTitle) ??
146
- DEFAULT_SITE_TITLE
147
- );
148
- }
149
-
150
- export function resolveMetaDescription(...candidates: Array<string | null | undefined>) {
151
- for (const candidate of candidates) {
152
- const description = normalizeMetaCandidate(candidate);
153
- if (description) {
154
- return truncateMetaDescription(description);
155
- }
156
- }
157
-
158
- return DEFAULT_SITE_DESCRIPTION;
159
- }
160
-
161
- export function resolvePageMetaDescription(
162
- manualDescription: string | null | undefined,
163
- blocks: unknown
164
- ) {
165
- return resolveMetaDescription(manualDescription, extractIntroExcerptFromBlocks(blocks));
166
- }
167
-
168
- export function resolvePostMetaDescription(
169
- manualDescription: string | null | undefined,
170
- subtitle: string | null | undefined
171
- ) {
172
- return resolveMetaDescription(manualDescription, subtitle);
173
- }
174
-
175
- export function resolveProductMetaDescription(
176
- manualDescription: string | null | undefined,
177
- shortDescription: string | null | undefined
178
- ) {
179
- return resolveMetaDescription(manualDescription, shortDescription);
180
- }
181
-
182
- export function stringifyJsonLd(value: unknown) {
183
- return JSON.stringify(value).replace(/</g, '\\u003c');
184
- }
185
-
186
- /**
187
- * Appends the site title to a page title for social cards, e.g.
188
- * `composeTitleWithSite('Home', 'NextBlock™ CMS') === 'Home | NextBlock™ CMS'`.
189
- * Unlike Next.js' `title.template` (which only affects the `<title>` tag), this
190
- * lets us produce a complete `og:title` / `twitter:title`.
191
- */
192
- export function composeTitleWithSite(
193
- pageTitle: string | null | undefined,
194
- siteTitle: string | null | undefined
195
- ): string {
196
- const cleanTitle = (pageTitle ?? '').trim();
197
- const cleanSite = (siteTitle ?? '').trim();
198
-
199
- if (!cleanSite) return cleanTitle;
200
- if (!cleanTitle) return cleanSite;
201
-
202
- const suffix = ` | ${cleanSite}`;
203
- return cleanTitle === cleanSite || cleanTitle.endsWith(suffix)
204
- ? cleanTitle
205
- : `${cleanTitle}${suffix}`;
206
- }
207
-
208
- /**
209
- * Resolves the canonical URL for a public page/post/product.
210
- *
211
- * By default this is the self-referencing `<siteUrl><path>`. When the content row
212
- * sets a manual `custom_canonical` override, that wins:
213
- * - absolute values (`https://…`) are used verbatim,
214
- * - root-relative (`/foo`) and bare (`foo`) values are resolved against `siteUrl`.
215
- * A null/blank override falls back to the self-referencing default, so existing
216
- * content is unaffected. `siteUrl` may be empty (pre-config / no NEXT_PUBLIC_URL),
217
- * in which case a relative path is returned and resolved by `metadataBase`.
218
- */
219
- export function buildCanonicalUrl(
220
- customCanonical: string | null | undefined,
221
- siteUrl: string,
222
- path: string
223
- ): string {
224
- const base = (siteUrl || '').replace(/\/+$/, '');
225
- const normalizedPath = path.startsWith('/') ? path : `/${path}`;
226
- const fallback = `${base}${normalizedPath}`;
227
-
228
- const custom = customCanonical?.trim();
229
- if (!custom) {
230
- return fallback;
231
- }
232
-
233
- if (/^https?:\/\//i.test(custom)) {
234
- return custom;
235
- }
236
-
237
- return custom.startsWith('/') ? `${base}${custom}` : `${base}/${custom}`;
238
- }
239
-
240
- /** Maps a language code (e.g. `fr`, `en-US`) to an Open Graph locale (`fr_FR`). */
241
- export function toOpenGraphLocale(languageCode?: string | null): string {
242
- const code = (languageCode ?? '').toLowerCase().split('-')[0];
243
- const map: Record<string, string> = {
244
- en: 'en_US',
245
- fr: 'fr_FR',
246
- es: 'es_ES',
247
- de: 'de_DE',
248
- pt: 'pt_PT',
249
- it: 'it_IT',
250
- nl: 'nl_NL',
251
- };
252
- return map[code] ?? 'en_US';
253
- }
254
-
255
- export interface SocialMetadataInput {
256
- /** Bare page title (without the site-title suffix). */
257
- title: string;
258
- description: string;
259
- /** Canonical URL of the page (absolute, or path resolved via metadataBase). */
260
- url: string;
261
- siteTitle: string;
262
- /** Resolved feature-image URL; falls back to the default OG image when empty. */
263
- imageUrl?: string | null;
264
- type?: 'website' | 'article';
265
- publishedTime?: string | null;
266
- locale?: string | null;
267
- }
268
-
269
- /**
270
- * Builds the `openGraph` + `twitter` metadata for a public page so that every
271
- * page emits a complete, suffixed social title and always has an OG image
272
- * (the feature image when present, otherwise the bundled default).
273
- */
274
- export function buildSocialMetadata(
275
- input: SocialMetadataInput
276
- ): Pick<Metadata, 'openGraph' | 'twitter'> {
277
- const usingDefaultImage = !input.imageUrl;
278
- const imageUrl = input.imageUrl || DEFAULT_OG_IMAGE;
279
- const socialTitle = composeTitleWithSite(input.title, input.siteTitle);
280
- const image = usingDefaultImage
281
- ? { url: imageUrl, width: 1200, height: 630, alt: socialTitle }
282
- : { url: imageUrl, alt: socialTitle };
283
-
284
- const openGraphBase = {
285
- title: socialTitle,
286
- description: input.description,
287
- url: input.url,
288
- siteName: input.siteTitle,
289
- images: [image],
290
- ...(input.locale ? { locale: input.locale } : {}),
291
- };
292
-
293
- const openGraph =
294
- input.type === 'article'
295
- ? {
296
- ...openGraphBase,
297
- type: 'article' as const,
298
- ...(input.publishedTime ? { publishedTime: input.publishedTime } : {}),
299
- }
300
- : { ...openGraphBase, type: 'website' as const };
301
-
302
- return {
303
- openGraph,
304
- twitter: {
305
- card: 'summary_large_image',
306
- title: socialTitle,
307
- description: input.description,
308
- images: [imageUrl],
309
- },
310
- };
311
- }
1
+ import type { Metadata } from 'next';
2
+
3
+ export const DEFAULT_SITE_TITLE = 'NextBlock™ CMS';
4
+
5
+ export const DEFAULT_SITE_DESCRIPTION =
6
+ 'NextBlock is an open-source CMS on Next.js + Supabase — a visual block editor, blazing-fast multilingual pages, and built-in e-commerce.';
7
+
8
+ export const DEFAULT_SITE_KEYWORDS =
9
+ 'NextBlock, CMS, Next.js, Supabase, headless CMS, block editor, visual page builder, multilingual, e-commerce, open source';
10
+
11
+ /** Bundled fallback Open Graph image (resolved to absolute via metadataBase). */
12
+ export const DEFAULT_OG_IMAGE = '/assets/nextblock-banner.jpg';
13
+
14
+ /**
15
+ * Intrinsic pixel size of DEFAULT_OG_IMAGE. Social scrapers trust the declared
16
+ * width/height over the file itself, so these must track the real asset — keep
17
+ * them next to the path so the two cannot drift apart.
18
+ */
19
+ export const DEFAULT_OG_IMAGE_WIDTH = 1672;
20
+ export const DEFAULT_OG_IMAGE_HEIGHT = 941;
21
+
22
+ const DEFAULT_META_DESCRIPTION_LENGTH = 160;
23
+
24
+ function normalizeWhitespace(value: string) {
25
+ return value.replace(/\s+/g, ' ').trim();
26
+ }
27
+
28
+ function decodeHtmlEntities(value: string) {
29
+ const entities: Record<string, string> = {
30
+ amp: '&',
31
+ apos: "'",
32
+ gt: '>',
33
+ lt: '<',
34
+ nbsp: ' ',
35
+ quot: '"',
36
+ };
37
+
38
+ return value.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, entity) => {
39
+ const key = String(entity);
40
+ if (key[0] === '#') {
41
+ const isHex = key[1]?.toLowerCase() === 'x';
42
+ const codePoint = Number.parseInt(key.slice(isHex ? 2 : 1), isHex ? 16 : 10);
43
+ return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;
44
+ }
45
+
46
+ return entities[key] ?? match;
47
+ });
48
+ }
49
+
50
+ export function stripHtmlToText(value: string) {
51
+ return normalizeWhitespace(
52
+ decodeHtmlEntities(
53
+ value
54
+ .replace(/<script[\s\S]*?<\/script>/gi, ' ')
55
+ .replace(/<style[\s\S]*?<\/style>/gi, ' ')
56
+ .replace(/<[^>]+>/g, ' ')
57
+ )
58
+ );
59
+ }
60
+
61
+ function extractParagraphTextFromHtml(value: string) {
62
+ const paragraphs = Array.from(value.matchAll(/<p\b[^>]*>([\s\S]*?)<\/p>/gi))
63
+ .map((match) => stripHtmlToText(match[1] ?? ''))
64
+ .filter(Boolean);
65
+
66
+ if (paragraphs.length > 0) {
67
+ return paragraphs[0];
68
+ }
69
+
70
+ return stripHtmlToText(value.replace(/<h[1-6]\b[\s\S]*?<\/h[1-6]>/gi, ' '));
71
+ }
72
+
73
+ function truncateMetaDescription(value: string, maxLength = DEFAULT_META_DESCRIPTION_LENGTH) {
74
+ const normalized = normalizeWhitespace(value);
75
+ if (normalized.length <= maxLength) {
76
+ return normalized;
77
+ }
78
+
79
+ const truncated = normalized.slice(0, maxLength + 1);
80
+ const lastSpace = truncated.lastIndexOf(' ');
81
+ const candidate = lastSpace > 80 ? truncated.slice(0, lastSpace) : normalized.slice(0, maxLength);
82
+
83
+ return candidate.replace(/[.,;:!?-]+$/, '').trim();
84
+ }
85
+
86
+ function normalizeMetaCandidate(value: string | null | undefined) {
87
+ if (!value) {
88
+ return null;
89
+ }
90
+
91
+ const normalized = stripHtmlToText(value);
92
+ return normalized || null;
93
+ }
94
+
95
+ function collectIntroTextCandidates(value: unknown, candidates: string[]) {
96
+ if (!value) {
97
+ return;
98
+ }
99
+
100
+ if (Array.isArray(value)) {
101
+ value.forEach((item) => collectIntroTextCandidates(item, candidates));
102
+ return;
103
+ }
104
+
105
+ if (typeof value !== 'object') {
106
+ return;
107
+ }
108
+
109
+ const block = value as {
110
+ block_type?: string;
111
+ content?: Record<string, unknown>;
112
+ };
113
+
114
+ if (block.block_type === 'section' || block.block_type === 'hero') {
115
+ collectIntroTextCandidates(block.content?.column_blocks, candidates);
116
+ collectIntroTextCandidates(block.content?.slides, candidates);
117
+ return;
118
+ }
119
+
120
+ if (block.block_type === 'text') {
121
+ const htmlContent = block.content?.html_content;
122
+ const textContent = block.content?.text_content;
123
+ const candidate =
124
+ typeof htmlContent === 'string'
125
+ ? extractParagraphTextFromHtml(htmlContent)
126
+ : typeof textContent === 'string'
127
+ ? normalizeWhitespace(textContent)
128
+ : '';
129
+
130
+ if (candidate) {
131
+ candidates.push(candidate);
132
+ }
133
+ }
134
+ }
135
+
136
+ export function extractIntroExcerptFromBlocks(blocks: unknown) {
137
+ const candidates: string[] = [];
138
+ collectIntroTextCandidates(blocks, candidates);
139
+
140
+ return (
141
+ candidates.find((candidate) => candidate.length >= 80) ??
142
+ candidates[0] ??
143
+ null
144
+ );
145
+ }
146
+
147
+ export function resolveMetaTitle(
148
+ manualTitle: string | null | undefined,
149
+ fallbackTitle: string | null | undefined
150
+ ) {
151
+ return (
152
+ normalizeMetaCandidate(manualTitle) ??
153
+ normalizeMetaCandidate(fallbackTitle) ??
154
+ DEFAULT_SITE_TITLE
155
+ );
156
+ }
157
+
158
+ export function resolveMetaDescription(...candidates: Array<string | null | undefined>) {
159
+ for (const candidate of candidates) {
160
+ const description = normalizeMetaCandidate(candidate);
161
+ if (description) {
162
+ return truncateMetaDescription(description);
163
+ }
164
+ }
165
+
166
+ return DEFAULT_SITE_DESCRIPTION;
167
+ }
168
+
169
+ export function resolvePageMetaDescription(
170
+ manualDescription: string | null | undefined,
171
+ blocks: unknown
172
+ ) {
173
+ return resolveMetaDescription(manualDescription, extractIntroExcerptFromBlocks(blocks));
174
+ }
175
+
176
+ export function resolvePostMetaDescription(
177
+ manualDescription: string | null | undefined,
178
+ subtitle: string | null | undefined
179
+ ) {
180
+ return resolveMetaDescription(manualDescription, subtitle);
181
+ }
182
+
183
+ export function resolveProductMetaDescription(
184
+ manualDescription: string | null | undefined,
185
+ shortDescription: string | null | undefined
186
+ ) {
187
+ return resolveMetaDescription(manualDescription, shortDescription);
188
+ }
189
+
190
+ export function stringifyJsonLd(value: unknown) {
191
+ return JSON.stringify(value).replace(/</g, '\\u003c');
192
+ }
193
+
194
+ /**
195
+ * Appends the site title to a page title for social cards, e.g.
196
+ * `composeTitleWithSite('Home', 'NextBlock™ CMS') === 'Home | NextBlock™ CMS'`.
197
+ * Unlike Next.js' `title.template` (which only affects the `<title>` tag), this
198
+ * lets us produce a complete `og:title` / `twitter:title`.
199
+ */
200
+ export function composeTitleWithSite(
201
+ pageTitle: string | null | undefined,
202
+ siteTitle: string | null | undefined
203
+ ): string {
204
+ const cleanTitle = (pageTitle ?? '').trim();
205
+ const cleanSite = (siteTitle ?? '').trim();
206
+
207
+ if (!cleanSite) return cleanTitle;
208
+ if (!cleanTitle) return cleanSite;
209
+
210
+ const suffix = ` | ${cleanSite}`;
211
+ return cleanTitle === cleanSite || cleanTitle.endsWith(suffix)
212
+ ? cleanTitle
213
+ : `${cleanTitle}${suffix}`;
214
+ }
215
+
216
+ /**
217
+ * Resolves the canonical URL for a public page/post/product.
218
+ *
219
+ * By default this is the self-referencing `<siteUrl><path>`. When the content row
220
+ * sets a manual `custom_canonical` override, that wins:
221
+ * - absolute values (`https://…`) are used verbatim,
222
+ * - root-relative (`/foo`) and bare (`foo`) values are resolved against `siteUrl`.
223
+ * A null/blank override falls back to the self-referencing default, so existing
224
+ * content is unaffected. `siteUrl` may be empty (pre-config / no NEXT_PUBLIC_URL),
225
+ * in which case a relative path is returned and resolved by `metadataBase`.
226
+ */
227
+ export function buildCanonicalUrl(
228
+ customCanonical: string | null | undefined,
229
+ siteUrl: string,
230
+ path: string
231
+ ): string {
232
+ const base = (siteUrl || '').replace(/\/+$/, '');
233
+ const normalizedPath = path.startsWith('/') ? path : `/${path}`;
234
+ const fallback = `${base}${normalizedPath}`;
235
+
236
+ const custom = customCanonical?.trim();
237
+ if (!custom) {
238
+ return fallback;
239
+ }
240
+
241
+ if (/^https?:\/\//i.test(custom)) {
242
+ return custom;
243
+ }
244
+
245
+ return custom.startsWith('/') ? `${base}${custom}` : `${base}/${custom}`;
246
+ }
247
+
248
+ /** Maps a language code (e.g. `fr`, `en-US`) to an Open Graph locale (`fr_FR`). */
249
+ export function toOpenGraphLocale(languageCode?: string | null): string {
250
+ const code = (languageCode ?? '').toLowerCase().split('-')[0];
251
+ const map: Record<string, string> = {
252
+ en: 'en_US',
253
+ fr: 'fr_FR',
254
+ es: 'es_ES',
255
+ de: 'de_DE',
256
+ pt: 'pt_PT',
257
+ it: 'it_IT',
258
+ nl: 'nl_NL',
259
+ };
260
+ return map[code] ?? 'en_US';
261
+ }
262
+
263
+ export interface SocialMetadataInput {
264
+ /** Bare page title (without the site-title suffix). */
265
+ title: string;
266
+ description: string;
267
+ /** Canonical URL of the page (absolute, or path resolved via metadataBase). */
268
+ url: string;
269
+ siteTitle: string;
270
+ /** Resolved feature-image URL; falls back to the default OG image when empty. */
271
+ imageUrl?: string | null;
272
+ type?: 'website' | 'article';
273
+ publishedTime?: string | null;
274
+ locale?: string | null;
275
+ }
276
+
277
+ /**
278
+ * Builds the `openGraph` + `twitter` metadata for a public page so that every
279
+ * page emits a complete, suffixed social title and always has an OG image
280
+ * (the feature image when present, otherwise the bundled default).
281
+ */
282
+ export function buildSocialMetadata(
283
+ input: SocialMetadataInput
284
+ ): Pick<Metadata, 'openGraph' | 'twitter'> {
285
+ const usingDefaultImage = !input.imageUrl;
286
+ const imageUrl = input.imageUrl || DEFAULT_OG_IMAGE;
287
+ const socialTitle = composeTitleWithSite(input.title, input.siteTitle);
288
+ const image = usingDefaultImage
289
+ ? { url: imageUrl, width: 1200, height: 630, alt: socialTitle }
290
+ : { url: imageUrl, alt: socialTitle };
291
+
292
+ const openGraphBase = {
293
+ title: socialTitle,
294
+ description: input.description,
295
+ url: input.url,
296
+ siteName: input.siteTitle,
297
+ images: [image],
298
+ ...(input.locale ? { locale: input.locale } : {}),
299
+ };
300
+
301
+ const openGraph =
302
+ input.type === 'article'
303
+ ? {
304
+ ...openGraphBase,
305
+ type: 'article' as const,
306
+ ...(input.publishedTime ? { publishedTime: input.publishedTime } : {}),
307
+ }
308
+ : { ...openGraphBase, type: 'website' as const };
309
+
310
+ return {
311
+ openGraph,
312
+ twitter: {
313
+ card: 'summary_large_image',
314
+ title: socialTitle,
315
+ description: input.description,
316
+ images: [imageUrl],
317
+ },
318
+ };
319
+ }