blob: 84f8ccabf67c980c077e1409581f6edf89453996 [file] [log] [blame]
Dirk Prankedb03b662021-11-19 09:15:15 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Dirk Prankeaea46f02021-11-19 12:45:42 -080015// This file implements support for the "subpages" extension. If a page author
16// inserts `{% subpages collections.all %}` into a document, this function will
17// find all of the pages that are sub-pages of the specified page (sub-pages in
18// the sense that /blink/design-documents is a sub-page of /blink) and display
19// them in a hierarchical tree format. `pageUrl` should be the (path part of
20// the) url of the current page (Eleventy's `page.url`) and
21// `collectionOfAllPages` should be Eleventy's `collections.all`.
Dirk Prankedb03b662021-11-19 09:15:15 -080022//
Dirk Prankeaea46f02021-11-19 12:45:42 -080023// TODO(crbug.com/1271672): Figure out how to make this cleaner so the syntax
24// is less clunky.
Dirk Prankedb03b662021-11-19 09:15:15 -080025function render(pageUrl, collectionOfAllPages) {
26 let topPage = new Page('', pageUrl);
27
Dirk Prankedb03b662021-11-19 09:15:15 -080028 let subPages = [];
29 for (const item of collectionOfAllPages) {
Dirk Prankeaea46f02021-11-19 12:45:42 -080030 if (isDescendant(item, topPage)) {
Dirk Prankedb03b662021-11-19 09:15:15 -080031 subPages.push(new Page(item.data.title, item.data.page.url));
32 }
33 }
Dirk Prankedb03b662021-11-19 09:15:15 -080034
Dirk Prankeaea46f02021-11-19 12:45:42 -080035 // Create a map from URLs to Pages for `pageUrl` and its sub-pages.
Dirk Prankedb03b662021-11-19 09:15:15 -080036 const pageMap = new Map();
37 pageMap.set(topPage.url, topPage);
38
Dirk Prankeaea46f02021-11-19 12:45:42 -080039 // Sorting the pages by url ensures that a parent will be added to the
40 // map before any of its descendants when we populate the map.
41 subPages.sort(byProperty('url'));
42
43 for (const subPage of subPages) {
Dirk Prankedb03b662021-11-19 09:15:15 -080044 pageMap.set(subPage.url, subPage);
Dirk Prankeaea46f02021-11-19 12:45:42 -080045 pageMap.get(subPage.parentUrl).subPages.push(subPage);
Dirk Prankedb03b662021-11-19 09:15:15 -080046 }
47
48 let html = ('<nav class="subpage-listing">\n' +
49 ' <h4>Subpage Listing</h4>\n' +
50 ' <ul>\n');
51
Dirk Prankeaea46f02021-11-19 12:45:42 -080052 topPage.subPages.sort(byProperty('title'));
53 for (const subPage of topPage.subPages) {
Dirk Prankedb03b662021-11-19 09:15:15 -080054 html += ' <li>\n' + subPage.walk(3);
55 }
56 html += (' </ul>\n' +
57 '</nav>\n');
58
59 return html;
60}
61
62class Page {
63 constructor(title, url) {
64 this.title = title;
Dirk Prankeaea46f02021-11-19 12:45:42 -080065 this.url = trimSlash(url);
66 this.parentUrl = dirname(this.url);
Dirk Prankedb03b662021-11-19 09:15:15 -080067
68 // This holds only the immediate sub-pages of the page, not the
69 // transitive closure of all sub-pages.
70 this.subPages = [];
71 }
72
73 // walk over the transitive closure of all of the page's subpages,
74 // and return an html fragment describing them as a tree of
75 // links and <details> elements (when a page has subpages).
76 // `indentDepth` is the number of levels to indent the HTML fragment.
77 walk(indentDepth) {
78 const indent = ' '.repeat(indentDepth);
79
80 this.subPages.sort(byProperty('title'));
81
82 if (this.subPages.length) {
Dirk Prankeaea46f02021-11-19 12:45:42 -080083 let html = (`${indent}<details open>\n` +
Dirk Prankedb03b662021-11-19 09:15:15 -080084 `${indent} <summary><a href="${this.url}">${
85 this.title}</a></summary>\n` +
86 `${indent} <ul>\n`);
87
88 for (const subPage of this.subPages) {
89 html += (`${indent} <li>\n` +
90 `${subPage.walk(indentDepth + 3)}`);
91 }
Dirk Prankeaea46f02021-11-19 12:45:42 -080092 html += (`${indent} </ul>\n` +
93 `${indent}</details>\n`);
94 return html;
Dirk Prankedb03b662021-11-19 09:15:15 -080095 } else {
96 return `${indent}<a href="${this.url}">${this.title}</a>\n`;
97 }
98 }
99}
100
101// Returns the directory above the `path`, e.g.:
102// `dirname("/foo/bar")` returns "/foo".
103// Note that `dirname("/foo/bar/") also returns "/foo").
104function dirname(path) {
105 comps = path.split('/');
106 return comps.slice(0, comps.length - 1).join('/');
107}
108
Dirk Prankeaea46f02021-11-19 12:45:42 -0800109// Returns a copy of the string `s` with the rightmost `/` removed.
110function trimSlash(s) {
111 if (s.endsWith('/')) {
Dirk Prankedb03b662021-11-19 09:15:15 -0800112 return s.substr(0, s.length - 1);
113 }
114 return s;
115}
116
Dirk Prankeaea46f02021-11-19 12:45:42 -0800117function isDescendant(item, topPage) {
118 let itemUrl = trimSlash(item.data.page.url);
119 return itemUrl.startsWith(topPage.url + '/');
120}
121
Dirk Prankedb03b662021-11-19 09:15:15 -0800122// Returns a comparison function that will compare two objects by
123// the lower-cased values of the specified property.
124function byProperty(prop) {
125 return (x, y) => {
126 a = x[prop].toLowerCase();
127 b = y[prop].toLowerCase();
128 return (a > b ? 1 : (a === b ? 0 : -1));
129 }
130}
131
132exports.render = render;