Skip to content

Commit

Permalink
Break out utils file
Browse files Browse the repository at this point in the history
Each function in its own file as a default export
  • Loading branch information
tylermercer committed Jan 31, 2024
1 parent 2d4a4cb commit 0d2ebd6
Show file tree
Hide file tree
Showing 27 changed files with 156 additions and 128 deletions.
3 changes: 2 additions & 1 deletion src/components/PostCard.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
import type { CollectionEntry } from "astro:content";
import { getPostDate, formatPostDate } from "../utils/utils";
import renderInlineMarkdown from "../utils/renderInlineMarkdown";
import formatPostDate from "../utils/formatPostDate";
import getPostDate from "../utils/getPostDate";
export type Props = {
post: CollectionEntry<"posts">;
Expand Down
2 changes: 1 addition & 1 deletion src/components/SiteNavHeader.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { trimTrailingSlash } from "../utils/utils";
import trimTrailingSlash from "../utils/trimTrailingSlash";
import Logo from "./Logo.astro";
const isCurrent = (path: string) =>
Expand Down
11 changes: 4 additions & 7 deletions src/layouts/Post.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { type CollectionEntry } from "astro:content";
import Layout from "./Base.astro";
import MungedEmail from "../components/MungedEmail.astro";
import ShareLinks from "../components/ShareLinks.astro";
import {
countWords,
formatPostDate,
getPostDate,
type WithNextAndPrev,
} from "../utils/utils";
import NextPrevLinks from "../components/NextPrevLinks.astro";
import renderInlineMarkdown from "../utils/renderInlineMarkdown";
import PostDescription from "../components/PostDescription.astro";
import type { WithNextAndPrev } from "../utils/toWithNextAndPrev";
import countWords from "../utils/countWords";
import formatPostDate from "../utils/formatPostDate";
import getPostDate from "../utils/getPostDate";
export type Props = {
post: WithNextAndPrev<CollectionEntry<"posts">>;
Expand Down
7 changes: 6 additions & 1 deletion src/pages/feeds/feed.json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import metadata from '../../content/_metadata';
import { getCollection } from 'astro:content';
import { filterOutDraftsIfProduction, formatDateIso, getPostDate, sortByDate, renderMarkdown, getCategory } from '../../utils/utils';
import formatDateIso from '../../utils/formatDateIso';
import getPostDate from "../../utils/getPostDate";
import sortByDate from "../../utils/sortByDate";
import filterOutDraftsIfProduction from "../../utils/filterOutDraftsIfProduction";
import getCategory from "../../utils/getCategory";
import renderMarkdown from "../../utils/renderMarkdown";

export async function GET(context) {
const blog = sortByDate(filterOutDraftsIfProduction(await getCollection('posts')));
Expand Down
6 changes: 5 additions & 1 deletion src/pages/feeds/feed.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import rss from '@astrojs/rss';

import metadata from '../../content/_metadata';
import { getCollection } from 'astro:content';
import { filterOutDraftsIfProduction, formatDateIso, getPostDate, renderMarkdown, sortByDate } from '../../utils/utils';
import formatDateIso from '../../utils/formatDateIso';
import getPostDate from "../../utils/getPostDate";
import sortByDate from "../../utils/sortByDate";
import filterOutDraftsIfProduction from "../../utils/filterOutDraftsIfProduction";
import renderMarkdown from "../../utils/renderMarkdown";

export async function GET(context) {
const blog = sortByDate(filterOutDraftsIfProduction(await getCollection('posts')));
Expand Down
5 changes: 4 additions & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
import { getCollection } from "astro:content";
import Layout from "../layouts/Home.astro";
import { filterOutDraftsIfProduction, firstFive, labelDrafts, sortByDate } from "../utils/utils";
import metadata from "../content/_metadata";
import filterOutDraftsIfProduction from "../utils/filterOutDraftsIfProduction";
import firstFive from "../utils/firstFive";
import labelDrafts from "../utils/labelDrafts";
import sortByDate from "../utils/sortByDate";
const { title, description } = metadata;
Expand Down
5 changes: 4 additions & 1 deletion src/pages/og-images/[...collectionAndSlug].png.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { getCollection, getEntryBySlug, type ContentCollectionKey, type CollectionEntry } from "astro:content";
import type { APIRoute } from "astro";

import { labelDrafts, sortByDate, filterOutDraftsIfProduction, getCategory } from "../../utils/utils";
import sortByDate from "../../utils/sortByDate";
import filterOutDraftsIfProduction from "../../utils/filterOutDraftsIfProduction";
import labelDrafts from "../../utils/labelDrafts";
import getCategory from "../../utils/getCategory";
import { renderImage } from "./_renderImage";

export async function getStaticPaths() {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/og-images/_renderImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sharp from "sharp";
import styles from './_styles.css?raw';
import faustinaRaw from '@fontsource/faustina/files/faustina-latin-400-normal.woff';
import figtreeRaw from '@fontsource/figtree/files/figtree-latin-700-normal.woff';
import { formatPostDate } from "../../utils/utils";
import formatPostDate from "../../utils/formatPostDate";

// They already are Buffers because of the custom rawFonts Vite plugin in astro.config.js, but TS doesn't know that
const figtree = figtreeRaw as unknown as Buffer;
Expand Down
13 changes: 5 additions & 8 deletions src/pages/posts/[...post].astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
---
import Layout from "../../layouts/Post.astro";
import { type CollectionEntry, getCollection } from "astro:content";
import {
filterOutDraftsIfProduction,
getCategory,
labelDrafts,
sortByDate,
toWithNextAndPrev,
type WithNextAndPrev,
} from "../../utils/utils";
import filterOutDraftsIfProduction from "../../utils/filterOutDraftsIfProduction";
import getCategory from "../../utils/getCategory";
import labelDrafts from "../../utils/labelDrafts";
import sortByDate from "../../utils/sortByDate";
import toWithNextAndPrev, { type WithNextAndPrev } from "../../utils/toWithNextAndPrev";
export async function getStaticPaths() {
const posts = await getCollection("posts");
Expand Down
6 changes: 5 additions & 1 deletion src/pages/posts/[category]/[page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import type { GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
import Layout from "../../../layouts/Category.astro";
import { checkCategory, filterOutDraftsIfProduction, getCategory, labelDrafts, sortByDate } from "../../../utils/utils";
import checkCategory from "../../../utils/checkCategory";
import filterOutDraftsIfProduction from "../../../utils/filterOutDraftsIfProduction";
import getCategory from "../../../utils/getCategory";
import labelDrafts from "../../../utils/labelDrafts";
import sortByDate from "../../../utils/sortByDate";
export type Props = {
page: {
Expand Down
4 changes: 3 additions & 1 deletion src/pages/posts/[page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import type { GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
import Layout from "../../layouts/Category.astro";
import { filterOutDraftsIfProduction, labelDrafts, sortByDate } from "../../utils/utils";
import category from '../../content/_allPostsCategory';
import filterOutDraftsIfProduction from "../../utils/filterOutDraftsIfProduction";
import labelDrafts from "../../utils/labelDrafts";
import sortByDate from "../../utils/sortByDate";
export type Props = {
page: {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/checkCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type CollectionEntry } from "astro:content";

export default function checkCategory(entry: CollectionEntry<'posts'>, knownCategorySlug: string) {
return entry.id.split('/').at(0) === knownCategorySlug;
}
6 changes: 6 additions & 0 deletions src/utils/countWords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Adapted from Nunjuck's wordcount filter implementation

export default function countWords(str: string): number {
const words = (str) ? str.match(/\w+/g) : null;
return (words) ? words.length : 0;
}
10 changes: 10 additions & 0 deletions src/utils/filterOutDraftsIfProduction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type CollectionEntry } from "astro:content";
import isDraft from "./isDraft";

export default function filterOutDraftsIfProduction(entries: CollectionEntry<'posts'>[]): CollectionEntry<'posts'>[] {
const isProduction = (import.meta.env.MODE === 'production');
if (!isProduction) return entries;
return entries.filter(
(e: CollectionEntry<'posts'>) => !isDraft(e)
);
}
4 changes: 4 additions & 0 deletions src/utils/firstFive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export default function firstFive(collection: any[]) {
return collection.slice(0, 5);
}
4 changes: 4 additions & 0 deletions src/utils/formatDateIso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export default function formatDateIso(date: Date) {
return date.toISOString();
}
5 changes: 5 additions & 0 deletions src/utils/formatPostDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DateTime } from "luxon";

export default function formatPostDate(date: Date) {
return DateTime.fromJSDate(date).toLocaleString(DateTime.DATE_MED);
}
8 changes: 8 additions & 0 deletions src/utils/getCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getCollection, type CollectionEntry } from "astro:content";

export default async function getCategory(entry: CollectionEntry<'posts'>): Promise<CollectionEntry<'categories'> | undefined> {
const categories = await getCollection('categories');
const category = entry.id.split('/').at(0);
if (!category) return undefined;
return categories.find(c => c.id === category);
}
10 changes: 10 additions & 0 deletions src/utils/getPostDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type CollectionEntry } from "astro:content";

const now = new Date();

export default function getPostDate(entry: CollectionEntry<'posts'>) {
if (entry.data.date) {
return entry.data.date;
}
else return now;
}
7 changes: 7 additions & 0 deletions src/utils/isDraft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type CollectionEntry } from "astro:content";

const now = new Date();

export default function isDraft(entry: CollectionEntry<'posts'>): boolean {
return !(entry.data.date && entry.data.date < now);
}
11 changes: 11 additions & 0 deletions src/utils/labelDrafts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type CollectionEntry } from "astro:content";
import isDraft from "./isDraft";


export default function labelDrafts(entries: CollectionEntry<'posts'>[]): CollectionEntry<'posts'>[] {
const label = `[Draft]`;
return entries.map((e: CollectionEntry<'posts'>) => {
e.data.title = (isDraft(e) && !e.data.title.startsWith(label)) ? `${label} ${e.data.title}` : e.data.title;
return e;
});
}
17 changes: 17 additions & 0 deletions src/utils/renderMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sanitizeHtml from 'sanitize-html';
import MarkdownIt from 'markdown-it';
import anchor from 'markdown-it-anchor';
import footnote from 'markdown-it-footnote';

const parser = new MarkdownIt({
html: true,
typographer: true
}).use(anchor, {
permalink: anchor.permalink.headerLink({
safariReaderFix: true
})
}).use(footnote);

export default function renderMarkdown(markdown: string) {
return sanitizeHtml(parser.render(markdown));
}
8 changes: 8 additions & 0 deletions src/utils/sortByDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type CollectionEntry } from "astro:content";
import getPostDate from "./getPostDate";


export default function sortByDate(entries: CollectionEntry<'posts'>[]): CollectionEntry<'posts'>[] {
return entries.sort(
(a, b) => getPostDate(b).getTime() - getPostDate(a).getTime());
}
14 changes: 14 additions & 0 deletions src/utils/toWithNextAndPrev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export type WithNextAndPrev<T> = {
item: T,
next?: T,
prev?: T
}

export default function toWithNextAndPrev<T>(items: T[], reverse: boolean = false): WithNextAndPrev<T>[] {
return items.map((t, i, a) => ({
item: t,
next: reverse ? a[i - 1] : a[i + 1],
prev: reverse ? a[i + 1] : a[i - 1]
}));
}
4 changes: 4 additions & 0 deletions src/utils/trimTrailingSlash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export default function trimTrailingSlash(str: string): string {
return str.replace(/\/$/, "");
}
3 changes: 3 additions & 0 deletions src/utils/unMarkdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function unMarkdown(raw: string): string {
return raw?.replace(/[\*_`]/g, '')
}
104 changes: 0 additions & 104 deletions src/utils/utils.ts

This file was deleted.

0 comments on commit 0d2ebd6

Please sign in to comment.