Class: Ace::Support::Markdown::Atoms::FrontmatterSerializer
- Inherits:
-
Object
- Object
- Ace::Support::Markdown::Atoms::FrontmatterSerializer
- Defined in:
- lib/ace/support/markdown/atoms/frontmatter_serializer.rb
Overview
Pure function to serialize frontmatter hash to YAML format Handles delimiter wrapping and YAML formatting
Class Method Summary collapse
- .empty_frontmatter_result ⇒ Object
-
.frontmatter_only(frontmatter) ⇒ String
Serialize frontmatter only (without body).
-
.rebuild_document(frontmatter, body) ⇒ String
Rebuild markdown document with frontmatter and body.
-
.serialize(frontmatter, body: nil) ⇒ Hash
Serialize frontmatter hash to YAML string with delimiters.
Class Method Details
.empty_frontmatter_result ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/ace/support/markdown/atoms/frontmatter_serializer.rb', line 77 def self.empty_frontmatter_result { content: "---\n---", valid: true, errors: [] } end |
.frontmatter_only(frontmatter) ⇒ String
Serialize frontmatter only (without body)
68 69 70 71 72 73 |
# File 'lib/ace/support/markdown/atoms/frontmatter_serializer.rb', line 68 def self.frontmatter_only(frontmatter) result = serialize(frontmatter) raise ValidationError, result[:errors].join(", ") unless result[:valid] result[:content] end |
.rebuild_document(frontmatter, body) ⇒ String
Rebuild markdown document with frontmatter and body
58 59 60 61 62 63 |
# File 'lib/ace/support/markdown/atoms/frontmatter_serializer.rb', line 58 def self.rebuild_document(frontmatter, body) result = serialize(frontmatter, body: body) raise ValidationError, result[:errors].join(", ") unless result[:valid] result[:content] end |
.serialize(frontmatter, body: nil) ⇒ Hash
Serialize frontmatter hash to YAML string with delimiters
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ace/support/markdown/atoms/frontmatter_serializer.rb', line 16 def self.serialize(frontmatter, body: nil) return empty_frontmatter_result if frontmatter.nil? || frontmatter.empty? unless frontmatter.is_a?(Hash) return { content: "", valid: false, errors: ["Frontmatter must be a hash, got #{frontmatter.class}"] } end begin # Convert to YAML yaml_content = YAML.dump(frontmatter).strip # Remove leading --- that YAML.dump adds yaml_content = yaml_content.sub(/^---\n/, "") # Build the document parts = ["---", yaml_content, "---"] # Add body if provided parts << "" << body.strip if body && !body.empty? { content: parts.join("\n"), valid: true, errors: [] } rescue => e { content: "", valid: false, errors: ["YAML serialization error: #{e.}"] } end end |