diff --git a/astro.config.mjs b/astro.config.mjs index 91277fd..30720d0 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,4 +1,6 @@ -import * as url from "url"; +import path from "path"; +import { fileURLToPath } from "url"; + import { defineConfig } from "astro/config"; import tailwind from "@astrojs/tailwind"; @@ -7,7 +9,7 @@ import image from "@astrojs/image"; import { SITE } from "./src/config.mjs"; -const __dirname = url.fileURLToPath(new URL(".", import.meta.url)); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); // https://astro.build/config export default defineConfig({ @@ -30,7 +32,7 @@ export default defineConfig({ vite: { resolve: { alias: { - "~/": `${__dirname}src/`, + "~": path.resolve(__dirname, "./src"), }, }, }, diff --git a/src/components/core/MetaTags.astro b/src/components/core/MetaTags.astro index 59dc897..04a92d5 100644 --- a/src/components/core/MetaTags.astro +++ b/src/components/core/MetaTags.astro @@ -18,7 +18,7 @@ const { const image = typeof _image === "string" ? new URL(_image, Astro.site) - : typeof _image["src"] !== "undefined" + : _image && typeof _image["src"] !== "undefined" ? new URL(getRelativeUrlByFilePath(_image.src), Astro.site) : null; --- diff --git a/src/components/core/Picture.astro b/src/components/core/Picture.astro index 6969d27..accdc5d 100644 --- a/src/components/core/Picture.astro +++ b/src/components/core/Picture.astro @@ -16,7 +16,7 @@ const { let picture = null; try { - picture = await getPicture({ + picture = src && await getPicture({ src, widths, formats, @@ -24,7 +24,6 @@ try { }) } catch (e) { - console.log(e); } const { image = {}, sources = [] } = picture || {} diff --git a/src/components/widgets/BlogPost.astro b/src/components/widgets/BlogPost.astro index 2823bd0..d857db8 100644 --- a/src/components/widgets/BlogPost.astro +++ b/src/components/widgets/BlogPost.astro @@ -24,14 +24,18 @@ const { post } = Astro.props; {post.title} - + { + post.image && ( + + ) + }
@@ -49,8 +48,7 @@ const {} = Astro.props; @@ -72,8 +70,7 @@ const {} = Astro.props; @@ -95,8 +92,7 @@ const {} = Astro.props; diff --git a/src/pages/blog/[...page].astro b/src/pages/blog/[...page].astro index 0052bb9..53f5a2b 100644 --- a/src/pages/blog/[...page].astro +++ b/src/pages/blog/[...page].astro @@ -2,12 +2,12 @@ import Layout from "~/layouts/PageLayout.astro"; import { SITE } from "~/config.mjs"; -import { getPosts } from "~/utils/getPosts"; +import { fetchPosts } from "~/utils/fetchPosts"; import BlogList from "~/components/widgets/BlogList.astro"; export async function getStaticPaths({ paginate }) { - const posts = await getPosts(); + const posts = await fetchPosts(); return paginate(posts, { pageSize: SITE.postsPerPage, diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index 1310335..dc11ad1 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -1,13 +1,13 @@ --- import { SITE } from "~/config.mjs"; -import { getPosts } from "~/utils/getPosts"; +import { fetchPosts } from "~/utils/fetchPosts"; import { findImage } from "~/utils/findImage"; import Layout from "~/layouts/PageLayout.astro"; import BlogPost from "~/components/widgets/BlogPost.astro"; export async function getStaticPaths() { - const posts = await getPosts(); + const posts = await fetchPosts(); return posts.map((post) => ({ params: { slug: post.slug }, diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js index 02d4f83..b7089a9 100644 --- a/src/pages/rss.xml.js +++ b/src/pages/rss.xml.js @@ -1,9 +1,9 @@ import rss from "@astrojs/rss"; import { SITE } from "~/config.mjs"; -import { getPosts } from "~/utils/getPosts"; +import { fetchPosts } from "~/utils/fetchPosts"; -const posts = await getPosts(); +const posts = await fetchPosts(); export const get = () => rss({ diff --git a/src/utils/getPosts.js b/src/utils/fetchPosts.js similarity index 93% rename from src/utils/getPosts.js rename to src/utils/fetchPosts.js index 838aa51..17a142a 100644 --- a/src/utils/getPosts.js +++ b/src/utils/fetchPosts.js @@ -18,7 +18,7 @@ const load = async function () { let _posts; -export const getPosts = async () => { +export const fetchPosts = async () => { _posts = _posts || load(); return await _posts; diff --git a/src/utils/findImage.js b/src/utils/findImage.js index f05c590..73fa47e 100644 --- a/src/utils/findImage.js +++ b/src/utils/findImage.js @@ -1,19 +1,22 @@ import { getAllImages } from "~/utils/getAllImages"; -export const findImage = async (imageRoute) => { +export const findImage = async (imagePath) => { + if (typeof imagePath !== "string") { + return null; + } + + if (imagePath.startsWith("http://") || imagePath.startsWith("https://")) { + return imagePath; + } + + if (!imagePath.startsWith("~/assets")) { + return null; + } // For now only consume images using ~/assets alias (or absolute) + const images = await getAllImages(); + const key = imagePath.replace("~/", "/src/"); - const key = imageRoute.replace("~/", "/src/"); - - const image = - typeof imageRoute === "string" && - (imageRoute.startsWith("/") || - imageRoute.startsWith("http://") || - imageRoute.startsWith("https://")) - ? imageRoute - : typeof images[key] === "function" - ? (await images[key]())["default"] - : null; - - return image; + return typeof images[key] === "function" + ? (await images[key]())["default"] + : null; }; diff --git a/src/utils/getAllImages.js b/src/utils/getAllImages.js index 6654ae8..f93be65 100644 --- a/src/utils/getAllImages.js +++ b/src/utils/getAllImages.js @@ -1,6 +1,8 @@ const load = async function () { - const images = import.meta.glob("~/assets/images/*"); - + let images = []; + try { + images = import.meta.glob("~/assets/images/*"); + } catch (e) {} return images; }; @@ -8,6 +10,5 @@ let _images; export const getAllImages = async () => { _images = _images || load(); - return await _images; }; diff --git a/src/utils/getProjectRootDir.js b/src/utils/getProjectRootDir.js new file mode 100644 index 0000000..ac367f2 --- /dev/null +++ b/src/utils/getProjectRootDir.js @@ -0,0 +1,12 @@ +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +export const getProjectRootDir = () => { + const mode = import.meta.env.MODE; + + return mode === "production" + ? path.join(__dirname, "../") + : path.join(__dirname, "../../"); +}; diff --git a/src/utils/getRelativeUrlByFilePath.js b/src/utils/getRelativeUrlByFilePath.js index 49b73c9..44fe755 100644 --- a/src/utils/getRelativeUrlByFilePath.js +++ b/src/utils/getRelativeUrlByFilePath.js @@ -1,10 +1,11 @@ -import * as url from "url"; +import path from "path"; +import { getProjectRootDir } from "./getProjectRootDir"; -const __src = url.fileURLToPath(new URL("../", import.meta.url)); +const __srcFolder = path.join(getProjectRootDir(), "/src"); export const getRelativeUrlByFilePath = (filepath) => { if (filepath) { - return "/" + filepath.substring(__src.length); + return filepath.replace(__srcFolder, ""); } return null;