diff --git a/package.json b/package.json index 33a6490..310f9bd 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "astroplate", - "version": "2.3.0", + "version": "2.4.0", "description": "Astro and Tailwindcss boilerplate", "author": "zeon.studio", "license": "MIT", diff --git a/src/layouts/PostSingle.astro b/src/layouts/PostSingle.astro index a585763..79b4cb4 100755 --- a/src/layouts/PostSingle.astro +++ b/src/layouts/PostSingle.astro @@ -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; diff --git a/src/lib/contentParser.astro b/src/lib/contentParser.astro new file mode 100644 index 0000000..a6d1013 --- /dev/null +++ b/src/lib/contentParser.astro @@ -0,0 +1,16 @@ +--- +import { + getCollection, + type CollectionEntry, + type CollectionKey, +} from "astro:content"; + +export const getSinglePage = async ( + collectionName: C, +): Promise[]> => { + const allPages = await getCollection(collectionName); + const removeIndex = allPages.filter((data) => data.id.match(/^(?!-)/)); + const removeDrafts = removeIndex.filter((data) => !data.data.draft); + return removeDrafts; +}; +--- diff --git a/src/lib/taxonomyParser.astro b/src/lib/taxonomyParser.astro index aa86862..56d50db 100644 --- a/src/lib/taxonomyParser.astro +++ b/src/lib/taxonomyParser.astro @@ -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[] = []; diff --git a/src/pages/[regular].astro b/src/pages/[regular].astro index b290452..9990e6a 100755 --- a/src/pages/[regular].astro +++ b/src/pages/[regular].astro @@ -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: { diff --git a/src/pages/authors/[single].astro b/src/pages/authors/[single].astro index 8a40a24..a61d766 100755 --- a/src/pages/authors/[single].astro +++ b/src/pages/authors/[single].astro @@ -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), ); diff --git a/src/pages/authors/index.astro b/src/pages/authors/index.astro index dcff77c..8df557f 100755 --- a/src/pages/authors/index.astro +++ b/src/pages/authors/index.astro @@ -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); --- diff --git a/src/pages/blog/[single].astro b/src/pages/blog/[single].astro index da762f0..984477b 100755 --- a/src/pages/blog/[single].astro +++ b/src/pages/blog/[single].astro @@ -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: { diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro index 1fd2058..4a29c30 100755 --- a/src/pages/blog/index.astro +++ b/src/pages/blog/index.astro @@ -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); } diff --git a/src/pages/blog/page/[slug].astro b/src/pages/blog/page/[slug].astro index a1385ff..26b7e1e 100755 --- a/src/pages/blog/page/[slug].astro +++ b/src/pages/blog/page/[slug].astro @@ -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() { } diff --git a/src/pages/categories/[category].astro b/src/pages/categories/[category].astro index 4df39c7..ca94f69 100755 --- a/src/pages/categories/[category].astro +++ b/src/pages/categories/[category].astro @@ -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!); --- diff --git a/src/pages/search.astro b/src/pages/search.astro index 1ef7bb9..0c15b9a 100755 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -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) => ({ diff --git a/src/pages/tags/[tag].astro b/src/pages/tags/[tag].astro index 62d74e7..765fd33 100755 --- a/src/pages/tags/[tag].astro +++ b/src/pages/tags/[tag].astro @@ -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!); ---