Class: Ace::Support::Markdown::Molecules::KramdownProcessor
- Inherits:
-
Object
- Object
- Ace::Support::Markdown::Molecules::KramdownProcessor
- 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
-
.extract_headings(content) ⇒ Array<Hash>
Extract headings from markdown.
-
.find_headings(element, headings = []) ⇒ Object
Recursively find all headings in AST.
-
.parse(content, options: {}) ⇒ Hash
Parse markdown content to AST.
-
.round_trip(content, options: {}) ⇒ Hash
Round-trip: parse and convert back to markdown.
-
.to_markdown(document) ⇒ Hash
Convert markdown AST back to markdown string.
-
.valid?(content) ⇒ Boolean
Validate markdown can be parsed without errors.
Class Method Details
.extract_headings(content) ⇒ Array<Hash>
Extract headings from markdown
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.[:level] } end element.children.each do |child| find_headings(child, headings) end headings end |
.parse(content, options: {}) ⇒ Hash
Parse markdown content to AST
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: {}) = { input: "GFM", # GitHub Flavored Markdown hard_wrap: false, auto_ids: true, parse_block_html: true } = .merge() begin document = Kramdown::Document.new(content, ) { document: document, success: true, warnings: document.warnings || [], errors: [] } rescue => e { document: nil, success: false, warnings: [], errors: ["Kramdown parsing error: #{e.}"] } end end |
.round_trip(content, options: {}) ⇒ Hash
Round-trip: parse and convert back to markdown
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: ) return parse_result unless parse_result[:success] to_markdown(parse_result[:document]) end |
.to_markdown(document) ⇒ Hash
Convert markdown AST back to markdown string
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.}"] } end end |
.valid?(content) ⇒ Boolean
Validate markdown can be parsed without errors
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 |