added contentParser with type support

This commit is contained in:
somrat sorkar
2023-12-09 11:14:43 +06:00
parent afb57b5825
commit 4c3b181a46
13 changed files with 74 additions and 86 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "astroplate",
"version": "2.3.0",
"version": "2.4.0",
"description": "Astro and Tailwindcss boilerplate",
"author": "zeon.studio",
"license": "MIT",
+2 -5
View File
@@ -2,11 +2,11 @@
import BlogCard from "@/components/BlogCard.astro";
import Share from "@/components/Share.astro";
import Disqus from "@/helpers/Disqus";
import { getSinglePage } from "@/lib/contentParser.astro";
import dateFormat from "@/lib/utils/dateFormat";
import similarItems from "@/lib/utils/similarItems";
import { humanize, markdownify, slugify } from "@/lib/utils/textConverter";
import { Image } from "astro:assets";
import { getCollection } from "astro:content";
import {
FaRegClock,
FaRegFolder,
@@ -16,10 +16,7 @@ import {
const COLLECTION_FOLDER = "blog";
const { post } = Astro.props;
const posts = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const posts = await getSinglePage(COLLECTION_FOLDER);
const similarPosts = similarItems(post, posts);
const { Content } = await post.render();
const { title, description, author, categories, image, date, tags } = post.data;
+16
View File
@@ -0,0 +1,16 @@
---
import {
getCollection,
type CollectionEntry,
type CollectionKey,
} from "astro:content";
export const getSinglePage = async <C extends CollectionKey>(
collectionName: C,
): Promise<CollectionEntry<C>[]> => {
const allPages = await getCollection(collectionName);
const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/));
const removeDrafts = removeIndex.filter((data) => !data.data.draft);
return removeDrafts;
};
---
+4 -12
View File
@@ -1,17 +1,9 @@
---
import { getCollection } from "astro:content";
import { slugify } from "./utils/textConverter";
// get all pages from collection
export const getSinglePage = async (collection: any) => {
const allPage = await getCollection(collection);
const removeIndex = allPage.filter((data: any) => data.id.match(/^(?!-)/));
const removeDrafts = removeIndex.filter((data: any) => !data.data.draft);
return removeDrafts;
};
import { getSinglePage } from "@/lib/contentParser.astro";
import { slugify } from "@/lib/utils/textConverter";
// get taxonomy from frontmatter
export const getTaxonomy = async (collection: string, name: string) => {
export const getTaxonomy = async (collection: any, name: string) => {
const singlePages = await getSinglePage(collection);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
@@ -26,7 +18,7 @@ export const getTaxonomy = async (collection: string, name: string) => {
};
// get all taxonomies from frontmatter
export const getAllTaxonomy = async (collection: string, name: string) => {
export const getAllTaxonomy = async (collection: any, name: string) => {
const singlePages = await getSinglePage(collection);
const taxonomyPages = singlePages.map((page: any) => page.data[name]);
let taxonomies: string[] = [];
+3 -5
View File
@@ -1,15 +1,13 @@
---
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import PageHeader from "@/partials/PageHeader.astro";
import { getCollection } from "astro:content";
// get static paths for all pages
export async function getStaticPaths() {
const COLLECTION_FOLDER = "pages";
const pages = await getCollection(
COLLECTION_FOLDER,
({ data }) => !data.draft,
);
const pages = await getSinglePage(COLLECTION_FOLDER);
const paths = pages.map((page) => ({
params: {
+7 -9
View File
@@ -2,16 +2,14 @@
import BlogCard from "@/components/BlogCard.astro";
import Social from "@/components/Social.astro";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { slugify } from "@/lib/utils/textConverter";
import { Image } from "astro:assets";
import { getCollection } from "astro:content";
// get all static paths for authors
export async function getStaticPaths() {
const COLLECTION_FOLDER = "authors";
const authors = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const authors = await getSinglePage(COLLECTION_FOLDER);
const paths = authors.map((author) => ({
params: {
@@ -25,10 +23,10 @@ export async function getStaticPaths() {
const { author } = Astro.props;
const { title, social, meta_title, description, image } = author.data;
const { Content } = await author.render();
const posts = await getCollection(
"blog",
({ data, slug }) => !data.draft && slug !== "-index",
);
// get all posts by author
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const postFilterByAuthor = posts.filter(
(post) => slugify(post.data.author) === slugify(title),
);
+3 -5
View File
@@ -1,16 +1,14 @@
---
import AuthorCard from "@/components/AuthorCard.astro";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import PageHeader from "@/partials/PageHeader.astro";
import { getCollection, getEntry } from "astro:content";
import { getEntry } from "astro:content";
const COLLECTION_FOLDER = "authors";
const authorIndex = await getEntry(COLLECTION_FOLDER, "-index");
const authors = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const authors = await getSinglePage(COLLECTION_FOLDER);
---
<Base title={authorIndex.data.title}>
+3 -6
View File
@@ -1,14 +1,11 @@
---
import Base from "@/layouts/Base.astro";
import PostSingle from "@/layouts/PostSingle.astro";
import { getCollection } from "astro:content";
import { getSinglePage } from "@/lib/contentParser.astro";
export async function getStaticPaths() {
const COLLECTION_FOLDER = "blog";
const posts = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const paths = posts.map((post) => ({
params: {
+9 -11
View File
@@ -3,22 +3,20 @@ import BlogCard from "@/components/BlogCard.astro";
import Pagination from "@/components/Pagination.astro";
import config from "@/config/config.json";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { getAllTaxonomy, getTaxonomy } from "@/lib/taxonomyParser.astro";
import { sortByDate } from "@/lib/utils/sortFunctions";
import PageHeader from "@/partials/PageHeader.astro";
import PostSidebar from "@/partials/PostSidebar.astro";
import { getCollection, getEntry } from "astro:content";
import { getEntry } from "astro:content";
const COLLECTION_FOLDER = "blog";
const BLOG_FOLDER = "blog";
const postIndex = await getEntry(COLLECTION_FOLDER, "-index");
const posts = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const allCategories = await getAllTaxonomy(COLLECTION_FOLDER, "categories");
const categories = await getTaxonomy(COLLECTION_FOLDER, "categories");
const tags = await getTaxonomy(COLLECTION_FOLDER, "tags");
const postIndex = await getEntry(BLOG_FOLDER, "-index");
const posts = await getSinglePage(BLOG_FOLDER);
const allCategories = await getAllTaxonomy(BLOG_FOLDER, "categories");
const categories = await getTaxonomy(BLOG_FOLDER, "categories");
const tags = await getTaxonomy(BLOG_FOLDER, "tags");
const sortedPosts = sortByDate(posts);
const totalPages: number = Math.ceil(posts.length / config.settings.pagination);
const currentPosts = sortedPosts.slice(0, config.settings.pagination);
@@ -46,7 +44,7 @@ const currentPosts = sortedPosts.slice(0, config.settings.pagination);
}
</div>
<Pagination
section={COLLECTION_FOLDER}
section={BLOG_FOLDER}
currentPage={1}
totalPages={totalPages}
/>
+11 -16
View File
@@ -3,23 +3,21 @@ import BlogCard from "@/components/BlogCard.astro";
import Pagination from "@/components/Pagination.astro";
import config from "@/config/config.json";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { getAllTaxonomy, getTaxonomy } from "@/lib/taxonomyParser.astro";
import { sortByDate } from "@/lib/utils/sortFunctions";
import PageHeader from "@/partials/PageHeader.astro";
import PostSidebar from "@/partials/PostSidebar.astro";
import { getCollection, getEntry } from "astro:content";
import { getEntry } from "astro:content";
const COLLECTION_FOLDER = "blog";
const BLOG_FOLDER = "blog";
const { slug } = Astro.params;
const postIndex = await getEntry(COLLECTION_FOLDER, "-index");
const posts = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const allCategories = await getAllTaxonomy(COLLECTION_FOLDER, "categories");
const categories = await getTaxonomy(COLLECTION_FOLDER, "categories");
const tags = await getTaxonomy(COLLECTION_FOLDER, "tags");
const postIndex = await getEntry(BLOG_FOLDER, "-index");
const posts = await getSinglePage(BLOG_FOLDER);
const allCategories = await getAllTaxonomy(BLOG_FOLDER, "categories");
const categories = await getTaxonomy(BLOG_FOLDER, "categories");
const tags = await getTaxonomy(BLOG_FOLDER, "tags");
const sortedPosts = sortByDate(posts);
const totalPages = Math.ceil(posts.length / config.settings.pagination);
const currentPage = slug && !isNaN(Number(slug)) ? Number(slug) : 1;
@@ -28,11 +26,8 @@ const indexOfFirstPost = indexOfLastPost - config.settings.pagination;
const currentPosts = sortedPosts.slice(indexOfFirstPost, indexOfLastPost);
export async function getStaticPaths() {
const COLLECTION_FOLDER = "blog";
const posts = await getCollection(
COLLECTION_FOLDER,
({ data, slug }) => !data.draft && slug !== "-index",
);
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const totalPages = Math.ceil(posts.length / config.settings.pagination);
const paths = [];
@@ -69,7 +64,7 @@ export async function getStaticPaths() {
}
</div>
<Pagination
section={COLLECTION_FOLDER}
section={BLOG_FOLDER}
currentPage={currentPage}
totalPages={totalPages}
/>
+6 -5
View File
@@ -1,11 +1,12 @@
---
import BlogCard from "@/components/BlogCard.astro";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { getTaxonomy } from "@/lib/taxonomyParser.astro";
import taxonomyFilter from "@/lib/utils/taxonomyFilter";
import PageHeader from "@/partials/PageHeader.astro";
import { getCollection } from "astro:content";
// get static paths for all categories
export async function getStaticPaths() {
const BLOG_FOLDER = "blog";
const categories = await getTaxonomy(BLOG_FOLDER, "categories");
@@ -18,10 +19,10 @@ export async function getStaticPaths() {
}
const { category } = Astro.params;
const posts = await getCollection(
"blog",
({ data, slug }) => !data.draft && slug !== "-index",
);
// get posts by category
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const filterByCategories = taxonomyFilter(posts, "categories", category!);
---
+3 -5
View File
@@ -1,12 +1,10 @@
---
import Base from "@/layouts/Base.astro";
import SearchLayout from "@/layouts/Search";
import { getCollection } from "astro:content";
import { getSinglePage } from "@/lib/contentParser.astro";
const posts = await getCollection(
"blog",
({ data, slug }) => !data.draft && slug !== "-index",
);
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
// List of items to search in
const searchList = posts.map((item) => ({
+6 -6
View File
@@ -1,10 +1,10 @@
---
import BlogCard from "@/components/BlogCard.astro";
import Base from "@/layouts/Base.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { getTaxonomy } from "@/lib/taxonomyParser.astro";
import taxonomyFilter from "@/lib/utils/taxonomyFilter";
import PageHeader from "@/partials/PageHeader.astro";
import { getCollection } from "astro:content";
export async function getStaticPaths() {
const BLOG_FOLDER = "blog";
@@ -18,11 +18,11 @@ export async function getStaticPaths() {
}
const { tag } = Astro.params;
const posts = await getCollection(
"blog",
({ data, slug }) => !data.draft && slug !== "-index",
);
const filterByTags = taxonomyFilter(posts, "categories", tag!);
// get posts by tag
const BLOG_FOLDER = "blog";
const posts = await getSinglePage(BLOG_FOLDER);
const filterByTags = taxonomyFilter(posts, "tags", tag!);
---
<Base title={tag}>