From 9cab4195c09325a5ceff00c74f15dadad38747be Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 25 Apr 2020 16:48:09 +0200 Subject: [PATCH] Replace JavaScript link fix with custom plugin This replaces the JavaScript link fix script with a custom plugin, based on the jekyll-relative-link plugin, and modified so that it can be used as Liquid "filter". While it borrows from the jekyll-relative-links plugin, it takes some shortcuts; - We use the code from jekyll-relative-links plugin to find/extract links on the page - Relative links are converted to absolute links, using the path of the markdown source file that's passed as argument - After conversion to an absolute link, we strip the ".md" extension; no attempt is made to resolve the file that's linked to. This is different from the jekyll-relative-links plugin, which _does_ resolve the linked file. This functionality could be added in future by someone who has more experience with Ruby. Signed-off-by: Sebastiaan van Stijn --- _includes/cli.md | 61 +--------------- _plugins/relative_links_filter.rb | 114 ++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 58 deletions(-) create mode 100644 _plugins/relative_links_filter.rb diff --git a/_includes/cli.md b/_includes/cli.md index f28ce4247f..c345dea800 100644 --- a/_includes/cli.md +++ b/_includes/cli.md @@ -4,7 +4,7 @@ ## Description -{{ controller_data.short }} +{{ controller_data.short | replace_relative_links: page.path }} {% if controller_data.min_api_version %} @@ -89,7 +89,7 @@ your client and daemon API versions. ## Extended description -{{ controller_data.long }} +{{ controller_data.long | replace_relative_links: page.path }} {% endunless %} @@ -137,7 +137,7 @@ For example uses of this command, refer to the [examples section](#examples) bel ## Examples -{{ controller_data.examples }} +{{ controller_data.examples | replace_relative_links: page.path }} {% endif %} @@ -203,58 +203,3 @@ For example uses of this command, refer to the [examples section](#examples) bel {% endunless %} - - diff --git a/_plugins/relative_links_filter.rb b/_plugins/relative_links_filter.rb new file mode 100644 index 0000000000..eff0459978 --- /dev/null +++ b/_plugins/relative_links_filter.rb @@ -0,0 +1,114 @@ +module Jekyll + # This custom Filter is used to fix up links to markdown pages that weren't + # resolved by Jekyll (or the "jekyll-relative-links" plugin). We need this hack + # to work around a bug in the "jekyll-relative-links" plugin; + # + # As reported in https://github.com/benbalter/jekyll-relative-links/issues/54, + # (relative) links to markdown pages in includes are not processed by Jekyll. + # This means that our reference pages (which use includes) have broken links. + # We could work around this by modifying the markdown for those pages to use + # "absolute" "html" links (/link/to/other/page/#some-anchor), but doing so + # would render the links broken when viewed on GitHub. Instead, we're fixing + # them up here, until the bug is fixed upstream. + # + # A second bug (https://github.com/benbalter/jekyll-relative-links/issues/61), + # causes (relative) links to markdown pages to not be resolved if the link's + # caption/title is wrapped. This bug is currently not handled by this plugin, + # but could possibly be addressed by modifying the TITLE_REGEX. + # + # This plugin is based on code in the jekyll-relative-links plugin, but takes + # some shortcuts; + # + # - We use the code from jekyll-relative-links plugin to find/extract links + # on the page + # - Relative links are converted to absolute links, using the path of the + # markdown source file that's passed as argument + # - After conversion to an absolute link, we strip the ".md" extension; no + # attempt is made to resolve the file that's linked to. This is different + # from the jekyll-relative-links plugin, which _does_ resolve the linked + # file. This functionality could be added in future by someone who has + # more experience with Ruby. + module RelativeLinksFilter + attr_accessor :site, :config + + # Use Jekyll's native relative_url filter + include Jekyll::Filters::URLFilters + + LINK_TEXT_REGEX = %r!(.*?)!.freeze + FRAGMENT_REGEX = %r!(#.+?)?!.freeze + TITLE_REGEX = %r{(\s+"(?:\\"|[^"])*(?