Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions pkg/posts/src/build/getMarkdownPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { marked } from 'marked'
import { readdir, readFile } from 'node:fs/promises'
import path from 'node:path'
import type { Post } from '../lib/types'
import { transformMd } from './transformMd'

const utf8 = { encoding: 'utf-8' as BufferEncoding }

export async function getMarkdownPosts(postsDir: string): Promise<Post[]> {
const {
default: { loadFront },
} = await import('yaml-front-matter')

const files = await readdir(postsDir, {
encoding: 'utf-8',
recursive: true,
})

return (
await Promise.all(
files
.filter((fn) => fn.match(/\.md$/))
.map(async (fn) => ({
contents: await readFile(`${postsDir}/${fn}`, utf8),
fn,
})),
)
)
.map(({ fn, contents }) => {
const { __content, ...frontmatter } = loadFront(contents)

return {
filename: path.basename(fn),
html: marked.parse(__content),
metadata: frontmatter,
}
})
.map((x) => transformMd(x))
}
5 changes: 4 additions & 1 deletion pkg/posts/src/build/getPostCollection.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/// <reference types="vitest/globals" />
import { getPostCollection } from './getPostCollection'
import type { Post } from '../lib/types'
import path from 'node:path'

const postsDir = path.resolve(process.cwd(), '../../content')

describe('getPostCollection', () => {
let allItems: Post[]
beforeAll(async () => {
allItems = await getPostCollection()
allItems = await getPostCollection(postsDir)
})

describe('allItems', () => {
Expand Down
46 changes: 4 additions & 42 deletions pkg/posts/src/build/getPostCollection.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
import { transformMd } from './markdown'
import { marked } from 'marked'
import { sortBy } from './sortBy'
import type { Post } from '../lib/types'
import path from 'node:path'
import { readdir, readFile } from 'node:fs/promises'

const markdownPath = path.resolve(process.cwd(), 'content/md')
const utf8 = { encoding: 'utf-8' as BufferEncoding }

const getMarkdownPosts = async (): Promise<Post[]> => {
const {
default: { loadFront },
} = await import('yaml-front-matter')

const files = await readdir(markdownPath, {
encoding: 'utf-8',
recursive: true,
})

return (
await Promise.all(
files
.filter((fn) => fn.match(/\.md$/))
.map(async (fn) => ({
contents: await readFile(`${markdownPath}/${fn}`, utf8),
fn,
})),
)
)
.map(({ fn, contents }) => {
const { __content, ...frontmatter } = loadFront(contents)

return {
filename: path.basename(fn),
html: marked.parse(__content),
metadata: frontmatter,
}
})
.map((x) => transformMd(x))
}
import { getMarkdownPosts } from './getMarkdownPosts'
import { sortBy } from './sortBy'

export const getPostCollection = async (): Promise<Post[]> => {
return (await getMarkdownPosts()).sort(sortBy<{}>('publishedDate', 'desc'))
export async function getPostCollection(postsDir: string): Promise<Post[]> {
return (await getMarkdownPosts(postsDir)).sort(sortBy<{}>('publishedDate', 'desc'))
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import md5 from 'md5'
import type { Post } from '../lib/types'

export interface MdFile {
type MdFile = {
filename: string
html: string
metadata: Record<string, string>
}

// function for reshaping each post
export const transformMd = ({
filename,
html,
metadata: { summary, title, date },
}: MdFile): Post => {
export function transformMd({ filename, html, metadata: { summary, title, date } }: MdFile): Post {
// the slug is the filename with the '.md' ending removed
const slug = filename.replace(/\.md$/, '').toLocaleLowerCase()

Expand Down
1 change: 1 addition & 0 deletions pkg/posts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"strict": true,
"moduleResolution": "bundler",
"noEmitOnError": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"module": "esnext"
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/posts/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { nodeExternals } from 'rollup-plugin-node-externals'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import { getPostCollection } from './src/build/getPostCollection'
import path from 'node:path'

export default defineConfig({
build: {
Expand All @@ -26,7 +27,7 @@ export default defineConfig({
},
load: async (id) => {
if (id === 'posts:json') {
const items = await getPostCollection()
const items = await getPostCollection(path.resolve(process.cwd(), '../../content'))
return `export default ${JSON.stringify(items, undefined, '\t')}`
}
},
Expand Down
Loading