Class: Ace::Lint::Atoms::FrontmatterExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/lint/atoms/frontmatter_extractor.rb

Overview

Pure function to extract frontmatter from markdown

Class Method Summary collapse

Class Method Details

.empty_resultObject



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

Parameters:

  • content (String)

    Markdown content with potential frontmatter

Returns:

  • (Hash)

    Result with :frontmatter, :body, :has_frontmatter



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

Parameters:

  • content (String)

    Markdown content

Returns:

  • (Boolean)

    True if frontmatter detected



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