Skip to content

Commit

Permalink
Make Book Backlinks more selective
Browse files Browse the repository at this point in the history
Books with simple titles sometimes had false positives if the word was
used in the text. This should make it so we only match on real links.

There is one edge case where it will match if you inlcude a
book_link.html but don't use the variable. I can't figure out how to get
around that. Yet.
  • Loading branch information
agude committed Nov 5, 2024
1 parent f9cba24 commit 610b121
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions _includes/book_backlinks.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ <h2 class="book-backlink-section">Reviews that mention <span
{% capture newline %}
{% endcapture %}

{% comment %}To avoid picking up words in the text that match a book title, we
instead look for the template we use to link to books, which should be
unique.{% endcomment %}
{% capture target_markdown %}book_link.html title="{{ page.title | downcase }}"{% endcapture %}
{% capture target_url %}{{ page.url | downcase }}{% endcapture %}

{% comment %}This sorted list will be used in a double for loop to insure that
books are sorted in their sections{% endcomment %}
{% assign sorted_titles = "" %}
Expand Down Expand Up @@ -49,9 +55,21 @@ <h2 class="book-backlink-section">Reviews that mention <span

{% comment %}Check if the title of this page is contained in the other
book's review. We have to used the normalized article text because if the
title is across a newline then it won't be found.{% endcomment %}
{% assign normalized_content = book.content | replace: newline, " " %}
{% if normalized_content contains page.title %}
title is across a newline then it won't be found.

At this point {{ normalized_content }} might or might not have been
rendered, so it could be either raw markdown or rendered HTML. We
therefore check for both the markdown link using {{ target_markdown }} or
the html link using {{ target_url }} raw markdown.

Also we don't have to worry about circular links because the content
variable doesn't include anything generated by the code in this file,
because this code is included in the layout, not in the markdown.{% endcomment %}
{% assign normalized_content = book.content | replace: newline, " " | downcase %}

{% if normalized_content contains target_markdown %}
{% assign placed_books = placed_books | push: book.title %}
{% elsif normalized_content contains target_url %}
{% assign placed_books = placed_books | push: book.title %}
{% endif %}

Expand Down

0 comments on commit 610b121

Please sign in to comment.