init commit
This commit is contained in:
commit
c0ee8a4a11
54 changed files with 1055 additions and 0 deletions
27
utils/getArtifact.ts
Normal file
27
utils/getArtifact.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import matter from 'gray-matter'
|
||||
import { remark } from 'remark'
|
||||
import html from 'remark-html'
|
||||
|
||||
const root = path.join(process.cwd(), 'artifacts')
|
||||
|
||||
export async function getArtifact(dir: string, id: string) {
|
||||
const fullPath = path.join(root, dir, `${id}.md`)
|
||||
|
||||
// read file contents
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8')
|
||||
|
||||
// parse file contents
|
||||
const { content, data } = matter(fileContents)
|
||||
|
||||
// convert markdown to html
|
||||
const processedContent = await remark().use(html).process(content)
|
||||
|
||||
// return id, content, and data
|
||||
return {
|
||||
id,
|
||||
content: processedContent.toString(),
|
||||
...data,
|
||||
} as any
|
||||
}
|
||||
31
utils/getArtifacts.ts
Normal file
31
utils/getArtifacts.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import matter from 'gray-matter'
|
||||
|
||||
const root = path.join(process.cwd(), 'artifacts')
|
||||
|
||||
export const getArtifacts = (dir: string) => {
|
||||
const fileNames = fs.readdirSync(root + '/' + dir)
|
||||
|
||||
const filtered = fileNames.filter((fileName) => fileName.includes(`.md`))
|
||||
|
||||
return filtered.map((fileName) => {
|
||||
// set id
|
||||
const id = fileName.replace(/\.md$/, '')
|
||||
|
||||
// set full path
|
||||
const fullPath = path.join(root, dir, fileName)
|
||||
|
||||
// read file contents
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8')
|
||||
|
||||
// parse file contents
|
||||
const { data } = matter(fileContents)
|
||||
|
||||
// return id and data
|
||||
return {
|
||||
id,
|
||||
...data,
|
||||
}
|
||||
}) as any[]
|
||||
}
|
||||
2
utils/index.ts
Normal file
2
utils/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export { getArtifact } from './getArtifact'
|
||||
export { getArtifacts } from './getArtifacts'
|
||||
Loading…
Add table
Add a link
Reference in a new issue