Class: Obsidian::MarkdownParser

Inherits:
Object
  • Object
show all
Defined in:
lib/obsidian/parser/markdown_parser.rb

Constant Summary collapse

%r{
  \[\[
  (?<target>[^\]\#|]*) # link target
  (?:
    \#(?<fragment>[^|\]]*) # optional heading fragment
  )?
  (?:
    \|(?<text>[^\]]*) # optional link display text
  )?
  \]\]
}x

Instance Method Summary collapse

Instance Method Details

Convert Obsidian-flavored-markdown syntax to something parseable (i.e. with Github-flavored-markdown syntax)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/obsidian/parser/markdown_parser.rb', line 21

def expand_wikilinks(markdown_text, root:)
  markdown_text.gsub(WIKILINK_SYNTAX) do |s|
    text = $~[:text]
    target = $~[:target]
    fragment = $~[:fragment]
    page = root.find_in_tree(target)

    if page.nil?
      text.nil? ? target.split("/").last : text
    else
      display_text = text.nil? ? page.title : text
      href = fragment.nil? ? page.uri : "#{page.uri}##{fragment}"

      "[#{display_text}](#{href})"
    end
  end
end

#parse(markdown_text, root: nil) ⇒ Object



39
40
41
42
43
44
# File 'lib/obsidian/parser/markdown_parser.rb', line 39

def parse(markdown_text, root: nil)
  renderer = HtmlRenderer.new
  normalized = expand_wikilinks(markdown_text, root: root)
  document = Markly.parse(normalized, flags: Markly::SMART | Markly::UNSAFE | Markly::HARD_BREAKS, extensions: [:table, :tasklist, :autolink])
  Obsidian::ParsedMarkdownDocument.new(document, renderer: renderer)
end