/**
* YAML Helper for ANProto-compatible messages
*
* Format:
* name: author
* image: hash
* previous: hash
* ---
* Message content
*/
export class YAMLHelper {
/**
* Create YAML document from metadata and content
*/
static create(metadata, content) {
const lines = []
if (metadata.name) {
lines.push(`name: ${metadata.name}`)
}
if (metadata.image) {
lines.push(`image: ${metadata.image}`)
}
if (metadata.previous) {
lines.push(`previous: ${metadata.previous}`)
}
if (lines.length === 0) {
// No metadata, just return content
return content
}
// Add separator and content
lines.push('---')
lines.push(content)
return lines.join('\n')
}
/**
* Parse YAML document into metadata and content
*/
static parse(yaml) {
const parts = yaml.split('\n---\n')
if (parts.length === 1) {
// No metadata, just content
return {
metadata: {},
content: yaml
}
}
const [frontMatter, ...contentParts] = parts
const content = contentParts.join('\n---\n')
const metadata = {}
const lines = frontMatter.split('\n')
for (const line of lines) {
const match = line.match(/^(\w+):\s*(.+)$/)
if (match) {
const [, key, value] = match
metadata[key] = value
}
}
return { metadata, content }
}
}