Skip to content

Commit

Permalink
refactor: update backlinks section in post.html and add backlinks gen…
Browse files Browse the repository at this point in the history
…erator plugin

- Update the backlinks section in the post.html layout file to include a conditional check for displaying backlinks.
- Add a new backlinks generator plugin in the _plugins directory to identify and add backlinks to each document.
- Modify the post.html layout file to include the generated backlinks in a styled box.

Related to cotes2020#123
  • Loading branch information
yonrasgg committed Oct 23, 2024
1 parent 0637e35 commit 0fbaf44
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
16 changes: 16 additions & 0 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ <h1 data-toc-skip>{{ page.title }}</h1>
crossorigin="anonymous"
async>
</script>

<!-- Backlinks Section -->
{% assign home = site.html_pages | where: 'url', '/' | first %}
{% if page.url != home.url %}
{% if page.showbacklinks == true %}
{% if page.backlinks.size > 0 %}
<div class="backlink-box">
<ul>
{% for backlink in page.backlinks %}
<li><a href="{{ site.url }}{{ backlink.url }}">{{ backlink.title }}</a></li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endif %}
{% endif %}
19 changes: 19 additions & 0 deletions _plugins/backlinks_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Jekyll
class BacklinksGenerator < Generator
def generate(site)
all_notes = site.collections['docs'].docs
all_posts = site.posts.docs
all_pages = site.pages

all_docs = all_notes + all_posts + all_pages

# Identify backlinks and add them to each doc
all_docs.each do |current_doc|
backlinks = all_docs.select do |doc|
doc.content.include?(current_doc.url)
end
current_doc.data['backlinks'] = backlinks
end
end
end
end
3 changes: 1 addition & 2 deletions _posts/2020-03-20-introduction-to-pytrhon.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,5 @@ Feel free to explore these resources as you begin your journey with Python!
---

[^footnote]: **Next Steps**: In the next post, we'll dive into {% raw %}
[**Python variables**](_posts/2020-03-20-introduction-to-pytrhon.md)
[**Python variables**](_posts/2020-03-20-python-variables.md)
{% endraw %} to get you familiar with manipulating data using Python. Stay tuned and happy coding!

14 changes: 14 additions & 0 deletions assets/css/jekyll-theme-chirpy.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@
';

/* append your custom style below */

/* Backlink Box Styles */
.backlink-box {
background-color: #fffdf7;
}

.backlink-box:before {
content: "What links here";
font-style: italic;
}

.backlink-box li {
line-height: 1em;
}

0 comments on commit 0fbaf44

Please sign in to comment.