Class: Ace::Lint::Atoms::KramdownParser

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

Overview

Pure function to parse markdown with kramdown

Class Method Summary collapse

Class Method Details

.format(content, options: {}) ⇒ Hash

Format markdown content with kramdown

Parameters:

  • content (String)

    Markdown content

  • options (Hash) (defaults to: {})

    Kramdown options

Returns:

  • (Hash)

    Result with :success, :formatted_content, :errors



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ace/lint/atoms/kramdown_parser.rb', line 51

def self.format(content, options: {})
  parse_result = parse(content, options: options)

  return {success: false, formatted_content: nil, errors: parse_result[:errors]} unless parse_result[:success]

  begin
    # Convert back to markdown
    formatted = parse_result[:document].to_kramdown

    {
      success: true,
      formatted_content: formatted,
      errors: []
    }
  rescue => e
    {
      success: false,
      formatted_content: nil,
      errors: ["Kramdown formatting error: #{e.message}"]
    }
  end
end

.format_kramdown_message(warning) ⇒ Object

Kramdown warnings are already formatted strings



75
76
77
# File 'lib/ace/lint/atoms/kramdown_parser.rb', line 75

def self.format_kramdown_message(warning)
  warning.to_s
end

.parse(content, options: {}) ⇒ Hash

Parse markdown content with kramdown

Parameters:

  • content (String)

    Markdown content

  • options (Hash) (defaults to: {})

    Kramdown options

Returns:

  • (Hash)

    Result with :success, :document, :errors, :warnings



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
# File 'lib/ace/lint/atoms/kramdown_parser.rb', line 15

def self.parse(content, options: {})
  # Minimal defaults - let kramdown use its defaults
  # Users can override via .ace/lint/config.yml
  default_options = {
    input: "GFM" # Use GitHub Flavored Markdown
  }

  merged_options = default_options.merge(options)

  begin
    document = Kramdown::Document.new(content, merged_options)

    # Kramdown collects warnings during parsing (as strings)
    # All warnings are informational, not errors
    warnings = document.warnings || []

    {
      success: true, # Kramdown warnings don't indicate parsing failure
      document: document,
      errors: [],
      warnings: warnings
    }
  rescue => e
    {
      success: false,
      document: nil,
      errors: ["Kramdown parsing error: #{e.message}"],
      warnings: []
    }
  end
end