Skip to content

Commit

Permalink
Merge pull request magento#5864 from magento/ds_fix-plugin
Browse files Browse the repository at this point in the history
Fix the 'last_updated_at' page plugin
  • Loading branch information
dshevtsov authored Oct 28, 2019
2 parents fcd8241 + 5fe7ebd commit d47c7cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
1 change: 0 additions & 1 deletion _plugins/generators/mrg_pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def generate(site)
# Data from '_data/codebase/mrg' as a Hash where
# the filename is a key and its content is a value.
mrg_data = @site.data['codebase']['mrg']

# Loop through the hash where a key is assigned to a 'mod' (module is a
# special token in Ruby and should not be used) and value is assigned to
# 'metadata'.
Expand Down
26 changes: 17 additions & 9 deletions _plugins/page-params/last-modified-at.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@
# the original file.
# For available date formats, refer to https://git-scm.com/docs/git-log#git-log---dateltformatgt
#
Jekyll::Hooks.register :pages, :pre_render do |page|
Jekyll::Hooks.register :pages, :post_init do |page|
# Do nothing in serving mode
next if page.site.config['serving']
# Do nothing if the date is already set
next if page.data['last_modified_at']

# Process only files with 'md' and 'html' extensions
next unless File.extname(page.path).match?(/md|html/)
# Do nothing for redirects

# Skip redirects
next if page.name == 'redirect.html'

# Skip pages where the parameter is already set
next if page.data['last_modified_at']

# Skip pages created by custom generators like 'mrg_pages'
next if page.kind_of? Jekyll::PageWithoutAFile

# Read real path of the page. If this is a symlink read it to get path of the real file with content.
real_filepath = File.realpath page.path

dir = File.dirname real_filepath
filename = File.basename real_filepath

# Read date of the last commit and assign it to last_modified_at parameter
# of the page.
page.data['last_modified_at'] =
`cd #{dir} && git log -1 --format=%cd --date=iso -- #{filename}`.strip
# Change directory to the parent directory of the page to read from the corresponding git history.
Dir.chdir(dir) do
# Read date of the last commit and assign it to last_modified_at parameter
# of the page.
page.data['last_modified_at'] = `git log -1 --format=%cd --date=iso -- #{page.name}`.strip
end
end

0 comments on commit d47c7cd

Please sign in to comment.