Fix image in meta and other details
This commit is contained in:
@ -18,7 +18,7 @@ const load = async function () {
|
||||
|
||||
let _posts;
|
||||
|
||||
export const getPosts = async () => {
|
||||
export const fetchPosts = async () => {
|
||||
_posts = _posts || load();
|
||||
|
||||
return await _posts;
|
@ -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;
|
||||
};
|
||||
|
@ -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;
|
||||
};
|
||||
|
12
src/utils/getProjectRootDir.js
Normal file
12
src/utils/getProjectRootDir.js
Normal file
@ -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, "../../");
|
||||
};
|
@ -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;
|
||||
|
Reference in New Issue
Block a user