diff --git a/server/src/index.js b/server/src/index.js index f1f7f34..6cbf66d 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -7,6 +7,7 @@ import swaggerUI from 'swagger-ui-express' import socketServer from './api/socket.js' import connectDB from './db.js' import apiRouter from './api/routes/index.js' +import loadAndUpdateQuests from './utils/quest.js' async function main() { const port = 3000 @@ -14,6 +15,7 @@ async function main() { const server = http.createServer(app) socketServer(server) await connectDB() + await loadAndUpdateQuests() const file = await fs.readFile('./swagger.yaml', 'utf8') const swaggerDocument = YAML.parse(file) diff --git a/server/src/models/quest.js b/server/src/models/quest.js index 329df8b..21e948f 100644 --- a/server/src/models/quest.js +++ b/server/src/models/quest.js @@ -1,12 +1,13 @@ -import { model } from 'mongoose' -import { Schema } from 'yaml' +import { model, Schema } from 'mongoose' const Quest = model( 'Quest', new Schema( { name: String, - description: String, + title: String, + order: Number, + content: String, }, { timestamp: true } ) diff --git a/server/src/utils/quest.js b/server/src/utils/quest.js index eb4e5f9..fe04459 100644 --- a/server/src/utils/quest.js +++ b/server/src/utils/quest.js @@ -1,8 +1,15 @@ import fs from 'fs/promises' import path from 'path' -import { marked } from 'marked' +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') async function parseQuests() { @@ -22,14 +29,17 @@ async function parseQuests() { const content = markdown.slice(match[0].length) const htmlContent = marked.parse(content) + const { title, order } = metadata + return { name, - metadata, + title, + order, content: htmlContent, } } catch (error) { console.error(`Error parsing quest ${name}:`, error) - return null + throw error } }) ) @@ -37,4 +47,17 @@ async function parseQuests() { return quests } -parseQuests().then((quests) => console.log(quests)) +async function updateQuests(quests) { + Promise.all( + quests.map((quest) => { + return Quest.findOneAndUpdate({ name: quest.name }, quest, { + upsert: true, + }) + }) + ) +} + +export default async function loadAndUpdateQuests() { + const quests = await parseQuests() + await updateQuests(quests) +}