Class: Ace::Lint::Atoms::FrontmatterExtractor
- Inherits:
-
Object
- Object
- Ace::Lint::Atoms::FrontmatterExtractor
- Defined in:
- lib/ace/lint/atoms/frontmatter_extractor.rb
Overview
Pure function to extract frontmatter from markdown
Class Method Summary collapse
- .empty_result ⇒ Object
-
.extract(content) ⇒ Hash
Extract frontmatter and content from markdown.
-
.has_frontmatter?(content) ⇒ Boolean
Check if content has frontmatter.
Class Method Details
.empty_result ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/ace/lint/atoms/frontmatter_extractor.rb', line 63 def self.empty_result { frontmatter: nil, body: "", has_frontmatter: false, error: "Empty content" } end |
.extract(content) ⇒ Hash
Extract frontmatter and content from markdown
11 12 13 14 15 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 |
# File 'lib/ace/lint/atoms/frontmatter_extractor.rb', line 11 def self.extract(content) return empty_result if content.nil? || content.empty? # Check if content starts with frontmatter delimiter unless content.start_with?("---\n", "---\r\n") return { frontmatter: nil, body: content, has_frontmatter: false } end # Find the ending delimiter # Start search after the first "---\n" start_index = content.index("\n") + 1 end_match = content.match(/\n---\n|\n---\r\n/, start_index) unless end_match return { frontmatter: nil, body: content, has_frontmatter: false, error: "Missing closing '---' delimiter for frontmatter" } end end_index = end_match.begin(0) # Extract frontmatter YAML (between the delimiters) frontmatter_content = content[4...end_index] # Extract body content (after the closing delimiter) body_start = end_match.end(0) body_content = content[body_start..] || "" { frontmatter: frontmatter_content, body: body_content, has_frontmatter: true } end |
.has_frontmatter?(content) ⇒ Boolean
Check if content has frontmatter
56 57 58 59 60 61 |
# File 'lib/ace/lint/atoms/frontmatter_extractor.rb', line 56 def self.has_frontmatter?(content) return false if content.nil? || content.empty? content.start_with?("---\n", "---\r\n") && (content.include?("\n---\n") || content.include?("\n---\r\n")) end |