summaryrefslogtreecommitdiff
path: root/assets/js/checkLinks.js
blob: bd41fb6d7367a0289a126e71b04f5a2ded089b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);