cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nikhilkh <...@git.apache.org>
Subject [GitHub] cordova-docs pull request: Doc Anchor Fixes
Date Sat, 06 Feb 2016 00:38:00 GMT
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-docs/pull/487#discussion_r52092001
  
    --- Diff: www/static/js/docs.js ---
    @@ -17,49 +17,81 @@
     
     $(document).ready(function () {
     
    -    function getAnchorName(i, heading, prefix) {
    -        var name = prefix;
    +    function slugify(text) {
    +        text = text.toLowerCase();
    +
    +        // replace unaccepted characters with spaces
    +        // NOTE:
    +        //      a better regex would have been /[^\d\s\w]/ug, but the 'u' flag
    +        //      (Unicode) is not supported in some browsers, and we support
    +        //      many languages that use Unicode characters
    +        text = text.replace(/[\[\]\(\)\=\+\?]/g, ' ');
    +
    +        // trim whitespace and replace runs of whitespace with single dashes
    +        text = text.trim().replace(/ +/g, '-');
    +
    +        return text;
    +    }
    +
    +    function getIdForHeading(heading) {
             if (heading.id) {
    -            name += heading.id;
    +            return heading.id;
             } else if (heading.name) {
    -            name += heading.name;
    +            return heading.name;
             } else {
    -            name += i;
    +            return slugify(heading.innerText);
             }
    -        return name;
         }
     
    -    var anchorLink = function (id) {
    -        var anchor = document.createElement("a");
    +    function permalinkTo(id) {
    +        var anchor       = document.createElement("a");
             anchor.className = "header-link";
             anchor.href      = "#" + id;
             anchor.innerHTML = "<i class=\"glyphicon glyphicon-link\"></i>";
             return anchor;
         }
     
    +    function anchorFor(id) {
    +        var anchor       = document.createElement("a");
    +        anchor.className = "fragment-anchor";
    +        anchor.id        = id;
    +        return anchor;
    +    }
    +
    +    // go through all headings in the content and add some links
    +    $('#page-toc-source h1, #page-toc-source h2').each(function (i, heading) {
    +
    +        headingId = getIdForHeading(heading);
    +
    +        // add an anchor for linking to the heading
    +        // NOTE:
    +        //      we could have set the ID on the heading itself but we didn't
    +        //      because the <a> has some extra CSS that floats it above the
    +        //      heading and makes it easier to see when linked
    +        $(heading).before(anchorFor(headingId))
    +
    +        // add a permalink to all but the first heading
    +        if (i > 0) {
    +            $(heading).append(permalinkTo(headingId));
    +        }
    +    });
    +
         // Table of Contents
         $('#page-toc').toc({
             'selectors':         'h1,h2', // elements to use as headings
             'container':         '#page-toc-source', // element to find all selectors in
    -        'prefix':            'link-', // prefix for anchor tags and class names
    +        'prefix':            'toc-', // prefix for anchor tags and class names
    --- End diff --
    
    can we remove the prefix?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


Mime
View raw message