Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 1 | // 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 Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 15 | // 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 Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 22 | // |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 23 | // TODO(crbug.com/1271672): Figure out how to make this cleaner so the syntax |
| 24 | // is less clunky. |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 25 | function render(pageUrl, collectionOfAllPages) { |
| 26 | let topPage = new Page('', pageUrl); |
| 27 | |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 28 | let subPages = []; |
| 29 | for (const item of collectionOfAllPages) { |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 30 | if (isDescendant(item, topPage)) { |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 31 | subPages.push(new Page(item.data.title, item.data.page.url)); |
| 32 | } |
| 33 | } |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 34 | |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 35 | // Create a map from URLs to Pages for `pageUrl` and its sub-pages. |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 36 | const pageMap = new Map(); |
| 37 | pageMap.set(topPage.url, topPage); |
| 38 | |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 39 | // 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 Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 44 | pageMap.set(subPage.url, subPage); |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 45 | pageMap.get(subPage.parentUrl).subPages.push(subPage); |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | let html = ('<nav class="subpage-listing">\n' + |
| 49 | ' <h4>Subpage Listing</h4>\n' + |
| 50 | ' <ul>\n'); |
| 51 | |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 52 | topPage.subPages.sort(byProperty('title')); |
| 53 | for (const subPage of topPage.subPages) { |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 54 | html += ' <li>\n' + subPage.walk(3); |
| 55 | } |
| 56 | html += (' </ul>\n' + |
| 57 | '</nav>\n'); |
| 58 | |
| 59 | return html; |
| 60 | } |
| 61 | |
| 62 | class Page { |
| 63 | constructor(title, url) { |
| 64 | this.title = title; |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 65 | this.url = trimSlash(url); |
| 66 | this.parentUrl = dirname(this.url); |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 67 | |
| 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 Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 83 | let html = (`${indent}<details open>\n` + |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 84 | `${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 Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 92 | html += (`${indent} </ul>\n` + |
| 93 | `${indent}</details>\n`); |
| 94 | return html; |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 95 | } 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"). |
| 104 | function dirname(path) { |
| 105 | comps = path.split('/'); |
| 106 | return comps.slice(0, comps.length - 1).join('/'); |
| 107 | } |
| 108 | |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 109 | // Returns a copy of the string `s` with the rightmost `/` removed. |
| 110 | function trimSlash(s) { |
| 111 | if (s.endsWith('/')) { |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 112 | return s.substr(0, s.length - 1); |
| 113 | } |
| 114 | return s; |
| 115 | } |
| 116 | |
Dirk Pranke | aea46f0 | 2021-11-19 12:45:42 -0800 | [diff] [blame] | 117 | function isDescendant(item, topPage) { |
| 118 | let itemUrl = trimSlash(item.data.page.url); |
| 119 | return itemUrl.startsWith(topPage.url + '/'); |
| 120 | } |
| 121 | |
Dirk Pranke | db03b66 | 2021-11-19 09:15:15 -0800 | [diff] [blame] | 122 | // Returns a comparison function that will compare two objects by |
| 123 | // the lower-cased values of the specified property. |
| 124 | function 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 | |
| 132 | exports.render = render; |