discourse/lib/markdown_linker.rb

Failed to ignore revisions in .git-blame-ignore-revs.

30 lines
602 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-06 03:16:51 +08:00
# Helps create links using markdown (where references are at the bottom)
class MarkdownLinker
def initialize(base_url)
@base_url = base_url
@index = 1
@markdown_links = {}
@rendered = 1
end
def create(title, url)
@markdown_links[@index] = url.start_with?(@base_url) ? url : "#{@base_url}#{url}"
2013-02-06 03:16:51 +08:00
result = "[#{title}][#{@index}]"
2013-02-26 00:42:20 +08:00
@index += 1
2013-02-06 03:16:51 +08:00
result
end
def references
result = +""
2017-07-28 09:20:09 +08:00
(@rendered..@index - 1).each do |i|
2013-08-22 01:12:41 +08:00
result << "[#{i}]: #{@markdown_links[i]}\n"
2013-02-06 03:16:51 +08:00
end
@rendered = @index
result
end
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
end