summaryrefslogtreecommitdiff
path: root/assets/js/red-links.js
blob: 8c1d71958b72d6f86cc02d434fee2e1f086ed676 (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
(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);