PageListWithBreadcrumbs

Table of contents
No headers
/***
    USAGE:

    PageListWithBreadcrumbs(pages, ancestor)

    PARAMETERS:

    pages : list
        List of pages objects to show in the list.

    (optional) ancestor : map (default: homepage)
        Common parent page.  Is used to determine where the breadcrumbs should start from.

***/

var pages = $0 ?? $pages;
var ancestor = $1 ?? $ancestor;

var ancestor_depth = ancestor ? #ancestor.parents : 0;

var pages_by_parent_id = { };
var ordered_parent_ids = [ ];
foreach(var page in pages) {
    var parent_id = page.parent_id ?? page.parent.id;
    if(pages_by_parent_id[parent_id] is nil) {
        let ordered_parent_ids ..= [ parent_id ];
        let pages_by_parent_id ..= { (parent_id): [ page ] };
     } else {
        let pages_by_parent_id ..= { (parent_id): pages_by_parent_id[parent_id] .. [ page ] };
    }
}

foreach(var parent_id in ordered_parent_ids) {
    <div class=(__count % 2 ? "mt-control-list mt-control-list-alt": "mt-control-list")>
        var pages = pages_by_parent_id[parent_id];
      
        // show linked page path
        <div class="mt-control-block-category">
            var parents = pages[0].parents;
            if(ancestor_depth > 0) {
                let parents = list.splice(parents, 0, ancestor_depth);
            }
            let parents = list.splice(parents, -1);
            <ol>
                foreach(var parent in parents) {
                    <li>web.link(parent.uri, parent.title);</li>
                }
            </ol>
        </div>

        // list child pages
        <div class="mt-control-block-content">
            <dl>
                foreach(var page in pages) {
                    <dt>
                        web.link(page.uri, page.title);
                        if (page.rating.count > 0) {
                            " ";
                            <span class="rating">
                            var upvotes = Num.round(page.rating.score * page.rating.count);
                            wiki.localize("MindTouch.IDF.pagelist.rating", [upvotes, page.rating.count]);
                            </span>
                        }
                    </dt>
                    <dd>
                        page.overview ?? wiki.text(page.path, "Overview")
                    </dd>
                }
            </dl>
        </div>
    </div>
}
Page statistics
277 view(s), 6 edit(s) and 2558 character(s)

Tags

This page has no custom tags set.

Comments

You must to post a comment.

Attach file

Attachments