jekyll: move relative URLs fix from Dockerfile to Jekyll plugin

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-07-28 21:16:57 +02:00
parent a288b444be
commit d2fca0eb8e
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
6 changed files with 66 additions and 35 deletions

View File

@ -47,16 +47,11 @@ ARG JEKYLL_ENV
ARG DOCS_URL
ENV TARGET=/out
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/src/.jekyll-cache <<EOT
set -eu
CONFIG_FILES=_config.yml$([ "$JEKYLL_ENV" = "production" ] && echo ",_config_production.yml" || true)
(
--mount=type=cache,target=/src/.jekyll-cache <<EOT
set -eu
CONFIG_FILES=_config.yml$([ "$JEKYLL_ENV" = "production" ] && echo ",_config_production.yml" || true)
set -x
bundle exec jekyll build --profile -d ${TARGET} --config ${CONFIG_FILES}
)
find ${TARGET} -type f -name '*.html' | while read i; do
sed -i "s#\(<a[^>]* href=\"\)${DOCS_URL}/#\1/#g" "$i"
done
EOT
# htmlproofer checks for broken links

25
_plugins/fix_swagger.rb Normal file
View File

@ -0,0 +1,25 @@
require 'jekyll'
require 'octopress-hooks'
require_relative 'util.rb'
module Jekyll
class FetchRemote < Octopress::Hooks::Site
def post_read(site)
beginning_time = Time.now
Jekyll.logger.info "Starting plugin fix_swagger.rb..."
files = Dir.glob(%w[./docker-hub/api/*.yaml ./engine/api/*.yaml])
Jekyll.logger.info " Fixing up #{files.size} swagger file(s)..."
files.each do |f|
Jekyll.logger.info " #{f}"
text = File.read(f)
replace = text.gsub(get_docs_url, "")
File.open(f, "w") { |f2| f2.puts replace }
end
end_time = Time.now
Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds"
end
end
end

29
_plugins/fix_url.rb Normal file
View File

@ -0,0 +1,29 @@
require 'jekyll'
require 'octopress-hooks'
require_relative 'util.rb'
module Jekyll
class FetchRemote < Octopress::Hooks::Site
def post_write(site)
beginning_time = Time.now
Jekyll.logger.info "Starting plugin fix_url.rb..."
# TODO: use dynamic URL from util.get_docs_url instead of hardcoded one
# but needs to remove first all absolute URLs in our code base.
docs_url = "https://docs.docker.com"
dest = site.config['destination'] || '_site'
files = Dir.glob("#{dest}/**/*.html")
Jekyll.logger.info " Fixing up URLs in #{files.size} html file(s) to be relative"
files.each do|f|
text = File.read(f)
replace = text.gsub(/(<a[^>]* href=\")#{docs_url}/, '\1')
File.open(f, "w") { |f2| f2.puts replace }
end
end_time = Time.now
Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds"
end
end
end

View File

@ -1,22 +0,0 @@
require 'jekyll'
require 'octopress-hooks'
module Jekyll
class FetchRemote < Octopress::Hooks::Site
def post_read(site)
beginning_time = Time.now
Jekyll.logger.info "Starting plugin fix_urls.rb..."
Jekyll.logger.info " Fixing up URLs in swagger files"
Dir.glob(%w[./docker-hub/api/*.yaml ./engine/api/*.yaml]) do |file_name|
Jekyll.logger.info " #{file_name}"
text = File.read(file_name)
replace = text.gsub("https://docs.docker.com", "")
File.open(file_name, "w") { |file| file.puts replace }
end
end_time = Time.now
Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds"
end
end
end

View File

@ -1,16 +1,15 @@
require_relative 'util.rb'
Jekyll::Hooks.register :site, :post_write do |site|
beginning_time = Time.now
Jekyll.logger.info "Starting plugin update_sitemap.rb..."
sitemap_path = File.join(site.dest, 'sitemap.xml')
# DEPLOY_URL is from Netlify for preview of sitemap on PR
# https://docs.netlify.com/configure-builds/environment-variables/#deploy-urls-and-metadata
docs_url = ENV['DEPLOY_URL'] || ENV['DOCS_URL'] || 'http://localhost:4000'
if File.exist?(sitemap_path)
sitemap_file = File.read(sitemap_path)
replace = sitemap_file.gsub!("<loc>/", "<loc>#{docs_url}/")
Jekyll.logger.info " Replacing '<loc>/' with '<loc>#{docs_url}/' in #{sitemap_path}"
replace = sitemap_file.gsub!("<loc>/", "<loc>#{get_docs_url}/")
Jekyll.logger.info " Replacing '<loc>/' with '<loc>#{get_docs_url}/' in #{sitemap_path}"
File.open(sitemap_path, "w") { |file| file.puts replace }
end

5
_plugins/util.rb Normal file
View File

@ -0,0 +1,5 @@
def get_docs_url
# DEPLOY_URL is from Netlify for preview
# https://docs.netlify.com/configure-builds/environment-variables/#deploy-urls-and-metadata
ENV['DEPLOY_URL'] || ENV['DOCS_URL'] || 'https://docs.docker.com'
end