Use Prettier
This commit is contained in:
@ -1,22 +1,22 @@
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
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;
|
||||
const mode = import.meta.env.MODE;
|
||||
|
||||
return mode === "production" ? path.join(__dirname, "../") : path.join(__dirname, "../../");
|
||||
return mode === 'production' ? path.join(__dirname, '../') : path.join(__dirname, '../../');
|
||||
};
|
||||
|
||||
const __srcFolder = path.join(getProjectRootDir(), "/src");
|
||||
const __srcFolder = path.join(getProjectRootDir(), '/src');
|
||||
|
||||
/** */
|
||||
export const getRelativeUrlByFilePath = (filepath) => {
|
||||
if (filepath) {
|
||||
return filepath.replace(__srcFolder, "");
|
||||
}
|
||||
if (filepath) {
|
||||
return filepath.replace(__srcFolder, '');
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
};
|
||||
|
@ -1,37 +1,37 @@
|
||||
const load = async function () {
|
||||
let images = [];
|
||||
try {
|
||||
images = import.meta.glob("~/assets/images/**");
|
||||
} catch (e) {
|
||||
// continue regardless of error
|
||||
}
|
||||
return images;
|
||||
let images = [];
|
||||
try {
|
||||
images = import.meta.glob('~/assets/images/**');
|
||||
} catch (e) {
|
||||
// continue regardless of error
|
||||
}
|
||||
return images;
|
||||
};
|
||||
|
||||
let _images;
|
||||
|
||||
/** */
|
||||
export const fetchLocalImages = async () => {
|
||||
_images = _images || load();
|
||||
return await _images;
|
||||
_images = _images || load();
|
||||
return await _images;
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findImage = async (imagePath) => {
|
||||
if (typeof imagePath !== "string") {
|
||||
return null;
|
||||
}
|
||||
if (typeof imagePath !== 'string') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (imagePath.startsWith("http://") || imagePath.startsWith("https://")) {
|
||||
return imagePath;
|
||||
}
|
||||
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)
|
||||
if (!imagePath.startsWith('~/assets')) {
|
||||
return null;
|
||||
} // For now only consume images using ~/assets alias (or absolute)
|
||||
|
||||
const images = await fetchLocalImages();
|
||||
const key = imagePath.replace("~/", "/src/");
|
||||
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;
|
||||
};
|
||||
|
@ -1,17 +1,17 @@
|
||||
import slugify from "limax";
|
||||
import slugify from 'limax';
|
||||
|
||||
import { SITE, BLOG } from "~/config.mjs";
|
||||
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 trimSlash = (s) => trim(trim(s, '/'));
|
||||
const createPath = (...params) => '/' + params.filter((el) => !!el).join('/');
|
||||
|
||||
const basePathname = trimSlash(SITE.basePathname);
|
||||
|
||||
@ -22,26 +22,26 @@ 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 getCanonical = (path = '') => new URL(path, SITE.origin);
|
||||
|
||||
/** */
|
||||
export const getPermalink = (slug = "", type = "page") => {
|
||||
const _slug = cleanSlug(slug);
|
||||
export const getPermalink = (slug = '', type = 'page') => {
|
||||
const _slug = cleanSlug(slug);
|
||||
|
||||
switch (type) {
|
||||
case "category":
|
||||
return createPath(basePathname, CATEGORY_BASE, _slug);
|
||||
switch (type) {
|
||||
case 'category':
|
||||
return createPath(basePathname, CATEGORY_BASE, _slug);
|
||||
|
||||
case "tag":
|
||||
return createPath(basePathname, TAG_BASE, _slug);
|
||||
case 'tag':
|
||||
return createPath(basePathname, TAG_BASE, _slug);
|
||||
|
||||
case "post":
|
||||
return createPath(basePathname, BLOG.postsWithoutBlogSlug ? "" : BLOG_BASE, _slug);
|
||||
case 'post':
|
||||
return createPath(basePathname, BLOG.postsWithoutBlogSlug ? '' : BLOG_BASE, _slug);
|
||||
|
||||
case "page":
|
||||
default:
|
||||
return createPath(basePathname, _slug);
|
||||
}
|
||||
case 'page':
|
||||
default:
|
||||
return createPath(basePathname, _slug);
|
||||
}
|
||||
};
|
||||
|
||||
/** */
|
||||
@ -49,6 +49,6 @@ export const getBlogPermalink = () => getPermalink(BLOG_BASE);
|
||||
|
||||
/** */
|
||||
export const getHomePermalink = () => {
|
||||
const permalink = getPermalink();
|
||||
return permalink !== "/" ? permalink + "/" : permalink;
|
||||
const permalink = getPermalink();
|
||||
return permalink !== '/' ? permalink + '/' : permalink;
|
||||
};
|
||||
|
@ -1,66 +1,66 @@
|
||||
import getReadingTime from "reading-time";
|
||||
import getReadingTime from 'reading-time';
|
||||
|
||||
const getNormalizedPost = async (post) => {
|
||||
const { frontmatter, compiledContent, rawContent, file } = post;
|
||||
const ID = file.split("/").pop().split(".").shift();
|
||||
const { frontmatter, compiledContent, rawContent, file } = post;
|
||||
const ID = file.split('/').pop().split('.').shift();
|
||||
|
||||
return {
|
||||
id: ID,
|
||||
return {
|
||||
id: ID,
|
||||
|
||||
pubDate: frontmatter.pubDate,
|
||||
draft: frontmatter.draft,
|
||||
pubDate: frontmatter.pubDate,
|
||||
draft: frontmatter.draft,
|
||||
|
||||
canonical: frontmatter.canonical,
|
||||
slug: frontmatter.slug || ID,
|
||||
canonical: frontmatter.canonical,
|
||||
slug: frontmatter.slug || ID,
|
||||
|
||||
title: frontmatter.title,
|
||||
description: frontmatter.description,
|
||||
body: compiledContent(),
|
||||
image: frontmatter.image,
|
||||
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),
|
||||
};
|
||||
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", {
|
||||
eager: true,
|
||||
});
|
||||
const posts = import.meta.glob('~/data/posts/**/*.md', {
|
||||
eager: true,
|
||||
});
|
||||
|
||||
const normalizedPosts = Object.keys(posts).map(async (key) => {
|
||||
const post = await posts[key];
|
||||
return await getNormalizedPost(post);
|
||||
});
|
||||
const normalizedPosts = Object.keys(posts).map(async (key) => {
|
||||
const post = await posts[key];
|
||||
return await getNormalizedPost(post);
|
||||
});
|
||||
|
||||
const results = (await Promise.all(normalizedPosts))
|
||||
.sort((a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf())
|
||||
.filter((post) => !post.draft);
|
||||
return results;
|
||||
const results = (await Promise.all(normalizedPosts))
|
||||
.sort((a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf())
|
||||
.filter((post) => !post.draft);
|
||||
return results;
|
||||
};
|
||||
|
||||
let _posts;
|
||||
|
||||
/** */
|
||||
export const fetchPosts = async () => {
|
||||
_posts = _posts || load();
|
||||
_posts = _posts || load();
|
||||
|
||||
return await _posts;
|
||||
return await _posts;
|
||||
};
|
||||
|
||||
/** */
|
||||
export const findPostsByIds = async (ids) => {
|
||||
if (!Array.isArray(ids)) return [];
|
||||
if (!Array.isArray(ids)) return [];
|
||||
|
||||
const posts = await fetchPosts();
|
||||
const posts = await fetchPosts();
|
||||
|
||||
return ids.reduce(function (r, id) {
|
||||
posts.some(function (post) {
|
||||
return id === post.id && r.push(post);
|
||||
});
|
||||
return r;
|
||||
}, []);
|
||||
return ids.reduce(function (r, id) {
|
||||
posts.some(function (post) {
|
||||
return id === post.id && r.push(post);
|
||||
});
|
||||
return r;
|
||||
}, []);
|
||||
};
|
||||
|
@ -1,9 +1,9 @@
|
||||
/** */
|
||||
export const getFormattedDate = (date) =>
|
||||
date
|
||||
? new Date(date).toLocaleDateString("en-us", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
})
|
||||
: "";
|
||||
date
|
||||
? new Date(date).toLocaleDateString('en-us', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
})
|
||||
: '';
|
||||
|
Reference in New Issue
Block a user