Add Categories and Tags with new configs

This commit is contained in:
prototypa
2022-08-30 11:37:19 -04:00
parent 4859dcee87
commit 49c6c5611b
26 changed files with 434 additions and 217 deletions

View File

@ -10,9 +10,9 @@ const load = async function () {
return await getNormalizedPost(post);
});
const results = (await Promise.all(normalizedPosts)).sort(
(a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf()
);
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;
};
@ -23,3 +23,16 @@ export const fetchPosts = async () => {
return await _posts;
};
export const findPostsByIds = async (ids) => {
if (!Array.isArray(ids)) return [];
const posts = await fetchPosts();
return ids.reduce(function (r, a) {
posts.some(function (el) {
return a === el.slug && r.push(el);
});
return r;
}, []);
};

View File

@ -5,6 +5,7 @@ export const getNormalizedPost = async (post) => {
return {
pubDate: frontmatter.pubDate,
draft: frontmatter.draft,
canonical: frontmatter.canonical,
slug: file.split("/").pop().split(".").shift(),

47
src/utils/permalinks.js Normal file
View File

@ -0,0 +1,47 @@
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;
}
const trimSlash = (s) => trim(s, "/");
const createPath = (...params) => "/" + params.filter((el) => !!el).join("/")
const baseUrl = trimSlash(SITE.baseUrl);
const blogBaseUrl = trimSlash(BLOG.slug);
const categoryBaseUrl = trim(BLOG?.category?.slug);
const tagBaseUrl = trim(BLOG?.tag?.slug);
const cleanSlug = (slug) => trimSlash(slug);
export const getCanonical = (path = "") => new URL(path, SITE.domain);
export const getPermalink = (slug = "", type = "page") => {
const _slug = cleanSlug(slug);
switch (type) {
case "category":
return createPath(baseUrl, categoryBaseUrl, _slug)
case "tag":
return createPath(baseUrl, tagBaseUrl, _slug)
case "post":
return createPath(baseUrl, BLOG.postsWithoutBlogSlug ? "" : blogBaseUrl, _slug);
case "page":
default:
return createPath(baseUrl, _slug);
}
};
export const getBlogPermalink = () => getPermalink(blogBaseUrl);
export const getHomePermalink = () => {
const permalink = getPermalink();
return permalink !== "/" ? permalink + "/" : permalink;
}