UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.66 kB
{"version":3,"file":"format-media-browser-key.mjs","names":[],"sources":["../../../src/utils/media/format-media-browser-key.ts"],"sourcesContent":["import slug from \"slug\";\n\nconst getFileNameWithoutExtension = (value: string) => {\n\tconst lastDotIndex = value.lastIndexOf(\".\");\n\tif (lastDotIndex <= 0) return value;\n\n\treturn value.slice(0, lastDotIndex);\n};\n\nconst getSlugifiedFileName = (fileName: string | null | undefined) => {\n\tif (!fileName) return null;\n\n\tconst trimmed = fileName.trim();\n\tif (!trimmed) return null;\n\n\tconst basename = trimmed.split(\"/\").pop()?.split(\"\\\\\").pop() ?? trimmed;\n\tconst slugified = slug(getFileNameWithoutExtension(basename), {\n\t\tlower: true,\n\t});\n\n\treturn slugified || null;\n};\n\nconst getSlugifiedExtension = (extension: string | null | undefined) => {\n\tif (!extension) return null;\n\n\tconst normalized = extension.trim().replace(/^\\.+/, \"\");\n\tif (!normalized) return null;\n\n\treturn (\n\t\tslug(normalized, {\n\t\t\tlower: true,\n\t\t}) || null\n\t);\n};\n\n/**\n * Formats the canonical storage key into the browser-facing path shape that\n * includes a readable filename segment without changing the stored key itself.\n */\nconst formatMediaBrowserKey = (props: {\n\tkey: string;\n\tfileName?: string | null;\n\textension?: string | null;\n}) => {\n\tconst slugifiedFileName = getSlugifiedFileName(props.fileName);\n\tif (!slugifiedFileName) return props.key;\n\tconst slugifiedExtension = getSlugifiedExtension(props.extension);\n\tconst displayFileName = slugifiedExtension\n\t\t? `${slugifiedFileName}.${slugifiedExtension}`\n\t\t: slugifiedFileName;\n\n\tconst parts = props.key.split(\"/\").filter(Boolean);\n\tif (parts.length === 0) return props.key;\n\n\treturn [...parts, displayFileName].join(\"/\");\n};\n\nexport default formatMediaBrowserKey;\n"],"mappings":"oBAEA,MAAM,EAA+B,GAAkB,CACtD,IAAM,EAAe,EAAM,YAAY,GAAG,EAG1C,OAFI,GAAgB,EAAU,EAEvB,EAAM,MAAM,EAAG,CAAY,CACnC,EAEM,EAAwB,GAAwC,CACrE,GAAI,CAAC,EAAU,OAAO,KAEtB,IAAM,EAAU,EAAS,KAAK,EAC9B,GAAI,CAAC,EAAS,OAAO,KAErB,IAAM,EAAW,EAAQ,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,IAAI,GAAK,EAKhE,OAJkB,EAAK,EAA4B,CAAQ,EAAG,CAC7D,MAAO,EACR,CAEe,GAAK,IACrB,EAEM,EAAyB,GAAyC,CACvE,GAAI,CAAC,EAAW,OAAO,KAEvB,IAAM,EAAa,EAAU,KAAK,CAAC,CAAC,QAAQ,OAAQ,EAAE,EAGtD,OAFK,GAGJ,EAAK,EAAY,CAChB,MAAO,EACR,CAAC,GALsB,IAOzB,EAMM,EAAyB,GAIzB,CACL,IAAM,EAAoB,EAAqB,EAAM,QAAQ,EAC7D,GAAI,CAAC,EAAmB,OAAO,EAAM,IACrC,IAAM,EAAqB,EAAsB,EAAM,SAAS,EAC1D,EAAkB,EACrB,GAAG,EAAkB,GAAG,IACxB,EAEG,EAAQ,EAAM,IAAI,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,EAGjD,OAFI,EAAM,SAAW,EAAU,EAAM,IAE9B,CAAC,GAAG,EAAO,CAAe,CAAC,CAAC,KAAK,GAAG,CAC5C"}