2020-05-19 22:59:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
class BidirectionalLinksGenerator < Jekyll::Generator
|
|
|
|
def generate(site)
|
2020-05-30 13:12:51 -04:00
|
|
|
all_notes = site.collections['notes'].docs
|
2020-07-18 14:37:33 -04:00
|
|
|
all_pages = site.pages
|
2020-05-30 13:12:51 -04:00
|
|
|
|
2020-07-18 14:37:33 -04:00
|
|
|
all_docs = all_notes + all_pages
|
|
|
|
|
2020-07-21 17:57:36 -04:00
|
|
|
# Convert all Wiki/Roam-style double-bracket link syntax to plain HTML
|
|
|
|
# anchor tag elements (<a>) with "internal-link" CSS class
|
2020-07-18 14:37:33 -04:00
|
|
|
all_docs.each do |current_note|
|
|
|
|
all_docs.each do |note_potentially_linked_to|
|
2020-05-30 13:12:51 -04:00
|
|
|
current_note.content = current_note.content.gsub(
|
|
|
|
/\[\[#{note_potentially_linked_to.data['title']}\]\]/i,
|
|
|
|
"<a class='internal-link' href='#{note_potentially_linked_to.url}'>#{note_potentially_linked_to.data['title']}</a>"
|
|
|
|
)
|
|
|
|
end
|
2020-07-21 17:57:36 -04:00
|
|
|
end
|
2020-05-19 22:59:37 -04:00
|
|
|
|
2020-07-21 17:57:36 -04:00
|
|
|
# Identify note backlinks and add them to each note
|
|
|
|
all_notes.each do |current_note|
|
2020-05-30 13:12:51 -04:00
|
|
|
notes_linking_to_current_note = all_notes.filter do |e|
|
2020-05-19 22:59:37 -04:00
|
|
|
e.content.include?(current_note.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
current_note.data['backlinks'] = notes_linking_to_current_note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|