Fix minimal details

This commit is contained in:
prototypa
2022-09-04 00:56:24 -04:00
parent 570cf904c4
commit 1cf25d6b43
44 changed files with 757 additions and 641 deletions

22
src/utils/directories.js Normal file
View File

@ -0,0 +1,22 @@
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, "../../");
};
const __srcFolder = path.join(getProjectRootDir(), "/src");
/** */
export const getRelativeUrlByFilePath = (filepath) => {
if (filepath) {
return filepath.replace(__srcFolder, "");
}
return null;
};

View File

@ -1,14 +0,0 @@
const load = async function () {
let images = [];
try {
images = import.meta.glob("~/assets/images/*");
} catch (e) {}
return images;
};
let _images;
export const fetchLocalImages = async () => {
_images = _images || load();
return await _images;
};

View File

@ -1,24 +0,0 @@
import getReadingTime from "reading-time";
export const getNormalizedPost = async (post) => {
const { frontmatter, compiledContent, rawContent, file } = post;
return {
pubDate: frontmatter.pubDate,
draft: frontmatter.draft,
canonical: frontmatter.canonical,
slug: file.split("/").pop().split(".").shift(),
title: frontmatter.title,
description: frontmatter.description,
body: compiledContent(),
image: frontmatter.image,
excerpt: frontmatter.excerpt,
authors: frontmatter.authors,
category: frontmatter.category,
tags: frontmatter.tags,
readingTime: Math.ceil(getReadingTime(rawContent()).minutes),
};
};

View File

@ -1,12 +0,0 @@
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, "../../");
};

View File

@ -1,12 +0,0 @@
import path from "path";
import { getProjectRootDir } from "./getProjectRootDir";
const __srcFolder = path.join(getProjectRootDir(), "/src");
export const getRelativeUrlByFilePath = (filepath) => {
if (filepath) {
return filepath.replace(__srcFolder, "");
}
return null;
};

View File

@ -1,5 +1,20 @@
import { fetchLocalImages } from "~/utils/fetchLocalImages";
const load = async function () {
let images = [];
try {
images = import.meta.glob("~/assets/images/*");
} catch (e) {}
return images;
};
let _images;
/** */
export const fetchLocalImages = async () => {
_images = _images || load();
return await _images;
};
/** */
export const findImage = async (imagePath) => {
if (typeof imagePath !== "string") {
return null;
@ -16,7 +31,5 @@ export const findImage = async (imagePath) => {
const images = await fetchLocalImages();
const key = imagePath.replace("~/", "/src/");
return typeof images[key] === "function"
? (await images[key]())["default"]
: null;
return typeof images[key] === "function" ? (await images[key]())["default"] : null;
};

View File

@ -1,17 +1,17 @@
import slugify from 'limax';
import slugify from "limax";
import { SITE, BLOG } from "~/config.mjs";
const trim = (str, ch) => {
let start = 0, end = str.length;
while(start < end && str[start] === ch)
++start;
while(end > start && str[end - 1] === ch)
--end;
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
let start = 0,
end = str.length;
while (start < end && str[start] === ch) ++start;
while (end > start && str[end - 1] === ch) --end;
return start > 0 || end < str.length ? str.substring(start, end) : str;
};
const trimSlash = (s) => trim(trim(s, "/"));
const createPath = (...params) => "/" + params.filter((el) => !!el).join("/")
const createPath = (...params) => "/" + params.filter((el) => !!el).join("/");
const basePathname = trimSlash(SITE.basePathname);
@ -21,17 +21,19 @@ export const BLOG_BASE = cleanSlug(BLOG.slug);
export const CATEGORY_BASE = cleanSlug(BLOG?.category?.slug);
export const TAG_BASE = cleanSlug(BLOG?.tag?.slug);
/** */
export const getCanonical = (path = "") => new URL(path, SITE.origin);
/** */
export const getPermalink = (slug = "", type = "page") => {
const _slug = cleanSlug(slug);
switch (type) {
case "category":
return createPath(basePathname, CATEGORY_BASE, _slug)
return createPath(basePathname, CATEGORY_BASE, _slug);
case "tag":
return createPath(basePathname, TAG_BASE, _slug)
return createPath(basePathname, TAG_BASE, _slug);
case "post":
return createPath(basePathname, BLOG.postsWithoutBlogSlug ? "" : BLOG_BASE, _slug);
@ -42,8 +44,11 @@ export const getPermalink = (slug = "", type = "page") => {
}
};
/** */
export const getBlogPermalink = () => getPermalink(BLOG_BASE);
/** */
export const getHomePermalink = () => {
const permalink = getPermalink();
return permalink !== "/" ? permalink + "/" : permalink;
}
};

View File

@ -1,4 +1,30 @@
import { getNormalizedPost } from "~/utils/getNormalizedPost";
import getReadingTime from "reading-time";
const getNormalizedPost = async (post) => {
const { frontmatter, compiledContent, rawContent, file } = post;
const ID = file.split("/").pop().split(".").shift();
return {
id: ID,
pubDate: frontmatter.pubDate,
draft: frontmatter.draft,
canonical: frontmatter.canonical,
slug: frontmatter.slug || ID,
title: frontmatter.title,
description: frontmatter.description,
body: compiledContent(),
image: frontmatter.image,
excerpt: frontmatter.excerpt,
authors: frontmatter.authors,
category: frontmatter.category,
tags: frontmatter.tags,
readingTime: Math.ceil(getReadingTime(rawContent()).minutes),
};
};
const load = async function () {
const posts = import.meta.glob("~/data/posts/**/*.md", {
@ -18,12 +44,14 @@ const load = async function () {
let _posts;
/** */
export const fetchPosts = async () => {
_posts = _posts || load();
return await _posts;
};
/** */
export const findPostsByIds = async (ids) => {
if (!Array.isArray(ids)) return [];
@ -31,7 +59,7 @@ export const findPostsByIds = async (ids) => {
return ids.reduce(function (r, id) {
posts.some(function (post) {
return id === post.ID && r.push(post);
return id === post.id && r.push(post);
});
return r;
}, []);

View File

@ -1,3 +1,4 @@
/** */
export const getFormattedDate = (date) =>
date
? new Date(date).toLocaleDateString("en-us", {