strapi utils
This commit is contained in:
@@ -3,22 +3,12 @@
|
|||||||
import Layout from '../layouts/Layout.astro';
|
import Layout from '../layouts/Layout.astro';
|
||||||
import { getCollection } from 'astro:content';
|
import { getCollection } from 'astro:content';
|
||||||
import MarkdownComponent from "../components/MardownContent.astro";
|
import MarkdownComponent from "../components/MardownContent.astro";
|
||||||
|
import { getStrapiMedia } from '../utils/strapi';
|
||||||
|
|
||||||
// Fetch all posts from Strapi using Astro's content collection
|
// Fetch all posts from Strapi using Astro's content collection
|
||||||
const strapiPoleElements = await getCollection("strapiPoleElementsLoader");
|
const strapiPoleElements = await getCollection("strapiPoleElementsLoader");
|
||||||
// Get Strapi URL from environment variables with fallback to localhost
|
// Get Strapi URL from environment variables with fallback to localhost
|
||||||
const BASE_URL = await import.meta.env.STRAPI_URL || "http://localhost:1337";
|
const BASE_URL = await import.meta.env.STRAPI_URL || "http://localhost:1337";
|
||||||
|
|
||||||
// Helper function to handle media URLs from Strapi
|
|
||||||
function getStrapiMedia(url: string | null) {
|
|
||||||
if (url == null) return null;
|
|
||||||
// Return as-is if it's a data URL (base64)
|
|
||||||
if (url.startsWith("data:")) return url;
|
|
||||||
// Return as-is if it's an absolute URL
|
|
||||||
if (url.startsWith("http") || url.startsWith("//")) return url;
|
|
||||||
// Prepend BASE_URL for relative URLs
|
|
||||||
return `${BASE_URL}${url}`;
|
|
||||||
}
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
@@ -28,20 +18,20 @@ function getStrapiMedia(url: string | null) {
|
|||||||
<!-- Responsive grid layout using Tailwind CSS -->
|
<!-- Responsive grid layout using Tailwind CSS -->
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
||||||
{/* Map through posts and create article cards */}
|
{/* Map through posts and create article cards */}
|
||||||
{strapiPoleElements.map((post) => (
|
{strapiPoleElements.map((poleElement) => (
|
||||||
<article class="bg-white rounded-lg shadow-lg overflow-hidden">
|
<article class="bg-white rounded-lg shadow-lg overflow-hidden">
|
||||||
{/* Post cover image */}
|
{/* Post cover image */}
|
||||||
<img
|
<img
|
||||||
src={getStrapiMedia(post.data.mainImage.url)}
|
src={getStrapiMedia(poleElement.data.mainImage.url, BASE_URL)}
|
||||||
alt={post.data.mainImage.alternativeText}
|
alt={poleElement.data.mainImage.alternativeText}
|
||||||
class="w-full h-48 object-cover"
|
class="w-full h-48 object-cover"
|
||||||
/>
|
/>
|
||||||
{/* Post content container */}
|
{/* Post content container */}
|
||||||
<div class="p-4">
|
<div class="p-4">
|
||||||
<h2 class="text-xl font-bold mb-2">{post.data.name}</h2>
|
<h2 class="text-xl font-bold mb-2">{poleElement.data.name}</h2>
|
||||||
<MarkdownComponent content={post.data.description} class="text-gray-600 mb-4"/>
|
<MarkdownComponent content={poleElement.data.description} class="text-gray-600 mb-4"/>
|
||||||
<div class="text-sm text-gray-500">
|
<div class="text-sm text-gray-500">
|
||||||
Published: {new Date(post.data.publishedAt).toLocaleDateString()}
|
Published: {new Date(poleElement.data.publishedAt).toLocaleDateString()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|||||||
10
client/src/utils/strapi.ts
Normal file
10
client/src/utils/strapi.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// Helper function to handle media URLs from Strapi
|
||||||
|
export function getStrapiMedia(url: string | null, baseUrl: string) {
|
||||||
|
if (url == null) return null;
|
||||||
|
// Return as-is if it's a data URL (base64)
|
||||||
|
if (url.startsWith("data:")) return url;
|
||||||
|
// Return as-is if it's an absolute URL
|
||||||
|
if (url.startsWith("http") || url.startsWith("//")) return url;
|
||||||
|
// Prepend baseUrl for relative URLs
|
||||||
|
return `${baseUrl}${url}`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user