Class: MarkdownLinker

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

Overview

Helps create links using markdown (where references are at the bottom)

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ MarkdownLinker

Returns a new instance of MarkdownLinker.



5
6
7
8
9
10
# File 'lib/markdown_linker.rb', line 5

def initialize(base_url)
  @base_url = base_url
  @index = 1
  @markdown_links = {}
  @rendered = 1
end

Instance Method Details

#create(title, url) ⇒ Object



12
13
14
15
16
17
# File 'lib/markdown_linker.rb', line 12

def create(title, url)
  @markdown_links[@index] = url.start_with?(@base_url) ? url : "#{@base_url}#{url}"
  result = "[#{title}][#{@index}]"
  @index += 1
  result
end

#referencesObject



19
20
21
22
23
24
# File 'lib/markdown_linker.rb', line 19

def references
  result = +""
  (@rendered..@index - 1).each { |i| result << "[#{i}]: #{@markdown_links[i]}\n" }
  @rendered = @index
  result
end