Class: Ace::Support::Markdown::Molecules::KramdownProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/markdown/molecules/kramdown_processor.rb

Overview

Parse and serialize markdown using Kramdown Provides GFM-compatible markdown processing

Class Method Summary collapse

Class Method Details

.extract_headings(content) ⇒ Array<Hash>

Extract headings from markdown

Parameters:

  • content (String)

    The markdown content

Returns:

  • (Array<Hash>)

    Array of :level



91
92
93
94
95
96
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 91

def self.extract_headings(content)
  result = parse(content)
  return [] unless result[:success]

  find_headings(result[:document].root)
end

.find_headings(element, headings = []) ⇒ Object

Recursively find all headings in AST



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 101

def self.find_headings(element, headings = [])
  if element.type == :header
    text = element.children
      .select { |c| c.type == :text }
      .map(&:value)
      .join

    headings << {
      text: text,
      level: element.options[:level]
    }
  end

  element.children.each do |child|
    find_headings(child, headings)
  end

  headings
end

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

Parse markdown content to AST

Parameters:

  • content (String)

    The markdown content

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

    Kramdown options

Returns:

  • (Hash)

    Result with :document, :success, :errors



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
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 17

def self.parse(content, options: {})
  default_options = {
    input: "GFM", # GitHub Flavored Markdown
    hard_wrap: false,
    auto_ids: true,
    parse_block_html: true
  }

  merged_options = default_options.merge(options)

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

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

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

Round-trip: parse and convert back to markdown

Parameters:

  • content (String)

    The markdown content

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

    Kramdown options

Returns:

  • (Hash)

    Result with :markdown, :success, :errors



73
74
75
76
77
78
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 73

def self.round_trip(content, options: {})
  parse_result = parse(content, options: options)
  return parse_result unless parse_result[:success]

  to_markdown(parse_result[:document])
end

.to_markdown(document) ⇒ Hash

Convert markdown AST back to markdown string

Parameters:

  • document (Kramdown::Document)

    The kramdown document

Returns:

  • (Hash)

    Result with :markdown, :success, :errors

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 49

def self.to_markdown(document)
  raise ArgumentError, "Document must be a Kramdown::Document" unless document.is_a?(Kramdown::Document)

  begin
    markdown = document.to_kramdown

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

.valid?(content) ⇒ Boolean

Validate markdown can be parsed without errors

Parameters:

  • content (String)

    The markdown content

Returns:

  • (Boolean)

    true if valid



83
84
85
86
# File 'lib/ace/support/markdown/molecules/kramdown_processor.rb', line 83

def self.valid?(content)
  result = parse(content)
  result[:success] && result[:errors].empty?
end