2022-09-29 01:59:28 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import matter from 'gray-matter'
|
2024-02-22 23:44:00 +00:00
|
|
|
import { unified } from 'unified'
|
2022-09-29 01:59:28 +00:00
|
|
|
import remarkGfm from 'remark-gfm'
|
2024-02-22 23:44:00 +00:00
|
|
|
import remarkParse from 'remark-parse'
|
|
|
|
import remarkRehype from 'remark-rehype'
|
|
|
|
import rehypeStringify from 'rehype-stringify'
|
2022-09-29 01:59:28 +00:00
|
|
|
|
|
|
|
// DOCUMENTS_PATH is useful when you want to get the path to a specific file
|
|
|
|
export const DOCUMENTS_PATH = path.join(process.cwd(), 'documents')
|
|
|
|
|
|
|
|
// documentFilePaths is the list of all mdx files inside the DOCUMENTS_PATH directory
|
|
|
|
export const documentFilePaths = fs
|
|
|
|
.readdirSync(DOCUMENTS_PATH)
|
|
|
|
// Only include md(x) files
|
|
|
|
.filter((path) => /\.mdx?$/.test(path))
|
|
|
|
|
2024-02-22 23:50:41 +00:00
|
|
|
export interface DocumentData {
|
|
|
|
title?: string
|
|
|
|
description?: string
|
|
|
|
indexWeight?: number
|
|
|
|
}
|
|
|
|
|
2022-09-29 01:59:28 +00:00
|
|
|
export interface Document {
|
|
|
|
content: string
|
2024-02-22 23:50:41 +00:00
|
|
|
data: DocumentData
|
2022-09-29 01:59:28 +00:00
|
|
|
filePath: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getDocuments = (): Document[] => {
|
2023-03-28 12:21:17 +00:00
|
|
|
const documents = documentFilePaths.map((filePath) => {
|
2022-09-29 01:59:28 +00:00
|
|
|
const source = fs.readFileSync(path.join(DOCUMENTS_PATH, filePath), 'utf-8')
|
|
|
|
const { content, data } = matter(source)
|
|
|
|
|
|
|
|
return {
|
|
|
|
content,
|
|
|
|
data,
|
|
|
|
filePath,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-28 12:21:17 +00:00
|
|
|
documents.sort((a, b) => {
|
|
|
|
if (a.data.indexWeight > b.data.indexWeight) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if (a.data.indexWeight < b.data.indexWeight) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return a.data.title.localeCompare(b.data.title)
|
|
|
|
})
|
|
|
|
|
2022-09-29 01:59:28 +00:00
|
|
|
return documents
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getDocumentBySlug = async (slug: string) => {
|
|
|
|
const documentFilePath = path.join(DOCUMENTS_PATH, `${slug}.mdx`)
|
|
|
|
const source = fs.readFileSync(documentFilePath, 'utf-8')
|
|
|
|
|
|
|
|
const { content, data } = matter(source)
|
|
|
|
|
2024-02-22 23:44:00 +00:00
|
|
|
const result = await unified()
|
|
|
|
.use(remarkParse)
|
|
|
|
.use(remarkGfm)
|
|
|
|
.use(remarkRehype)
|
|
|
|
// .use(rehypePrism)
|
|
|
|
.use(rehypeStringify)
|
|
|
|
.process(content)
|
2022-09-29 01:59:28 +00:00
|
|
|
|
2024-02-22 23:44:00 +00:00
|
|
|
return { source: result.value, data, documentFilePath }
|
2022-09-29 01:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getNextDocumentBySlug = (slug: string) => {
|
|
|
|
const documents = getDocuments()
|
|
|
|
const currentFileName = `${slug}.mdx`
|
|
|
|
const currentDocument = documents.find(
|
|
|
|
(document) => document.filePath === currentFileName
|
|
|
|
)
|
|
|
|
if (!currentDocument) return null
|
|
|
|
const currentDocumentIndex = documents.indexOf(currentDocument)
|
|
|
|
|
|
|
|
const document = documents[currentDocumentIndex - 1]
|
|
|
|
// no prev document found
|
|
|
|
if (!document) return null
|
|
|
|
|
|
|
|
const nextDocumentSlug = document?.filePath.replace(/\.mdx?$/, '')
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: document.data.title,
|
|
|
|
slug: nextDocumentSlug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getPreviousDocumentBySlug = (slug: string) => {
|
|
|
|
const documents = getDocuments()
|
|
|
|
const currentFileName = `${slug}.mdx`
|
|
|
|
const currentDocument = documents.find(
|
|
|
|
(document) => document.filePath === currentFileName
|
|
|
|
)
|
|
|
|
if (!currentDocument) return null
|
|
|
|
const currentDocumentIndex = documents.indexOf(currentDocument)
|
|
|
|
|
|
|
|
const document = documents[currentDocumentIndex + 1]
|
|
|
|
// no prev document found
|
|
|
|
if (!document) return null
|
|
|
|
|
|
|
|
const previousDocumentSlug = document?.filePath.replace(/\.mdx?$/, '')
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: document.data.title,
|
|
|
|
slug: previousDocumentSlug,
|
|
|
|
}
|
|
|
|
}
|