Add config to toggle html extension in links

This commit is contained in:
Maxime Vaillancourt 2021-01-26 08:32:07 -05:00
parent 5d190b6a97
commit 9232ce3ded
5 changed files with 15 additions and 7 deletions

View File

@ -6,6 +6,10 @@ exclude: ['_includes/notes_graph.json']
# Pages deploys your repository (which is usually the repository name).
baseurl: /
# If you are using a host that cannot resolve URLs that do
# not end with .html (such as Neocities), set this to 'true'.
use_html_extension: false
permalink: pretty
relative_permalinks: false

View File

@ -22,7 +22,7 @@ layout: default
<div style="display: grid; grid-gap: 1em; grid-template-columns: repeat(1fr);">
{% for backlink in page.backlinks %}
<div class="backlink-box">
<a class="internal-link" href="{{ backlink.url }}">{{ backlink.title }}</a><br>
<a class="internal-link" href="{{ backlink.url }}{%- if site.use_html_extension -%}.html{%- endif -%}">{{ backlink.title }}</a><br>
<div style="font-size: 0.9em">{{ backlink.excerpt | strip_html | truncatewords: 20 }}</div>
</div>
{% endfor %}

View File

@ -4,4 +4,4 @@ title: Consistency is key
Show up. Do the work. Be consistent.
Then go take a look at the [first note](/your-first-note){: .internal-link}.
Then go take a look at the [[Your first note|first note]].

View File

@ -23,6 +23,8 @@ Since the Web is all about HTML, you can always use plain HTML if you want, like
Of course, you can also link to external websites, like this: [this is a link to Wikipedia](https://wikipedia.org/). Again, you can use plain HTML if you prefer.
**Note about static hosts**: if you use a static host that doesn't support URLs that don't end with `.html` (such as Neocities), try changing the `use_html_extension` value to `true` in the `_config.yml` file and restart the Jekyll server (or re-build the site). This adds a `.html` extension to note URLs and may resolve issues with links. If you're still having trouble, I recommend using Netlify to host your digital garden: it's free, easy to use, and fully supports this template's features out of the box.
### Automatic bi-directional links
Notice in the "Notes mentioning this note" section that there is another note linking to this note. This is a bi-directional link, and those are automatically created when you create links to other notes.

View File

@ -9,6 +9,8 @@ class BidirectionalLinksGenerator < Jekyll::Generator
all_docs = all_notes + all_pages
link_extension = !!site.config["use_html_extension"] ? '.html' : ''
# Convert all Wiki/Roam-style double-bracket link syntax to plain HTML
# anchor tag elements (<a>) with "internal-link" CSS class
all_docs.each do |current_note|
@ -22,28 +24,28 @@ class BidirectionalLinksGenerator < Jekyll::Generator
# [[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='#{note_potentially_linked_to.url}'>\\1</a>"
"<a class='internal-link' href='#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
)
# 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='#{note_potentially_linked_to.url}'>\\1</a>"
"<a class='internal-link' href='#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
)
# 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='#{note_potentially_linked_to.url}'>\\1</a>"
"<a class='internal-link' href='#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
)
# Replace double-bracketed links using note filename
# [[cats]]
current_note.content = current_note.content.gsub(
/\[\[(#{title_from_filename})\]\]/i,
"<a class='internal-link' href='#{note_potentially_linked_to.url}'>\\1</a>"
"<a class='internal-link' href='#{note_potentially_linked_to.url}#{link_extension}'>\\1</a>"
)
end
@ -71,7 +73,7 @@ class BidirectionalLinksGenerator < Jekyll::Generator
# Nodes: Graph
graph_nodes << {
id: note_id_from_note(current_note),
path: current_note.url,
path: "#{current_note.url}#{link_extension}",
label: current_note.data['title'],
} unless current_note.path.include?('_notes/index.html')