diff --git a/src/world/loader.ts b/src/world/loader.ts index 0f17b63..22d7cd2 100644 --- a/src/world/loader.ts +++ b/src/world/loader.ts @@ -1,4 +1,23 @@ -import matter from 'gray-matter' +import { parse as parseYaml } from 'yaml' + +interface ParsedFile { + data: Record + content: string +} + +const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/ + +function matter(raw: string): ParsedFile { + const match = raw.match(FRONTMATTER_RE) + if (!match) { + return { data: {}, content: raw } + } + const yamlSrc = match[1] ?? '' + const content = match[2] ?? '' + const parsed = parseYaml(yamlSrc) + const data = (parsed && typeof parsed === 'object' ? parsed : {}) as Record + return { data, content } +} import type { Room, RoomDescriptions, Item } from './types' import type { Direction } from '../engine/types' import { roomFrontmatterSchema, itemFrontmatterSchema, endingFrontmatterSchema, encounterFrontmatterSchema } from './schema'