Class: Ace::Support::Items::Atoms::FrontmatterParser
- Inherits:
-
Object
- Object
- Ace::Support::Items::Atoms::FrontmatterParser
- Defined in:
- lib/ace/support/items/atoms/frontmatter_parser.rb
Overview
Pure function to parse YAML frontmatter from markdown files. Extracts frontmatter hash and body content from ‘—` delimited blocks.
Class Method Summary collapse
-
.extract_body(content) ⇒ String
Extract content after frontmatter.
-
.parse(content) ⇒ Array(Hash, String)
Parse both frontmatter and body.
-
.parse_frontmatter(content) ⇒ Hash
Parse YAML frontmatter from markdown content.
Class Method Details
.extract_body(content) ⇒ String
Extract content after frontmatter
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ace/support/items/atoms/frontmatter_parser.rb', line 34 def self.extract_body(content) return "" if content.nil? || content.empty? if content.start_with?("---\n") end_index = content.index("\n---\n", 4) if end_index return content[(end_index + 5)..] || "" end end content end |
.parse(content) ⇒ Array(Hash, String)
Parse both frontmatter and body
50 51 52 |
# File 'lib/ace/support/items/atoms/frontmatter_parser.rb', line 50 def self.parse(content) [parse_frontmatter(content), extract_body(content)] end |
.parse_frontmatter(content) ⇒ Hash
Parse YAML frontmatter from markdown content
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ace/support/items/atoms/frontmatter_parser.rb', line 15 def self.parse_frontmatter(content) return {} if content.nil? || content.empty? return {} unless content.start_with?("---\n") end_index = content.index("\n---\n", 4) return {} unless end_index yaml_content = content[4...end_index] begin YAML.safe_load(yaml_content, permitted_classes: [Date, Time, Symbol]) || {} rescue Psych::SyntaxError {} end end |