From 69655922dc8fada82e347b4598c53b784e370e20 Mon Sep 17 00:00:00 2001 From: Yehonal Date: Sun, 12 Jan 2020 18:24:18 +0100 Subject: improved red-link check --- _includes/git-wiki/sections/head/scripts.html | 2 +- _includes/git-wiki/sections/tail/tail.html | 8 +++- assets/js/checkLinks.js | 53 +++++++++++++++++++++++++++ assets/js/red-links.js | 41 --------------------- wiki/main_page.md | 2 +- 5 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 assets/js/checkLinks.js delete mode 100644 assets/js/red-links.js diff --git a/_includes/git-wiki/sections/head/scripts.html b/_includes/git-wiki/sections/head/scripts.html index 9cf23f3..9497626 100644 --- a/_includes/git-wiki/sections/head/scripts.html +++ b/_includes/git-wiki/sections/head/scripts.html @@ -4,7 +4,7 @@ - + diff --git a/_includes/git-wiki/sections/tail/tail.html b/_includes/git-wiki/sections/tail/tail.html index 8aa8139..f5944c9 100644 --- a/_includes/git-wiki/sections/tail/tail.html +++ b/_includes/git-wiki/sections/tail/tail.html @@ -18,9 +18,15 @@ {% endif %} +{% assign items = site.html_pages %} +{% for page in items %} +{% assign url = page.url | relative_url %} +{% assign urls = urls | append: url | append: "," %} +{% endfor %} + diff --git a/assets/js/checkLinks.js b/assets/js/checkLinks.js new file mode 100644 index 0000000..bd41fb6 --- /dev/null +++ b/assets/js/checkLinks.js @@ -0,0 +1,53 @@ +(function ($) { + // + // RED LINK FEATURE (Hacky) + $.fn.checkLinks = function (staticPages) { + setTimeout( + function () { + $('a').each(function () { + // avoid red link for external urls + if (this.hostname != window.location.hostname) { + if ($(this).parents('#git-wiki-content').length > 0) + $(this).addClass("external-link"); + return; + } + + for (var k in staticPages) { + var page = staticPages[k]; + var link = document.createElement("a"); + link.href = page; + + if (this.href === link.href) { + return; + } + } + + var ext = this.pathname.split('.').pop().split(/\#|\?/)[0]; + + // pessimistic condition based on the fact that + // markdown files are automatically converted in html + // if they are part of the wiki (the real check is right below) + var lExt = ext && ext.toLowerCase(); + var isRed = lExt == "md" || lExt == "markdown"; + + if (isRed) + $(this).css('color', 'red'); + + var that = this; + $.ajax({ + type: 'HEAD', + url: this.href, + success: function () { + $(that).css('color', ''); + }, + error: function (xhr, ajaxOptions, thrownError) { + if (xhr.status == 404) { + $(that).css('color', 'red'); + } + } + }); + }); + }, 0); + }; + +})(jQuery); \ No newline at end of file diff --git a/assets/js/red-links.js b/assets/js/red-links.js deleted file mode 100644 index 8c1d719..0000000 --- a/assets/js/red-links.js +++ /dev/null @@ -1,41 +0,0 @@ -(function ($) { - // - // RED LINK FEATURE (Hacky) - // TODO: filter external links - $.fn.redLinks = function () { - setTimeout( - function () { - $('a').each(function () { - // avoid red link for external urls - if (this.hostname != window.location.hostname) { - if ($(this).parents('#git-wiki-content').length > 0) - $(this).addClass("external-link"); - return; - } - - var ext = this.href.split('.').pop().split(/\#|\?/)[0]; - - // [Performance tip] pessimistic condition based on the fact that - // markdown files are automatically converted in html - // if they are part of the wiki (the real check is right below) - if (ext.toLowerCase() == "md" || ext.toLowerCase() == "markdown") - $(this).css('color', 'red'); - - var that = this; - $.ajax({ - type: 'HEAD', - url: this.href, - success: function () { - $(that).css('color', ''); - }, - error: function (xhr, ajaxOptions, thrownError) { - if (xhr.status == 404) { - $(that).css('color', 'red'); - } - } - }); - }); - }, 0); - }; - -})(jQuery); \ No newline at end of file diff --git a/wiki/main_page.md b/wiki/main_page.md index a1716a5..62ec0b5 100644 --- a/wiki/main_page.md +++ b/wiki/main_page.md @@ -4,4 +4,4 @@ redirect_from: "/" This is a sample of main page. You can edit it to start your wiki. -For documentation, installation guide and demo of [git-wiki-theme](git-wiki-theme.md) visit this [link](http://drassil.github.io/git-wiki/) +For documentation, installation guide and demo of [git-wiki-theme](git-wiki-theme) visit this [link](http://drassil.github.io/git-wiki/) -- cgit