Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ here.](https://maxkapur.com/jekyll-related/)

## Installation

This gem is *not* hosted on RubyGems.org. I may upload it eventually; for now,
you can depend directly on the git repo by adding the following to your Gemfile:

```text
group :jekyll_plugins do
# Track the main branch. Might break occasionally :D
gem "jekyll-related", git: "https://github.com/maxkapur/jekyll-related", branch: "main"
# Pin a specific git commit
gem "jekyll-related", git: "https://github.com/maxkapur/jekyll-related", ref: "67da865bd33324d4330ca57e419e7782c1e8bc6d"
end
```

Below is boilerplate generated by the `bundle gem command`:

TODO: Replace
`UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your
gem name right after releasing it to RubyGems.org. Please do not do it earlier
Expand Down
1 change: 0 additions & 1 deletion jekyll-related.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency "jekyll", "~> 4.3"
spec.add_dependency "tokenizer", "~> 0.3"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
11 changes: 6 additions & 5 deletions lib/jekyll/generator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# frozen_string_literal: true

require "tokenizer"

module Jekyll
module Related
TOKENIZER = Tokenizer::WhitespaceTokenizer.new

class Generator < Jekyll::Generator
def generate(site)
# Update config with defaults in place. This ensures that the config
Expand All @@ -14,7 +10,7 @@ def generate(site)

# Count tokens within each post.
post_tallies = site.posts.docs.to_h do |post|
[post, (TOKENIZER.tokenize post.content.downcase).tally]
[post, (tokenize post.content.downcase).tally]
end

# Count global frequency of each token.
Expand Down Expand Up @@ -73,6 +69,11 @@ def generate(site)
end
end

# Split post body into "words"
def tokenize(post)
post.split(/\b/).delete_if { |x| /\s+/.match? x }
end

# Cosine similarity between the two count vectors
def similarity(current_post, related_post)
v_current = @post_token_volume[current_post][:volume]
Expand Down