mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: design game file and parse it to mongo
* 取消用 README 來寫關卡說明
This commit is contained in:
1 parent
6204bd0b17
commit
0de4b6ac9d
4 files changed
+61
-20
No files matched your search
@@ -1,15 +1,9 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { Marked } from 'marked'
|
||||
import yaml from 'yaml'
|
||||
|
||||
import Quest from '../models/quest.js'
|
||||
|
||||
const marked = new Marked({
|
||||
mangle: false,
|
||||
headerIds: false,
|
||||
})
|
||||
|
||||
const questDirectory = path.join(process.cwd(), 'quests')
|
||||
|
||||
export default async function loadAndUpdateQuests() {
|
||||
@@ -22,27 +16,19 @@ export default async function loadAndUpdateQuests() {
|
||||
if (!stat.isDirectory()) {
|
||||
return null
|
||||
}
|
||||
const fullPath = path.join(questDirectory, id, 'README.md')
|
||||
const fullPath = path.join(questDirectory, id, 'game.yml')
|
||||
|
||||
try {
|
||||
// Read and parse the README.md file.
|
||||
const markdown = await fs.readFile(fullPath, 'utf-8')
|
||||
const body = await fs.readFile(fullPath, 'utf-8')
|
||||
|
||||
// Extract and parse YAML frontmatter.
|
||||
const match = /---\n([\s\S]+?)\n---/.exec(markdown)
|
||||
const metadata = yaml.parse(match[1])
|
||||
const content = markdown.slice(match[0].length)
|
||||
const htmlContent = marked.parse(content)
|
||||
|
||||
const { title, order } = metadata
|
||||
const quest = yaml.parse(body)
|
||||
|
||||
return Quest.findByIdAndUpdate(
|
||||
id,
|
||||
{
|
||||
_id: id,
|
||||
title,
|
||||
order,
|
||||
content: htmlContent,
|
||||
id,
|
||||
...quest,
|
||||
},
|
||||
{
|
||||
upsert: true,
|
||||
|
||||
Reference in new issue
Block a user