From 59b4652c6172b48a685bfb9a86e134540dc17aa6 Mon Sep 17 00:00:00 2001 From: psuwala Date: Fri, 31 Dec 2021 17:09:28 +0100 Subject: [PATCH] allow for using - and _ in the links --- _notes/your-first-note.md | 2 ++ _plugins/bidirectional_links_generator.rb | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/_notes/your-first-note.md b/_notes/your-first-note.md index 0fd3283..06d8b43 100644 --- a/_notes/your-first-note.md +++ b/_notes/your-first-note.md @@ -17,6 +17,8 @@ To link to another note, you can use multiple syntaxes. The following four use t Non-latin languages are supported too: [[안녕하세요]]. +Dashes and underscores in file names are supported, and may be omitted in the bracket link syntax. As an example, the `your-first-note.md` file can be linked to with [[your first note]] or [[your-first-note]], or even [[yOuR-FiRsT Note]]. + In all cases, if the double-bracket link does not point to a valid note, the double brackets will still be shown, like this: [[there is no note that matches this link]]. Alternatively, you can use regular [Markdown syntax](https://www.markdownguide.org/getting-started/) for links, with a relative link to the other note, like this: [this is a Markdown link to the note about cats](/cats){: .internal-link}. Don't forget to use the `.internal-link` class to make sure the link is styled as an internal link (without the little arrow). diff --git a/_plugins/bidirectional_links_generator.rb b/_plugins/bidirectional_links_generator.rb index 430b96a..918c662 100644 --- a/_plugins/bidirectional_links_generator.rb +++ b/_plugins/bidirectional_links_generator.rb @@ -15,10 +15,12 @@ class BidirectionalLinksGenerator < Jekyll::Generator # anchor tag elements () with "internal-link" CSS class all_docs.each do |current_note| all_docs.each do |note_potentially_linked_to| - title_from_filename = Regexp.escape(File.basename( - note_potentially_linked_to.basename, - File.extname(note_potentially_linked_to.basename) - ).gsub('_', ' ').gsub('-', ' ').capitalize) + note_title_regexp_pattern = Regexp.escape( + File.basename( + note_potentially_linked_to.basename, + File.extname(note_potentially_linked_to.basename) + ) + ).gsub('\_', '[ _]').gsub('\-', '[ -]').capitalize title_from_data = note_potentially_linked_to.data['title'] if title_from_data @@ -31,7 +33,7 @@ class BidirectionalLinksGenerator < Jekyll::Generator # 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.gsub!( - /\[\[#{title_from_filename}\|(.+?)(?=\])\]\]/i, + /\[\[#{note_title_regexp_pattern}\|(.+?)(?=\])\]\]/i, anchor_tag ) @@ -52,7 +54,7 @@ class BidirectionalLinksGenerator < Jekyll::Generator # Replace double-bracketed links using note filename # [[cats]] current_note.content.gsub!( - /\[\[(#{title_from_filename})\]\]/i, + /\[\[(#{note_title_regexp_pattern})\]\]/i, anchor_tag ) end