emdash 0.9.0 → 0.11.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/{adapters-DoNJiveC.d.mts → adapters-BktHA7EO.d.mts} +1 -1
- package/dist/{adapters-DoNJiveC.d.mts.map → adapters-BktHA7EO.d.mts.map} +1 -1
- package/dist/{apply-BzltprvY.mjs → apply-Ded_1vng.mjs} +167 -254
- package/dist/apply-Ded_1vng.mjs.map +1 -0
- package/dist/astro/index.d.mts +6 -6
- package/dist/astro/index.mjs +10 -2
- package/dist/astro/index.mjs.map +1 -1
- package/dist/astro/middleware/auth.d.mts +5 -5
- package/dist/astro/middleware/auth.mjs +5 -5
- package/dist/astro/middleware/redirect.mjs +5 -5
- package/dist/astro/middleware/request-context.mjs +4 -4
- package/dist/astro/middleware/setup.mjs +1 -1
- package/dist/astro/middleware.d.mts.map +1 -1
- package/dist/astro/middleware.mjs +94 -43
- package/dist/astro/middleware.mjs.map +1 -1
- package/dist/astro/types.d.mts +12 -11
- package/dist/astro/types.d.mts.map +1 -1
- package/dist/{base64-BRICGH2l.mjs → base64-MBPo9ozB.mjs} +1 -1
- package/dist/{base64-BRICGH2l.mjs.map → base64-MBPo9ozB.mjs.map} +1 -1
- package/dist/{byline-BSaNL1w7.mjs → byline-gFn1r0vA.mjs} +4 -4
- package/dist/{byline-BSaNL1w7.mjs.map → byline-gFn1r0vA.mjs.map} +1 -1
- package/dist/{bylines-CvJ3PYz2.mjs → bylines-DTFI8nDM.mjs} +5 -5
- package/dist/{bylines-CvJ3PYz2.mjs.map → bylines-DTFI8nDM.mjs.map} +1 -1
- package/dist/{cache-C6N_hhN7.mjs → cache-BAJbeoZ8.mjs} +3 -3
- package/dist/{cache-C6N_hhN7.mjs.map → cache-BAJbeoZ8.mjs.map} +1 -1
- package/dist/{chunks-NBQVDOci.mjs → chunks-BK1oZS-l.mjs} +2 -2
- package/dist/{chunks-NBQVDOci.mjs.map → chunks-BK1oZS-l.mjs.map} +1 -1
- package/dist/cli/index.mjs +342 -95
- package/dist/cli/index.mjs.map +1 -1
- package/dist/client/cf-access.d.mts +1 -1
- package/dist/client/index.d.mts +1 -1
- package/dist/client/index.mjs +1 -1
- package/dist/{config-BI0V3ICQ.mjs → config-CVssduLe.mjs} +1 -1
- package/dist/{config-BI0V3ICQ.mjs.map → config-CVssduLe.mjs.map} +1 -1
- package/dist/{content-8lOYF0pr.mjs → content-CERxPUN0.mjs} +14 -3
- package/dist/content-CERxPUN0.mjs.map +1 -0
- package/dist/database/instrumentation.d.mts +6 -4
- package/dist/database/instrumentation.d.mts.map +1 -1
- package/dist/database/instrumentation.mjs +19 -7
- package/dist/database/instrumentation.mjs.map +1 -1
- package/dist/db/index.d.mts +3 -3
- package/dist/db/index.mjs +1 -1
- package/dist/db/libsql.d.mts +1 -1
- package/dist/db/postgres.d.mts +1 -1
- package/dist/db/sqlite.d.mts +1 -1
- package/dist/{db-errors-WRezodiz.mjs → db-errors-B7P2pSCn.mjs} +1 -1
- package/dist/{db-errors-WRezodiz.mjs.map → db-errors-B7P2pSCn.mjs.map} +1 -1
- package/dist/{default-D8ksjWhO.mjs → default-pHuz9WF6.mjs} +1 -1
- package/dist/{default-D8ksjWhO.mjs.map → default-pHuz9WF6.mjs.map} +1 -1
- package/dist/{error-D_-tqP-I.mjs → error-DqnRMM5z.mjs} +1 -1
- package/dist/{error-D_-tqP-I.mjs.map → error-DqnRMM5z.mjs.map} +1 -1
- package/dist/{index-BFRaVcD6.d.mts → index-Cg-rC4Gj.d.mts} +110 -87
- package/dist/index-Cg-rC4Gj.d.mts.map +1 -0
- package/dist/index.d.mts +11 -11
- package/dist/index.mjs +29 -28
- package/dist/{load-DDqMMvZL.mjs → load-DR1VwFXR.mjs} +2 -2
- package/dist/{load-DDqMMvZL.mjs.map → load-DR1VwFXR.mjs.map} +1 -1
- package/dist/{loader-CKLbBnhK.mjs → loader-ou_PXAjg.mjs} +31 -6
- package/dist/loader-ou_PXAjg.mjs.map +1 -0
- package/dist/{manifest-schema-DqWNC3lM.mjs → manifest-schema-CXAbd1vH.mjs} +1 -1
- package/dist/{manifest-schema-DqWNC3lM.mjs.map → manifest-schema-CXAbd1vH.mjs.map} +1 -1
- package/dist/media/index.d.mts +1 -1
- package/dist/media/index.mjs +1 -1
- package/dist/media/local-runtime.d.mts +7 -7
- package/dist/media/local-runtime.mjs +3 -3
- package/dist/{media-BW32b4gi.mjs → media-1fFhub9c.mjs} +22 -10
- package/dist/media-1fFhub9c.mjs.map +1 -0
- package/dist/{mode-ier8jbBk.mjs → mode-YhqNVef_.mjs} +1 -1
- package/dist/{mode-ier8jbBk.mjs.map → mode-YhqNVef_.mjs.map} +1 -1
- package/dist/{options-BVp3UsTS.mjs → options-nPxWnrya.mjs} +1 -1
- package/dist/{options-BVp3UsTS.mjs.map → options-nPxWnrya.mjs.map} +1 -1
- package/dist/page/index.d.mts +2 -2
- package/dist/{patterns-CrCYkMBb.mjs → patterns-DsUZ4uxI.mjs} +1 -1
- package/dist/{patterns-CrCYkMBb.mjs.map → patterns-DsUZ4uxI.mjs.map} +1 -1
- package/dist/{placeholder-BE4o_2dc.d.mts → placeholder-CDPtkelt.d.mts} +1 -1
- package/dist/{placeholder-BE4o_2dc.d.mts.map → placeholder-CDPtkelt.d.mts.map} +1 -1
- package/dist/{placeholder-CIJejMlK.mjs → placeholder-Ci0RLeCk.mjs} +1 -1
- package/dist/{placeholder-CIJejMlK.mjs.map → placeholder-Ci0RLeCk.mjs.map} +1 -1
- package/dist/plugins/adapt-sandbox-entry.d.mts +5 -5
- package/dist/plugins/adapt-sandbox-entry.mjs +2 -2
- package/dist/{public-url-DByxYjUw.mjs → public-url-B1AxbbbQ.mjs} +1 -1
- package/dist/{public-url-DByxYjUw.mjs.map → public-url-B1AxbbbQ.mjs.map} +1 -1
- package/dist/{query-Cg9ZKRQ0.mjs → query-8c_meo_K.mjs} +13 -13
- package/dist/{query-Cg9ZKRQ0.mjs.map → query-8c_meo_K.mjs.map} +1 -1
- package/dist/{redirect-BhUBKRc1.mjs → redirect-C5H7VGIX.mjs} +3 -3
- package/dist/{redirect-BhUBKRc1.mjs.map → redirect-C5H7VGIX.mjs.map} +1 -1
- package/dist/{registry-Dw70ChxB.mjs → registry-Do34mz_P.mjs} +7 -6
- package/dist/registry-Do34mz_P.mjs.map +1 -0
- package/dist/{request-cache-B-bmkipQ.mjs → request-cache-D4I69LeL.mjs} +6 -2
- package/dist/request-cache-D4I69LeL.mjs.map +1 -0
- package/dist/request-context.d.mts +27 -1
- package/dist/request-context.d.mts.map +1 -1
- package/dist/request-context.mjs +16 -3
- package/dist/request-context.mjs.map +1 -1
- package/dist/{runner-C7ADox5q.mjs → runner-DIcU2UCC.mjs} +465 -148
- package/dist/runner-DIcU2UCC.mjs.map +1 -0
- package/dist/{runner-Bnoj7vjK.d.mts → runner-Iu3IZSDM.d.mts} +2 -2
- package/dist/{runner-Bnoj7vjK.d.mts.map → runner-Iu3IZSDM.d.mts.map} +1 -1
- package/dist/runtime.d.mts +6 -6
- package/dist/runtime.mjs +3 -3
- package/dist/{search-dOGEccMa.mjs → search-DuWhx4NG.mjs} +322 -108
- package/dist/search-DuWhx4NG.mjs.map +1 -0
- package/dist/{secrets-CW3reAnU.mjs → secrets-CZ8rxLX3.mjs} +3 -3
- package/dist/{secrets-CW3reAnU.mjs.map → secrets-CZ8rxLX3.mjs.map} +1 -1
- package/dist/seed/index.d.mts +2 -2
- package/dist/seed/index.mjs +15 -14
- package/dist/seo/index.d.mts +1 -1
- package/dist/storage/local.d.mts +1 -1
- package/dist/storage/local.mjs +1 -1
- package/dist/storage/s3.d.mts +1 -1
- package/dist/storage/s3.mjs +1 -1
- package/dist/taxonomies-Bw76xAxo.mjs +407 -0
- package/dist/taxonomies-Bw76xAxo.mjs.map +1 -0
- package/dist/taxonomy-D6NvlKo8.mjs +218 -0
- package/dist/taxonomy-D6NvlKo8.mjs.map +1 -0
- package/dist/{tokens-D7zMmWi2.mjs → tokens-CyRDPVW2.mjs} +2 -2
- package/dist/{tokens-D7zMmWi2.mjs.map → tokens-CyRDPVW2.mjs.map} +1 -1
- package/dist/{transaction-Cn2rjY78.mjs → transaction-D44LBXvU.mjs} +1 -1
- package/dist/{transaction-Cn2rjY78.mjs.map → transaction-D44LBXvU.mjs.map} +1 -1
- package/dist/{transport-DNEfeMaU.d.mts → transport-DX_5rpsq.d.mts} +1 -1
- package/dist/{transport-DNEfeMaU.d.mts.map → transport-DX_5rpsq.d.mts.map} +1 -1
- package/dist/{transport-BeMCmin1.mjs → transport-xpzIjCIB.mjs} +1 -1
- package/dist/{transport-BeMCmin1.mjs.map → transport-xpzIjCIB.mjs.map} +1 -1
- package/dist/{types-CIOg5AR8.mjs → types-56BKbld_.mjs} +1 -1
- package/dist/types-56BKbld_.mjs.map +1 -0
- package/dist/{types-CRxNbK-Z.mjs → types-BIgulNsW.mjs} +2 -2
- package/dist/{types-CRxNbK-Z.mjs.map → types-BIgulNsW.mjs.map} +1 -1
- package/dist/{types-CrtWgIvl.d.mts → types-BQx6ZXpR.d.mts} +10 -1
- package/dist/types-BQx6ZXpR.d.mts.map +1 -0
- package/dist/{types-CJsYGpco.d.mts → types-B_CXXnzh.d.mts} +1 -1
- package/dist/{types-CJsYGpco.d.mts.map → types-B_CXXnzh.d.mts.map} +1 -1
- package/dist/{types-M78DQ1lx.d.mts → types-C-aFbqmA.d.mts} +1 -1
- package/dist/{types-M78DQ1lx.d.mts.map → types-C-aFbqmA.d.mts.map} +1 -1
- package/dist/types-DiI8NOG_.mjs +16 -0
- package/dist/types-DiI8NOG_.mjs.map +1 -0
- package/dist/{types-BuBIptGk.d.mts → types-IN5z_S3P.d.mts} +158 -92
- package/dist/types-IN5z_S3P.d.mts.map +1 -0
- package/dist/{types-BSyXeCFW.d.mts → types-IZSZfEwv.d.mts} +4 -3
- package/dist/types-IZSZfEwv.d.mts.map +1 -0
- package/dist/{types-CDbKp7ND.mjs → types-K-EkEQCI.mjs} +1 -1
- package/dist/{types-CDbKp7ND.mjs.map → types-K-EkEQCI.mjs.map} +1 -1
- package/dist/{validate-BfQh_C_y.d.mts → validate-CO3JjFV5.d.mts} +22 -5
- package/dist/validate-CO3JjFV5.d.mts.map +1 -0
- package/dist/{validate-Baqf0slj.mjs → validate-UK4Ja1uo.mjs} +14 -10
- package/dist/validate-UK4Ja1uo.mjs.map +1 -0
- package/dist/{validation-BfEI7tNe.mjs → validation-Vc5DQkJa.mjs} +5 -5
- package/dist/{validation-BfEI7tNe.mjs.map → validation-Vc5DQkJa.mjs.map} +1 -1
- package/dist/version-Bg31I_Ff.mjs +7 -0
- package/dist/{version-DoxrVdYf.mjs.map → version-Bg31I_Ff.mjs.map} +1 -1
- package/dist/{zod-generator-CC0xNe_K.mjs → zod-generator-CHnJUP2l.mjs} +8 -3
- package/dist/zod-generator-CHnJUP2l.mjs.map +1 -0
- package/package.json +9 -8
- package/src/api/errors.ts +5 -0
- package/src/api/handlers/content.ts +20 -0
- package/src/api/handlers/dashboard.ts +29 -36
- package/src/api/handlers/media-allowlist.ts +40 -0
- package/src/api/handlers/media.ts +1 -1
- package/src/api/handlers/menus.ts +400 -89
- package/src/api/handlers/taxonomies.ts +273 -97
- package/src/api/handlers/validate-media-fields.ts +125 -0
- package/src/api/schemas/common.ts +7 -0
- package/src/api/schemas/media.ts +23 -3
- package/src/api/schemas/menus.ts +23 -0
- package/src/api/schemas/schema.ts +11 -2
- package/src/api/schemas/taxonomies.ts +39 -0
- package/src/astro/integration/routes.ts +10 -0
- package/src/astro/middleware.ts +46 -11
- package/src/astro/routes/api/content/[collection]/[id]/permanent.ts +1 -1
- package/src/astro/routes/api/import/wordpress/rewrite-url-helpers.ts +196 -0
- package/src/astro/routes/api/import/wordpress/rewrite-urls.ts +9 -177
- package/src/astro/routes/api/media/upload-url.ts +10 -4
- package/src/astro/routes/api/media.ts +12 -4
- package/src/astro/routes/api/menus/[name]/items.ts +16 -6
- package/src/astro/routes/api/menus/[name]/reorder.ts +8 -3
- package/src/astro/routes/api/menus/[name]/translations.ts +82 -0
- package/src/astro/routes/api/menus/[name].ts +19 -10
- package/src/astro/routes/api/menus/index.ts +9 -6
- package/src/astro/routes/api/taxonomies/[name]/terms/[slug]/translations.ts +89 -0
- package/src/astro/routes/api/taxonomies/[name]/terms/[slug].ts +22 -22
- package/src/astro/routes/api/taxonomies/[name]/terms/index.ts +11 -14
- package/src/astro/routes/api/taxonomies/index.ts +9 -6
- package/src/astro/types.ts +5 -1
- package/src/auth/rate-limit.ts +3 -3
- package/src/cli/commands/bundle-utils.ts +81 -6
- package/src/cli/commands/bundle.ts +18 -15
- package/src/cli/commands/export-seed.ts +139 -24
- package/src/cli/commands/plugin-init.ts +216 -90
- package/src/database/instrumentation.ts +22 -8
- package/src/database/migrations/016_api_tokens.ts +18 -3
- package/src/database/migrations/036_i18n_menus_and_taxonomies.ts +477 -0
- package/src/database/migrations/037_credential_algorithm.ts +18 -0
- package/src/database/migrations/runner.ts +4 -0
- package/src/database/repositories/content.ts +11 -0
- package/src/database/repositories/media.ts +40 -10
- package/src/database/repositories/taxonomy.ts +193 -89
- package/src/database/types.ts +12 -3
- package/src/emdash-runtime.ts +16 -3
- package/src/fields/file.ts +7 -6
- package/src/fields/image.ts +12 -11
- package/src/fields/types.ts +3 -0
- package/src/i18n/resolve.ts +37 -0
- package/src/index.ts +1 -1
- package/src/loader.ts +49 -2
- package/src/mcp/server.ts +114 -26
- package/src/media/mime.ts +75 -0
- package/src/menus/index.ts +143 -124
- package/src/menus/types.ts +15 -1
- package/src/plugins/types.ts +81 -191
- package/src/request-cache.ts +6 -2
- package/src/request-context.ts +42 -2
- package/src/schema/registry.ts +5 -5
- package/src/schema/types.ts +3 -2
- package/src/schema/zod-generator.ts +12 -2
- package/src/seed/apply.ts +157 -54
- package/src/seed/types.ts +18 -1
- package/src/seed/validate.ts +27 -13
- package/src/taxonomies/index.ts +230 -213
- package/src/taxonomies/types.ts +10 -0
- package/dist/apply-BzltprvY.mjs.map +0 -1
- package/dist/content-8lOYF0pr.mjs.map +0 -1
- package/dist/index-BFRaVcD6.d.mts.map +0 -1
- package/dist/loader-CKLbBnhK.mjs.map +0 -1
- package/dist/media-BW32b4gi.mjs.map +0 -1
- package/dist/registry-Dw70ChxB.mjs.map +0 -1
- package/dist/request-cache-B-bmkipQ.mjs.map +0 -1
- package/dist/runner-C7ADox5q.mjs.map +0 -1
- package/dist/search-dOGEccMa.mjs.map +0 -1
- package/dist/taxonomies-ZlRtD6AG.mjs +0 -315
- package/dist/taxonomies-ZlRtD6AG.mjs.map +0 -1
- package/dist/types-4fVtCIm0.mjs +0 -68
- package/dist/types-4fVtCIm0.mjs.map +0 -1
- package/dist/types-BSyXeCFW.d.mts.map +0 -1
- package/dist/types-BuBIptGk.d.mts.map +0 -1
- package/dist/types-CIOg5AR8.mjs.map +0 -1
- package/dist/types-CrtWgIvl.d.mts.map +0 -1
- package/dist/validate-Baqf0slj.mjs.map +0 -1
- package/dist/validate-BfQh_C_y.d.mts.map +0 -1
- package/dist/version-DoxrVdYf.mjs +0 -7
- package/dist/zod-generator-CC0xNe_K.mjs.map +0 -1
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
const REGEX_SPECIAL_CHARS = /[.*+?^${}()|[\]\\]/g;
|
|
2
|
+
const WORDPRESS_IMAGE_SIZE_SUFFIX = /-\d+x\d+(?=\.[^./?#]+$)/;
|
|
3
|
+
const BASE_URL_EXTENSION = /^(.+)(\.[^./?#]+)$/;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Strip query parameters from a URL for base matching
|
|
7
|
+
*/
|
|
8
|
+
export function getBaseUrl(url: string): string {
|
|
9
|
+
try {
|
|
10
|
+
const parsed = new URL(url);
|
|
11
|
+
return `${parsed.origin}${parsed.pathname}`;
|
|
12
|
+
} catch {
|
|
13
|
+
// If URL parsing fails, try simple string split
|
|
14
|
+
return url.split("?")[0] || url;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build a map of base URLs to new URLs for flexible matching
|
|
20
|
+
*/
|
|
21
|
+
export function buildBaseUrlMap(urlMap: Record<string, string>): Map<string, string> {
|
|
22
|
+
const baseMap = new Map<string, string>();
|
|
23
|
+
for (const [oldUrl, newUrl] of Object.entries(urlMap)) {
|
|
24
|
+
const baseUrl = getBaseUrl(oldUrl);
|
|
25
|
+
baseMap.set(baseUrl, newUrl);
|
|
26
|
+
}
|
|
27
|
+
return baseMap;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Find matching new URL for a given URL, checking exact, base, and WordPress image-size matches
|
|
32
|
+
*/
|
|
33
|
+
export function findMatchingUrl(
|
|
34
|
+
url: string,
|
|
35
|
+
exactMap: Record<string, string>,
|
|
36
|
+
baseMap: Map<string, string>,
|
|
37
|
+
): string | null {
|
|
38
|
+
if (exactMap[url]) {
|
|
39
|
+
return exactMap[url];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const baseUrl = getBaseUrl(url);
|
|
43
|
+
const baseMatch = baseMap.get(baseUrl);
|
|
44
|
+
if (baseMatch) {
|
|
45
|
+
return baseMatch;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const wordPressImageMatch = baseMap.get(stripWordPressImageSizeSuffix(baseUrl));
|
|
49
|
+
if (wordPressImageMatch) {
|
|
50
|
+
return wordPressImageMatch;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Portable Text block type (simplified for URL rewriting)
|
|
58
|
+
*/
|
|
59
|
+
export interface PortableTextBlock {
|
|
60
|
+
_type: string;
|
|
61
|
+
_key?: string;
|
|
62
|
+
asset?: {
|
|
63
|
+
_type?: string;
|
|
64
|
+
_ref?: string;
|
|
65
|
+
url?: string;
|
|
66
|
+
};
|
|
67
|
+
link?: string;
|
|
68
|
+
// For nested content like galleries
|
|
69
|
+
images?: PortableTextBlock[];
|
|
70
|
+
columns?: Array<{ content?: PortableTextBlock[] }>;
|
|
71
|
+
[key: string]: unknown;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Rewrite URLs in a Portable Text array, returning whether any changes were made
|
|
76
|
+
*/
|
|
77
|
+
export function rewritePortableTextUrls(
|
|
78
|
+
blocks: PortableTextBlock[],
|
|
79
|
+
exactMap: Record<string, string>,
|
|
80
|
+
baseMap: Map<string, string>,
|
|
81
|
+
): { changed: boolean; urlsRewritten: number } {
|
|
82
|
+
let changed = false;
|
|
83
|
+
let urlsRewritten = 0;
|
|
84
|
+
|
|
85
|
+
for (const block of blocks) {
|
|
86
|
+
// Handle image blocks
|
|
87
|
+
if (block._type === "image" && block.asset?.url) {
|
|
88
|
+
const newUrl = findMatchingUrl(block.asset.url, exactMap, baseMap);
|
|
89
|
+
if (newUrl) {
|
|
90
|
+
block.asset.url = newUrl;
|
|
91
|
+
block.asset._ref = newUrl; // Also update the reference
|
|
92
|
+
changed = true;
|
|
93
|
+
urlsRewritten++;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Handle image link URLs (for linked images)
|
|
98
|
+
if (block._type === "image" && block.link) {
|
|
99
|
+
const newUrl = findMatchingUrl(block.link, exactMap, baseMap);
|
|
100
|
+
if (newUrl) {
|
|
101
|
+
block.link = newUrl;
|
|
102
|
+
changed = true;
|
|
103
|
+
urlsRewritten++;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Handle gallery blocks with nested images
|
|
108
|
+
if (block._type === "gallery" && Array.isArray(block.images)) {
|
|
109
|
+
const result = rewritePortableTextUrls(block.images, exactMap, baseMap);
|
|
110
|
+
if (result.changed) {
|
|
111
|
+
changed = true;
|
|
112
|
+
urlsRewritten += result.urlsRewritten;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Handle columns blocks with nested content
|
|
117
|
+
if (block._type === "columns" && Array.isArray(block.columns)) {
|
|
118
|
+
for (const column of block.columns) {
|
|
119
|
+
if (Array.isArray(column.content)) {
|
|
120
|
+
const result = rewritePortableTextUrls(column.content, exactMap, baseMap);
|
|
121
|
+
if (result.changed) {
|
|
122
|
+
changed = true;
|
|
123
|
+
urlsRewritten += result.urlsRewritten;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return { changed, urlsRewritten };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Rewrite URLs in a string field using simple string replacement
|
|
135
|
+
*/
|
|
136
|
+
export function rewriteStringUrls(
|
|
137
|
+
value: string,
|
|
138
|
+
exactMap: Record<string, string>,
|
|
139
|
+
baseMap: Map<string, string>,
|
|
140
|
+
): { newValue: string; changed: boolean; urlsRewritten: number } {
|
|
141
|
+
let newValue = value;
|
|
142
|
+
let changed = false;
|
|
143
|
+
let urlsRewritten = 0;
|
|
144
|
+
|
|
145
|
+
// Try exact matches first
|
|
146
|
+
for (const [oldUrl, newUrl] of Object.entries(exactMap)) {
|
|
147
|
+
if (newValue.includes(oldUrl)) {
|
|
148
|
+
newValue = newValue.split(oldUrl).join(newUrl);
|
|
149
|
+
changed = true;
|
|
150
|
+
urlsRewritten++;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// For base URL matching in strings, we need to be more careful
|
|
155
|
+
// Only match if we find a URL that starts with the base
|
|
156
|
+
for (const [baseUrl, newUrl] of baseMap.entries()) {
|
|
157
|
+
// Look for the base URL followed by optional query string or end
|
|
158
|
+
const regex = buildBaseUrlMatchRegex(baseUrl);
|
|
159
|
+
const matches = newValue.match(regex);
|
|
160
|
+
if (matches) {
|
|
161
|
+
for (const match of matches) {
|
|
162
|
+
// Don't replace if we already have an exact match in the map
|
|
163
|
+
if (!exactMap[match]) {
|
|
164
|
+
newValue = newValue.split(match).join(newUrl);
|
|
165
|
+
changed = true;
|
|
166
|
+
urlsRewritten++;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { newValue, changed, urlsRewritten };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Escape special regex characters in a string
|
|
177
|
+
*/
|
|
178
|
+
function escapeRegExp(string: string): string {
|
|
179
|
+
return string.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function stripWordPressImageSizeSuffix(url: string): string {
|
|
183
|
+
return url.replace(WORDPRESS_IMAGE_SIZE_SUFFIX, "");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function buildBaseUrlMatchRegex(baseUrl: string): RegExp {
|
|
187
|
+
const extensionMatch = BASE_URL_EXTENSION.exec(baseUrl);
|
|
188
|
+
const basePattern = extensionMatch
|
|
189
|
+
? `${escapeRegExp(extensionMatch[1])}(?:-\\d+x\\d+)?${escapeRegExp(extensionMatch[2])}`
|
|
190
|
+
: escapeRegExp(baseUrl);
|
|
191
|
+
|
|
192
|
+
return new RegExp(
|
|
193
|
+
`${basePattern}(\\?[^"'\\s]*)?(?=$|["'\\s<>)\\],;:!?]|\\.(?=$|["'\\s<>)\\]]))`,
|
|
194
|
+
"g",
|
|
195
|
+
);
|
|
196
|
+
}
|
|
@@ -22,9 +22,13 @@ import { normalizeMediaValue } from "#media/normalize.js";
|
|
|
22
22
|
import type { MediaProvider } from "#media/types.js";
|
|
23
23
|
import type { EmDashHandlers } from "#types";
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
import {
|
|
26
|
+
buildBaseUrlMap,
|
|
27
|
+
findMatchingUrl,
|
|
28
|
+
rewritePortableTextUrls,
|
|
29
|
+
rewriteStringUrls,
|
|
30
|
+
} from "./rewrite-url-helpers.js";
|
|
31
|
+
import type { PortableTextBlock } from "./rewrite-url-helpers.js";
|
|
28
32
|
|
|
29
33
|
export interface RewriteUrlsResult {
|
|
30
34
|
/** Total items updated */
|
|
@@ -37,6 +41,8 @@ export interface RewriteUrlsResult {
|
|
|
37
41
|
errors: Array<{ collection: string; id: string; error: string }>;
|
|
38
42
|
}
|
|
39
43
|
|
|
44
|
+
export const prerender = false;
|
|
45
|
+
|
|
40
46
|
export const POST: APIRoute = async ({ request, locals }) => {
|
|
41
47
|
const { emdash, user } = locals;
|
|
42
48
|
|
|
@@ -70,180 +76,6 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
70
76
|
}
|
|
71
77
|
};
|
|
72
78
|
|
|
73
|
-
/**
|
|
74
|
-
* Strip query parameters from a URL for base matching
|
|
75
|
-
*/
|
|
76
|
-
function getBaseUrl(url: string): string {
|
|
77
|
-
try {
|
|
78
|
-
const parsed = new URL(url);
|
|
79
|
-
return `${parsed.origin}${parsed.pathname}`;
|
|
80
|
-
} catch {
|
|
81
|
-
// If URL parsing fails, try simple string split
|
|
82
|
-
return url.split("?")[0] || url;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Build a map of base URLs to new URLs for flexible matching
|
|
88
|
-
*/
|
|
89
|
-
function buildBaseUrlMap(urlMap: Record<string, string>): Map<string, string> {
|
|
90
|
-
const baseMap = new Map<string, string>();
|
|
91
|
-
for (const [oldUrl, newUrl] of Object.entries(urlMap)) {
|
|
92
|
-
const baseUrl = getBaseUrl(oldUrl);
|
|
93
|
-
baseMap.set(baseUrl, newUrl);
|
|
94
|
-
}
|
|
95
|
-
return baseMap;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Find matching new URL for a given URL, checking both exact and base matches
|
|
100
|
-
*/
|
|
101
|
-
function findMatchingUrl(
|
|
102
|
-
url: string,
|
|
103
|
-
exactMap: Record<string, string>,
|
|
104
|
-
baseMap: Map<string, string>,
|
|
105
|
-
): string | null {
|
|
106
|
-
// Try exact match first
|
|
107
|
-
if (exactMap[url]) {
|
|
108
|
-
return exactMap[url];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Try base URL match (ignoring query params)
|
|
112
|
-
const baseUrl = getBaseUrl(url);
|
|
113
|
-
const baseMatch = baseMap.get(baseUrl);
|
|
114
|
-
if (baseMatch) {
|
|
115
|
-
return baseMatch;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Portable Text block type (simplified for URL rewriting)
|
|
123
|
-
*/
|
|
124
|
-
interface PortableTextBlock {
|
|
125
|
-
_type: string;
|
|
126
|
-
_key?: string;
|
|
127
|
-
asset?: {
|
|
128
|
-
_type?: string;
|
|
129
|
-
_ref?: string;
|
|
130
|
-
url?: string;
|
|
131
|
-
};
|
|
132
|
-
link?: string;
|
|
133
|
-
// For nested content like galleries
|
|
134
|
-
images?: PortableTextBlock[];
|
|
135
|
-
columns?: Array<{ content?: PortableTextBlock[] }>;
|
|
136
|
-
[key: string]: unknown;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Rewrite URLs in a Portable Text array, returning whether any changes were made
|
|
141
|
-
*/
|
|
142
|
-
function rewritePortableTextUrls(
|
|
143
|
-
blocks: PortableTextBlock[],
|
|
144
|
-
exactMap: Record<string, string>,
|
|
145
|
-
baseMap: Map<string, string>,
|
|
146
|
-
): { changed: boolean; urlsRewritten: number } {
|
|
147
|
-
let changed = false;
|
|
148
|
-
let urlsRewritten = 0;
|
|
149
|
-
|
|
150
|
-
for (const block of blocks) {
|
|
151
|
-
// Handle image blocks
|
|
152
|
-
if (block._type === "image" && block.asset?.url) {
|
|
153
|
-
const newUrl = findMatchingUrl(block.asset.url, exactMap, baseMap);
|
|
154
|
-
if (newUrl) {
|
|
155
|
-
block.asset.url = newUrl;
|
|
156
|
-
block.asset._ref = newUrl; // Also update the reference
|
|
157
|
-
changed = true;
|
|
158
|
-
urlsRewritten++;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Handle image link URLs (for linked images)
|
|
163
|
-
if (block._type === "image" && block.link) {
|
|
164
|
-
const newUrl = findMatchingUrl(block.link, exactMap, baseMap);
|
|
165
|
-
if (newUrl) {
|
|
166
|
-
block.link = newUrl;
|
|
167
|
-
changed = true;
|
|
168
|
-
urlsRewritten++;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Handle gallery blocks with nested images
|
|
173
|
-
if (block._type === "gallery" && Array.isArray(block.images)) {
|
|
174
|
-
const result = rewritePortableTextUrls(block.images, exactMap, baseMap);
|
|
175
|
-
if (result.changed) {
|
|
176
|
-
changed = true;
|
|
177
|
-
urlsRewritten += result.urlsRewritten;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Handle columns blocks with nested content
|
|
182
|
-
if (block._type === "columns" && Array.isArray(block.columns)) {
|
|
183
|
-
for (const column of block.columns) {
|
|
184
|
-
if (Array.isArray(column.content)) {
|
|
185
|
-
const result = rewritePortableTextUrls(column.content, exactMap, baseMap);
|
|
186
|
-
if (result.changed) {
|
|
187
|
-
changed = true;
|
|
188
|
-
urlsRewritten += result.urlsRewritten;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return { changed, urlsRewritten };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Rewrite URLs in a string field using simple string replacement
|
|
200
|
-
*/
|
|
201
|
-
function rewriteStringUrls(
|
|
202
|
-
value: string,
|
|
203
|
-
exactMap: Record<string, string>,
|
|
204
|
-
baseMap: Map<string, string>,
|
|
205
|
-
): { newValue: string; changed: boolean; urlsRewritten: number } {
|
|
206
|
-
let newValue = value;
|
|
207
|
-
let changed = false;
|
|
208
|
-
let urlsRewritten = 0;
|
|
209
|
-
|
|
210
|
-
// Try exact matches first
|
|
211
|
-
for (const [oldUrl, newUrl] of Object.entries(exactMap)) {
|
|
212
|
-
if (newValue.includes(oldUrl)) {
|
|
213
|
-
newValue = newValue.split(oldUrl).join(newUrl);
|
|
214
|
-
changed = true;
|
|
215
|
-
urlsRewritten++;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// For base URL matching in strings, we need to be more careful
|
|
220
|
-
// Only match if we find a URL that starts with the base
|
|
221
|
-
for (const [baseUrl, newUrl] of baseMap.entries()) {
|
|
222
|
-
// Look for the base URL followed by optional query string or end
|
|
223
|
-
const regex = new RegExp(escapeRegExp(baseUrl) + "(\\?[^\"'\\s]*)?", "g");
|
|
224
|
-
const matches = newValue.match(regex);
|
|
225
|
-
if (matches) {
|
|
226
|
-
for (const match of matches) {
|
|
227
|
-
// Don't replace if we already have an exact match in the map
|
|
228
|
-
if (!exactMap[match]) {
|
|
229
|
-
newValue = newValue.split(match).join(newUrl);
|
|
230
|
-
changed = true;
|
|
231
|
-
urlsRewritten++;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return { newValue, changed, urlsRewritten };
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Escape special regex characters in a string
|
|
242
|
-
*/
|
|
243
|
-
function escapeRegExp(string: string): string {
|
|
244
|
-
return string.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
245
|
-
}
|
|
246
|
-
|
|
247
79
|
async function rewriteUrls(
|
|
248
80
|
db: NonNullable<EmDashHandlers["db"]>,
|
|
249
81
|
urlMap: Record<string, string>,
|
|
@@ -15,8 +15,10 @@ import { ulid } from "ulidx";
|
|
|
15
15
|
|
|
16
16
|
import { requirePerm } from "#api/authorize.js";
|
|
17
17
|
import { apiError, apiSuccess, handleError } from "#api/error.js";
|
|
18
|
+
import { GLOBAL_UPLOAD_ALLOWLIST, resolveFieldAllowlist } from "#api/handlers/media-allowlist.js";
|
|
18
19
|
import { isParseError, parseBody } from "#api/parse.js";
|
|
19
20
|
import { DEFAULT_MAX_UPLOAD_SIZE, mediaUploadUrlBody } from "#api/schemas.js";
|
|
21
|
+
import { matchesMimeAllowlist, normalizeMime } from "#media/mime.js";
|
|
20
22
|
|
|
21
23
|
export const prerender = false;
|
|
22
24
|
|
|
@@ -70,9 +72,13 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
70
72
|
const body = await parseBody(request, mediaUploadUrlBody(maxSize));
|
|
71
73
|
if (isParseError(body)) return body;
|
|
72
74
|
|
|
73
|
-
// Validate content type
|
|
74
|
-
const
|
|
75
|
-
|
|
75
|
+
// Validate content type (field-aware widening)
|
|
76
|
+
const fieldAllowlist = body.fieldId
|
|
77
|
+
? await resolveFieldAllowlist(emdash.db, body.fieldId)
|
|
78
|
+
: null;
|
|
79
|
+
const allowlist = fieldAllowlist ?? [...GLOBAL_UPLOAD_ALLOWLIST];
|
|
80
|
+
|
|
81
|
+
if (!matchesMimeAllowlist(body.contentType, allowlist)) {
|
|
76
82
|
return apiError("INVALID_TYPE", "File type not allowed", 400);
|
|
77
83
|
}
|
|
78
84
|
|
|
@@ -100,7 +106,7 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
100
106
|
// Create pending media record with content hash
|
|
101
107
|
const mediaItem = await repo.createPending({
|
|
102
108
|
filename: body.filename,
|
|
103
|
-
mimeType: body.contentType,
|
|
109
|
+
mimeType: normalizeMime(body.contentType),
|
|
104
110
|
size: body.size,
|
|
105
111
|
storageKey,
|
|
106
112
|
contentHash: body.contentHash,
|
|
@@ -12,9 +12,11 @@ import { ulid } from "ulidx";
|
|
|
12
12
|
|
|
13
13
|
import { requirePerm } from "#api/authorize.js";
|
|
14
14
|
import { apiError, apiSuccess, handleError, unwrapResult } from "#api/error.js";
|
|
15
|
+
import { GLOBAL_UPLOAD_ALLOWLIST, resolveFieldAllowlist } from "#api/handlers/media-allowlist.js";
|
|
15
16
|
import { isParseError, parseQuery } from "#api/parse.js";
|
|
16
17
|
import { DEFAULT_MAX_UPLOAD_SIZE, formatFileSize, mediaListQuery } from "#api/schemas.js";
|
|
17
18
|
import { MediaRepository } from "#db/repositories/media.js";
|
|
19
|
+
import { matchesMimeAllowlist, normalizeMime } from "#media/mime.js";
|
|
18
20
|
import { generatePlaceholder } from "#media/placeholder.js";
|
|
19
21
|
import { computeContentHash } from "#utils/hash.js";
|
|
20
22
|
|
|
@@ -106,9 +108,15 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
106
108
|
return apiError("NO_FILE", "No file provided", 400);
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
// Validate file type
|
|
110
|
-
const
|
|
111
|
-
|
|
111
|
+
// Validate file type — widen the allowlist when a field-specific list is configured
|
|
112
|
+
const fieldIdEntry = formData.get("fieldId");
|
|
113
|
+
const fieldId =
|
|
114
|
+
typeof fieldIdEntry === "string" && fieldIdEntry.length > 0 ? fieldIdEntry : null;
|
|
115
|
+
|
|
116
|
+
const fieldAllowlist = fieldId ? await resolveFieldAllowlist(emdash.db, fieldId) : null;
|
|
117
|
+
const allowlist = fieldAllowlist ?? [...GLOBAL_UPLOAD_ALLOWLIST];
|
|
118
|
+
|
|
119
|
+
if (!matchesMimeAllowlist(file.type, allowlist)) {
|
|
112
120
|
return apiError("INVALID_TYPE", "File type not allowed", 400);
|
|
113
121
|
}
|
|
114
122
|
|
|
@@ -174,7 +182,7 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
174
182
|
// Create media record
|
|
175
183
|
const result = await emdash.handleMediaCreate({
|
|
176
184
|
filename: file.name,
|
|
177
|
-
mimeType: file.type,
|
|
185
|
+
mimeType: normalizeMime(file.type),
|
|
178
186
|
size: file.size,
|
|
179
187
|
width,
|
|
180
188
|
height,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Menu items CRUD endpoints
|
|
3
3
|
*
|
|
4
|
-
* POST /_emdash/api/menus/:name/items
|
|
5
|
-
* PUT /_emdash/api/menus/:name/items
|
|
6
|
-
* DELETE /_emdash/api/menus/:name/items
|
|
4
|
+
* POST /_emdash/api/menus/:name/items[?locale=xx]
|
|
5
|
+
* PUT /_emdash/api/menus/:name/items?id=...[&locale=xx]
|
|
6
|
+
* DELETE /_emdash/api/menus/:name/items?id=...[&locale=xx]
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { APIRoute } from "astro";
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
import { isParseError, parseBody, parseQuery } from "#api/parse.js";
|
|
19
19
|
import {
|
|
20
20
|
createMenuItemBody,
|
|
21
|
+
localeFilterQuery,
|
|
21
22
|
menuItemDeleteQuery,
|
|
22
23
|
menuItemUpdateQuery,
|
|
23
24
|
updateMenuItemBody,
|
|
@@ -32,11 +33,14 @@ export const POST: APIRoute = async ({ params, request, locals }) => {
|
|
|
32
33
|
const denied = requirePerm(user, "menus:manage");
|
|
33
34
|
if (denied) return denied;
|
|
34
35
|
|
|
36
|
+
const localeQ = parseQuery(new URL(request.url), localeFilterQuery);
|
|
37
|
+
if (isParseError(localeQ)) return localeQ;
|
|
38
|
+
|
|
35
39
|
try {
|
|
36
40
|
const body = await parseBody(request, createMenuItemBody);
|
|
37
41
|
if (isParseError(body)) return body;
|
|
38
42
|
|
|
39
|
-
const result = await handleMenuItemCreate(emdash.db, name, body);
|
|
43
|
+
const result = await handleMenuItemCreate(emdash.db, name, body, { locale: localeQ.locale });
|
|
40
44
|
return unwrapResult(result, 201);
|
|
41
45
|
} catch (error) {
|
|
42
46
|
return handleError(error, "Failed to create menu item", "MENU_ITEM_CREATE_ERROR");
|
|
@@ -53,13 +57,17 @@ export const PUT: APIRoute = async ({ params, request, locals }) => {
|
|
|
53
57
|
const url = new URL(request.url);
|
|
54
58
|
const query = parseQuery(url, menuItemUpdateQuery);
|
|
55
59
|
if (isParseError(query)) return query;
|
|
60
|
+
const localeQ = parseQuery(url, localeFilterQuery);
|
|
61
|
+
if (isParseError(localeQ)) return localeQ;
|
|
56
62
|
const itemId = query.id;
|
|
57
63
|
|
|
58
64
|
try {
|
|
59
65
|
const body = await parseBody(request, updateMenuItemBody);
|
|
60
66
|
if (isParseError(body)) return body;
|
|
61
67
|
|
|
62
|
-
const result = await handleMenuItemUpdate(emdash.db, name, itemId, body
|
|
68
|
+
const result = await handleMenuItemUpdate(emdash.db, name, itemId, body, {
|
|
69
|
+
locale: localeQ.locale,
|
|
70
|
+
});
|
|
63
71
|
return unwrapResult(result);
|
|
64
72
|
} catch (error) {
|
|
65
73
|
return handleError(error, "Failed to update menu item", "MENU_ITEM_UPDATE_ERROR");
|
|
@@ -76,10 +84,12 @@ export const DELETE: APIRoute = async ({ params, request, locals }) => {
|
|
|
76
84
|
const url = new URL(request.url);
|
|
77
85
|
const query = parseQuery(url, menuItemDeleteQuery);
|
|
78
86
|
if (isParseError(query)) return query;
|
|
87
|
+
const localeQ = parseQuery(url, localeFilterQuery);
|
|
88
|
+
if (isParseError(localeQ)) return localeQ;
|
|
79
89
|
const itemId = query.id;
|
|
80
90
|
|
|
81
91
|
try {
|
|
82
|
-
const result = await handleMenuItemDelete(emdash.db, name, itemId);
|
|
92
|
+
const result = await handleMenuItemDelete(emdash.db, name, itemId, { locale: localeQ.locale });
|
|
83
93
|
return unwrapResult(result);
|
|
84
94
|
} catch (error) {
|
|
85
95
|
return handleError(error, "Failed to delete menu item", "MENU_ITEM_DELETE_ERROR");
|
|
@@ -9,8 +9,8 @@ import type { APIRoute } from "astro";
|
|
|
9
9
|
import { requirePerm } from "#api/authorize.js";
|
|
10
10
|
import { handleError, unwrapResult } from "#api/error.js";
|
|
11
11
|
import { handleMenuItemReorder } from "#api/handlers/menus.js";
|
|
12
|
-
import { isParseError, parseBody } from "#api/parse.js";
|
|
13
|
-
import { reorderMenuItemsBody } from "#api/schemas.js";
|
|
12
|
+
import { isParseError, parseBody, parseQuery } from "#api/parse.js";
|
|
13
|
+
import { localeFilterQuery, reorderMenuItemsBody } from "#api/schemas.js";
|
|
14
14
|
|
|
15
15
|
export const prerender = false;
|
|
16
16
|
|
|
@@ -21,11 +21,16 @@ export const POST: APIRoute = async ({ params, request, locals }) => {
|
|
|
21
21
|
const denied = requirePerm(user, "menus:manage");
|
|
22
22
|
if (denied) return denied;
|
|
23
23
|
|
|
24
|
+
const localeQ = parseQuery(new URL(request.url), localeFilterQuery);
|
|
25
|
+
if (isParseError(localeQ)) return localeQ;
|
|
26
|
+
|
|
24
27
|
try {
|
|
25
28
|
const body = await parseBody(request, reorderMenuItemsBody);
|
|
26
29
|
if (isParseError(body)) return body;
|
|
27
30
|
|
|
28
|
-
const result = await handleMenuItemReorder(emdash.db, name, body.items
|
|
31
|
+
const result = await handleMenuItemReorder(emdash.db, name, body.items, {
|
|
32
|
+
locale: localeQ.locale,
|
|
33
|
+
});
|
|
29
34
|
return unwrapResult(result);
|
|
30
35
|
} catch (error) {
|
|
31
36
|
return handleError(error, "Failed to reorder menu items", "MENU_REORDER_ERROR");
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Menu translation endpoints
|
|
3
|
+
*
|
|
4
|
+
* GET /_emdash/api/menus/:name/translations — list translations for a menu (uses any locale row)
|
|
5
|
+
* POST /_emdash/api/menus/:name/translations — create a new locale translation (body: { locale, label })
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { APIRoute } from "astro";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { requirePerm } from "#api/authorize.js";
|
|
12
|
+
import { handleError, requireDb, unwrapResult } from "#api/error.js";
|
|
13
|
+
import { handleMenuCreate, handleMenuGet, handleMenuTranslations } from "#api/handlers/menus.js";
|
|
14
|
+
import { isParseError, parseBody, parseQuery } from "#api/parse.js";
|
|
15
|
+
import { localeFilterQuery } from "#api/schemas.js";
|
|
16
|
+
|
|
17
|
+
export const prerender = false;
|
|
18
|
+
|
|
19
|
+
const createTranslationBody = z
|
|
20
|
+
.object({
|
|
21
|
+
locale: z.string().min(1),
|
|
22
|
+
label: z.string().min(1).optional(),
|
|
23
|
+
})
|
|
24
|
+
.meta({ id: "CreateMenuTranslationBody" });
|
|
25
|
+
|
|
26
|
+
export const GET: APIRoute = async ({ params, request, locals }) => {
|
|
27
|
+
const { emdash, user } = locals;
|
|
28
|
+
const name = params.name!;
|
|
29
|
+
|
|
30
|
+
const dbErr = requireDb(emdash?.db);
|
|
31
|
+
if (dbErr) return dbErr;
|
|
32
|
+
|
|
33
|
+
const denied = requirePerm(user, "menus:read");
|
|
34
|
+
if (denied) return denied;
|
|
35
|
+
|
|
36
|
+
const localeQ = parseQuery(new URL(request.url), localeFilterQuery);
|
|
37
|
+
if (isParseError(localeQ)) return localeQ;
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
// Look up any menu row matching the name so we can get its translation_group.
|
|
41
|
+
const anchor = await handleMenuGet(emdash.db, name, { locale: localeQ.locale });
|
|
42
|
+
if (!anchor.success) return unwrapResult(anchor);
|
|
43
|
+
const result = await handleMenuTranslations(emdash.db, anchor.data.id);
|
|
44
|
+
return unwrapResult(result);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return handleError(error, "Failed to fetch menu translations", "MENU_TRANSLATIONS_ERROR");
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const POST: APIRoute = async ({ params, request, locals }) => {
|
|
51
|
+
const { emdash, user } = locals;
|
|
52
|
+
const name = params.name!;
|
|
53
|
+
|
|
54
|
+
const dbErr = requireDb(emdash?.db);
|
|
55
|
+
if (dbErr) return dbErr;
|
|
56
|
+
|
|
57
|
+
const denied = requirePerm(user, "menus:manage");
|
|
58
|
+
if (denied) return denied;
|
|
59
|
+
|
|
60
|
+
const localeQ = parseQuery(new URL(request.url), localeFilterQuery);
|
|
61
|
+
if (isParseError(localeQ)) return localeQ;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const body = await parseBody(request, createTranslationBody);
|
|
65
|
+
if (isParseError(body)) return body;
|
|
66
|
+
|
|
67
|
+
// Resolve the source menu (either by explicit locale in query, or the
|
|
68
|
+
// first matching row). Its id becomes the `translationOf` for the new row.
|
|
69
|
+
const source = await handleMenuGet(emdash.db, name, { locale: localeQ.locale });
|
|
70
|
+
if (!source.success) return unwrapResult(source);
|
|
71
|
+
|
|
72
|
+
const result = await handleMenuCreate(emdash.db, {
|
|
73
|
+
name,
|
|
74
|
+
label: body.label ?? source.data.label,
|
|
75
|
+
locale: body.locale,
|
|
76
|
+
translationOf: source.data.id,
|
|
77
|
+
});
|
|
78
|
+
return unwrapResult(result, 201);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return handleError(error, "Failed to create menu translation", "MENU_TRANSLATION_CREATE_ERROR");
|
|
81
|
+
}
|
|
82
|
+
};
|