Created
February 23, 2023 00:47
-
-
Save RyanWarner/b699435108558d058afc6715a915cdf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fg from 'fast-glob' | |
import fs from 'fs' | |
import path from 'path' | |
import matter from 'gray-matter' | |
import { bundleMDX } from 'mdx-bundler' | |
import { remarkMdxImages } from 'remark-mdx-images' | |
import { IPostType } from 'types/IPostType' | |
import { IPostMeta } from 'types/IPostMeta' | |
import { IFrontmatter } from 'types/IFrontmatter' | |
const CONTENT = './src/_content' | |
const ignore = ['**/_obsidian-templates/**', '.obsidian/**'] | |
export const getSourceOfFile = (filePath: string): string => { | |
return fs.readFileSync(filePath).toString() | |
} | |
interface Props { | |
postType?: IPostType | |
} | |
export const getAllPosts = (props?: Props): IPostMeta[] => { | |
const path = props?.postType | |
? `./src/_content/${props?.postType}/**/*` | |
: `./src/_content/**/*` | |
const entries = fg.sync([`${path}.md`, `${path}.mdx`], { | |
ignore, | |
objectMode: true, | |
absolute: true, | |
}) | |
const data = entries | |
.map((entry) => { | |
const source = getSourceOfFile(entry.path) | |
const slug = entry.name.split('.')[0] | |
const { data } = matter(source) | |
return { | |
frontmatter: data as IFrontmatter, | |
slug, | |
} | |
}) | |
.filter((posts) => { | |
if (!posts.frontmatter.isPublished) return false | |
// if (props?.postType !== undefined) { | |
// return ( | |
// posts.frontmatter.postType?.toLowerCase() === | |
// props?.postType.toLowerCase() | |
// ) | |
// } | |
return true | |
}) | |
.sort( | |
(a, b) => | |
new Date(b.frontmatter.publishedDate).getTime() - | |
new Date(a.frontmatter.publishedDate).getTime() | |
) | |
return data | |
} | |
export const getSingleSource = (filePath: string): string => { | |
const glob = `${CONTENT}/**/${filePath}.*` | |
const myPath = fg.sync(glob)[0] | |
return fs.readFileSync(myPath).toString() | |
} | |
export const getSinglePost = async (slug: string) => { | |
const source = getSingleSource(slug) | |
const glob = `${CONTENT}/**/${slug}.*` | |
const myPath = fg.sync(glob)[0] | |
const sections = myPath.split('/') | |
const dir = myPath.replace(`/${sections[sections.length - 1]}`, '') | |
const directory = path.join(process.cwd(), dir) | |
const { code, frontmatter } = await bundleMDX({ | |
source, | |
cwd: directory, | |
mdxOptions: (options) => { | |
options.remarkPlugins = [ | |
...(options.remarkPlugins ?? []), | |
remarkMdxImages, | |
] | |
return options | |
}, | |
esbuildOptions: (options) => { | |
// Set the `outdir` to a public location for this bundle. | |
// This is where the files will be created. | |
options.outdir = path.join( | |
process.cwd(), | |
'/public/content/images' | |
) | |
options.loader = { | |
...options.loader, | |
// Tell esbuild to use the `file` loader for image files | |
'.png': 'file', | |
'.jpg': 'file', | |
} | |
// This is the path that will go in the img src. | |
options.publicPath = '/content/images' | |
// Set write to true so that esbuild will output the files. | |
options.write = true | |
return options | |
}, | |
}) | |
return { | |
frontmatter, | |
code, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment