Skip to content

Commit

Permalink
Remove duplication in bidirectional links generator
Browse files Browse the repository at this point in the history
  • Loading branch information
maximevaillancourt committed Mar 15, 2021
1 parent 264ba2b commit c1ba4ec
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions _plugins/bidirectional_links_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,35 @@ def generate(site)
File.extname(note_potentially_linked_to.basename)
).gsub('_', ' ').gsub('-', ' ').capitalize

new_href = "#{site.baseurl}#{note_potentially_linked_to.url}#{link_extension}"
anchor_tag = "<a class='internal-link' href='#{new_href}'>\\1</a>"

# Replace double-bracketed links with label using note title
# [[A note about cats|this is a link to the note about cats]]
current_note.content = current_note.content.gsub(
/\[\[#{title_from_filename}\|(.+?)(?=\])\]\]/i,
"<a class='internal-link' href='#{site.baseurl}#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
anchor_tag
)

# Replace double-bracketed links with label using note filename
# [[cats|this is a link to the note about cats]]
current_note.content = current_note.content.gsub(
/\[\[#{note_potentially_linked_to.data['title']}\|(.+?)(?=\])\]\]/i,
"<a class='internal-link' href='#{site.baseurl}#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
anchor_tag
)

# Replace double-bracketed links using note title
# [[a note about cats]]
current_note.content = current_note.content.gsub(
/\[\[(#{note_potentially_linked_to.data['title']})\]\]/i,
"<a class='internal-link' href='#{site.baseurl}#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
anchor_tag
)

# Replace double-bracketed links using note filename
# [[cats]]
current_note.content = current_note.content.gsub(
/\[\[(#{title_from_filename})\]\]/i,
"<a class='internal-link' href='#{site.baseurl}#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
anchor_tag
)
end

Expand Down

0 comments on commit c1ba4ec

Please sign in to comment.