Feat/sub-route (#38)
* blog subroute support * docs: update readme and blog Co-authored-by: mrhut10 <ahut10@gmail.com>
This commit is contained in:
30
lib/mdx.js
30
lib/mdx.js
@@ -1,12 +1,12 @@
|
||||
import MDXComponents from '@/components/MDXComponents'
|
||||
import fs from 'fs'
|
||||
import matter from 'gray-matter'
|
||||
import visit from 'unist-util-visit'
|
||||
import renderToString from 'next-mdx-remote/render-to-string'
|
||||
import path from 'path'
|
||||
import readingTime from 'reading-time'
|
||||
import renderToString from 'next-mdx-remote/render-to-string'
|
||||
|
||||
import MDXComponents from '@/components/MDXComponents'
|
||||
import visit from 'unist-util-visit'
|
||||
import imgToJsx from './img-to-jsx'
|
||||
import getAllFilesRecursively from './utils/files'
|
||||
|
||||
const root = process.cwd()
|
||||
|
||||
@@ -24,8 +24,11 @@ const tokenClassNames = {
|
||||
comment: 'text-gray-400 italic',
|
||||
}
|
||||
|
||||
export async function getFiles(type) {
|
||||
return fs.readdirSync(path.join(root, 'data', type))
|
||||
export function getFiles(type) {
|
||||
const prefixPaths = path.join(root, 'data', type)
|
||||
const files = getAllFilesRecursively(prefixPaths)
|
||||
// Only want to return blog/path and ignore root
|
||||
return files.map(file => file.slice(prefixPaths.length + 1))
|
||||
}
|
||||
|
||||
export function formatSlug(slug) {
|
||||
@@ -39,8 +42,8 @@ export function dateSortDesc(a, b) {
|
||||
}
|
||||
|
||||
export async function getFileBySlug(type, slug) {
|
||||
const mdxPath = path.join(root, 'data', type, `${slug}.mdx`)
|
||||
const mdPath = path.join(root, 'data', type, `${slug}.md`)
|
||||
const mdxPath = path.join(root, 'data', type, `${slug.join('/')}.mdx`)
|
||||
const mdPath = path.join(root, 'data', type, `${slug.join('/')}.md`)
|
||||
const source = fs.existsSync(mdxPath)
|
||||
? fs.readFileSync(mdxPath, 'utf8')
|
||||
: fs.readFileSync(mdPath, 'utf8')
|
||||
@@ -86,16 +89,19 @@ export async function getFileBySlug(type, slug) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllFilesFrontMatter(type) {
|
||||
const files = fs.readdirSync(path.join(root, 'data', type))
|
||||
export async function getAllFilesFrontMatter(folder) {
|
||||
const prefixPaths = path.join(root, 'data', folder)
|
||||
|
||||
const files = getAllFilesRecursively(prefixPaths)
|
||||
|
||||
const allFrontMatter = []
|
||||
|
||||
files.forEach((file) => {
|
||||
const source = fs.readFileSync(path.join(root, 'data', type, file), 'utf8')
|
||||
const fileName = file.slice(prefixPaths.length + 1)
|
||||
const source = fs.readFileSync(file, 'utf8')
|
||||
const { data } = matter(source)
|
||||
if (data.draft !== true) {
|
||||
allFrontMatter.push({ ...data, slug: formatSlug(file) })
|
||||
allFrontMatter.push({ ...data, slug: formatSlug(fileName) })
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user