Top Level Namespace

Defined Under Namespace

Modules: Jekyll

Instance Method Summary collapse

Instance Method Details

#process_content(site_hostname, content, anchor_contents) ⇒ Object

Given hostname and content, updates <hN> tags, adding trailing <a class=“anchor-link”></a>.



5
6
7
8
9
10
11
12
# File 'lib/jekyll-theme-isotc154-helpers/headerlinks.rb', line 5

def process_content(site_hostname, content, anchor_contents)
  content = Nokogiri::HTML(content)
  content.css('main h2[id], main h3[id], main h4[id], main h5[id]').each do |el|
    html_id = el.get_attribute('id')
    el.inner_html = "#{el.inner_html}<a class='anchor-link' href='./##{html_id}'>#{anchor_contents}</a>"
  end
  return content.to_s
end

#process_doc_or_page(doc) ⇒ Object

Jekyll hook handler function.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll-theme-isotc154-helpers/headerlinks.rb', line 18

def process_doc_or_page(doc)
  site_hostname = URI(doc.site.config['url']).host

  # TODO: Read anchor_contents from site config
  anchor_contents = "<i class='fas fa-link'></i>"

  unless doc.respond_to?(:asset_file?) and doc.asset_file?
    if doc.respond_to?(:id)
      if $processed_urls.include? doc.id
        return
      end
      $processed_urls << doc.id
    end
    doc.output = process_content(site_hostname, doc.output, anchor_contents)
  end
end