Class: Ace::Support::Items::Atoms::FrontmatterSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/items/atoms/frontmatter_serializer.rb

Overview

Serializes frontmatter hashes to YAML block strings with ‘—` delimiters. Preserves inline-array style (`tags: [ux, design]`) and quotes YAML-ambiguous values.

Constant Summary collapse

YAML_AMBIGUOUS =
/\A(true|false|yes|no|on|off|null|~|-?\d+(\.\d+)?([eE][+-]?\d+)?)\z/i

Class Method Summary collapse

Class Method Details

.rebuild(frontmatter, body) ⇒ String

Rebuild a full document from frontmatter and body

Parameters:

  • frontmatter (Hash)

    Frontmatter data

  • body (String)

    Document body content

Returns:

  • (String)

    Full document with frontmatter block and body



29
30
31
# File 'lib/ace/support/items/atoms/frontmatter_serializer.rb', line 29

def self.rebuild(frontmatter, body)
  "#{serialize(frontmatter)}\n\n#{body}"
end

.serialize(frontmatter) ⇒ String

Serialize frontmatter hash to YAML block string

Parameters:

  • frontmatter (Hash)

    Frontmatter data

Returns:

  • (String)

    YAML frontmatter block including ‘—` delimiters



16
17
18
19
20
21
22
23
# File 'lib/ace/support/items/atoms/frontmatter_serializer.rb', line 16

def self.serialize(frontmatter)
  lines = ["---"]
  frontmatter.each do |key, value|
    serialize_entry(lines, key, value, indent: 0)
  end
  lines << "---"
  lines.join("\n")
end