Class: Bridgetown::Utils::Wikilinks

Inherits:
Object
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ Wikilinks

Returns a new instance of Wikilinks.

Parameters:



19
20
21
22
# File 'bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb', line 19

def initialize(resource:)
  @resource = resource
  @site = resource.site
end

Class Method Details

.setup_parsing_hook(config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb', line 7

def self.setup_parsing_hook(config)
  markdown_exts = config.markdown_ext.split(",").map { ".#{_1}" }

  config.hook :resources, :pre_render, priority: :low do |resource|
    next unless markdown_exts.include?(resource.relative_path.extname)
    next if resource.data.bypass_wikilinks

    Wikilinks.new(resource:).convert
  end
end

Instance Method Details

#convertObject

Sets the resource’s new content to the parsed string where [[Wiki-style Links]] are turned into [Wiki-style Links](...)



26
27
28
# File 'bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb', line 26

def convert
  @resource.content = parse_content(@resource.content)
end

#missing_title_strategy(title) ⇒ Object



54
55
56
57
# File 'bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb', line 54

def missing_title_strategy(title)
  # TODO: this should be configurable
  "<u>#{title} (missing)</u>"
end

#parse_content(input) ⇒ Object

Parse all [[wiki links]] unless they’re escaped, e.g. \[[don't parse me bro]]. You can use a pipe character to control the displayed text of the link: [[Actual page title|Link text here]], and you can also add an anchor for the link: [[Page Title#page_section_anchor]]

Parameters:

  • input (String)

Returns:

  • String



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'bridgetown-core/lib/bridgetown-core/utils/wikilinks.rb', line 36

def parse_content(input)
  input.gsub %r!(\\?)\[\[(.+?)\]\]! do
    next "[[#{Regexp.last_match[2]}]]" if Regexp.last_match[1] == "\\"

    search_title, printed_title =
      Regexp.last_match[2].split("|").map(&:strip)
    search_title, anchor = search_title.split("#")
    anchor = "##{anchor}" if anchor
    title = printed_title || search_title
    found = @site.resources.find { _1.data.title == search_title }
    if found
      "[#{title}](#{found.relative_url}#{anchor}){:.wikilink}"
    else
      missing_title_strategy(title)
    end
  end
end