Class: Synclenote::MarkdownToEnmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/synclenote/markdown_to_enml_builder.rb

Constant Summary collapse

HEADER =
<<~EOS
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
  <en-note>
EOS
<<~EOS
  </en-note>
EOS

Class Method Summary collapse

Class Method Details

.call(markdown_text) ⇒ Object



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
# File 'lib/synclenote/markdown_to_enml_builder.rb', line 16

def call(markdown_text)
  formatter = Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(
      filter_html: true,
      hard_wrap: true,
    ),
    underline: true,
    lax_spacing: true,
    footnotes: true,
    no_intra_emphasis: true,
    superscript: true,
    strikethrough: true,
    tables: true,
    space_after_headers: true,
    fenced_code_blocks: true,
    # autolink: true,
  )
  html = formatter.render(markdown_text)
  root = Nokogiri::HTML::DocumentFragment.parse(html)

  root.css("*[class]").each do |node|
    node.remove_attribute("class")
  end

  # '<a href="example.html?foo=bar&baz=quux">'
  # =>
  # '<a href="example.html?foo=bar&amp;baz=quux">'
  treated_html = root.to_html
  content = [
    HEADER,
    treated_html.gsub(/<(br|hr|img).*?>/, "\\&</\\1>"),
    FOOTER,
  ].join
  return content
end