Skip to content

Commit

Permalink
Merge pull request #266 from jonathanhefner/meta-tags-add-article_mod…
Browse files Browse the repository at this point in the history
…ified_time

Add `article:modified_time` meta tag
  • Loading branch information
jonathanhefner authored Jul 31, 2023
2 parents 794471b + 82f8a15 commit 9c72aaa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/rdoc/generator/template/rails/_head.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
<meta property="og:image" content="<%= canonical_url("/i/logo.svg") %>">
<% end %>
<meta property="og:type" content="article">
<meta property="article:modified_time" content="<%= og_modified_time %>">
8 changes: 6 additions & 2 deletions lib/sdoc/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SDoc::Helpers
require_relative "helpers/github"
include SDoc::Helpers::GitHub
require_relative "helpers/git"
include SDoc::Helpers::Git

def link_to(text, url, html_attributes = {})
return h(text) if url.nil?
Expand Down Expand Up @@ -50,6 +50,10 @@ def og_title(title)
"#{h title}#{" (#{project})" unless project.empty?}"
end

def og_modified_time
git_head_timestamp if git?
end

def page_description(leading_html, max_length: 160)
return if leading_html.nil? || !leading_html.include?("</p>")

Expand Down
10 changes: 9 additions & 1 deletion lib/sdoc/helpers/github.rb → lib/sdoc/helpers/git.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SDoc::Helpers::GitHub
module SDoc::Helpers::Git
def github_url(relative_path, line: nil)
return unless github?
line = "#L#{line}" if line
Expand All @@ -24,6 +24,14 @@ def git_head_sha1
end
end

attr_writer :git_head_timestamp

def git_head_timestamp
@git_head_timestamp ||= Dir.chdir(@options.root) do
`git show -s --format=%cI HEAD` .chomp
end
end

attr_writer :git_origin_url

def git_origin_url
Expand Down
20 changes: 20 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@ module Foo; module Bar; module Qux; end; end; end
end
end

describe "#og_modified_time" do
it "returns the commit time of the most recent commit in HEAD" do
@helpers.git_bin_path = "path/to/git"
@helpers.git_head_timestamp = "1999-12-31T12:34:56Z"

_(@helpers.og_modified_time).must_equal "1999-12-31T12:34:56Z"
end

it "returns the commit time of the most recent commit in HEAD (smoke test)" do
_(@helpers.og_modified_time).
must_match %r"\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\z"
end

it "returns nil when git is not installed" do
@helpers.git_bin_path = ""

_(@helpers.og_modified_time).must_be_nil
end
end

describe "#page_description" do
it "extracts the description from the leading paragraph" do
_(@helpers.page_description(<<~HTML)).must_equal "leading"
Expand Down

0 comments on commit 9c72aaa

Please sign in to comment.