From 90ff64742ede6ef29018cb967fc4f20d7b85b592 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 10 Jun 2022 20:43:25 +0200 Subject: [PATCH] chore: avoid generating subdirectories for each page on new docs site (#15967) * chore: avoid generating subdirectories for each page on new docs site Refs #15844 * add comments * refactor pathPrefix * update component library permalinks * rewrite URLs * fix indentation in package.json --- docs/.eleventy.js | 35 +++++++++++++++++++ docs/package.json | 3 ++ docs/src/_includes/components/docs-index.html | 2 +- docs/src/_includes/layouts/base.njk | 4 +-- docs/src/_includes/layouts/components.html | 2 +- docs/src/library/library.json | 2 +- docs/src/src.json | 3 ++ docs/src/static/sitemap.njk | 2 +- 8 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 docs/src/src.json diff --git a/docs/.eleventy.js b/docs/.eleventy.js index ba0c98d5a34..ed1ffb83162 100644 --- a/docs/.eleventy.js +++ b/docs/.eleventy.js @@ -91,6 +91,19 @@ module.exports = function(eleventyConfig) { return markdown.render(value); }); + /* + * Removes `.html` suffix from the given url. + * `page.url` will include the `.html` suffix for all documents + * except for those written as `index.html` (their `page.url` ends with a `/`). + */ + eleventyConfig.addFilter("prettyURL", url => { + if (url.endsWith(".html")) { + return url.slice(0, -".html".length); + } + + return url; + }); + //------------------------------------------------------------------------------ // Plugins //------------------------------------------------------------------------------ @@ -345,6 +358,28 @@ module.exports = function(eleventyConfig) { // END, eleventy-img + //------------------------------------------------------------------------------ + // Settings + //------------------------------------------------------------------------------ + + /* + * When we run `eleventy --serve`, Eleventy 1.x uses browser-sync to serve the content. + * By default, browser-sync (more precisely, underlying serve-static) will not serve + * `foo/bar.html` when we request `foo/bar`. Thus, we need to rewrite URLs to append `.html` + * so that pretty links without `.html` can work in a local development environment. + * + * There's no need to rewrite URLs that end with `/`, because that already works well + * (server will return the content of `index.html` in the directory). + * URLs with a file extension, like main.css, main.js, sitemap.xml, etc. should not be rewritten + */ + eleventyConfig.setBrowserSyncConfig({ + middleware: (req, res, next) => { + if (!/(?:\.[^/]+|\/)$/u.test(req.url)) { + req.url += ".html"; + } + return next(); + } + }); return { passthroughFileCopy: true, diff --git a/docs/package.json b/docs/package.json index ba963f8e767..a42c33aa68f 100644 --- a/docs/package.json +++ b/docs/package.json @@ -38,5 +38,8 @@ "rimraf": "^3.0.2", "sass": "^1.52.1", "slugify": "^1.6.3" + }, + "engines": { + "node": ">=14.0.0" } } diff --git a/docs/src/_includes/components/docs-index.html b/docs/src/_includes/components/docs-index.html index c915def4b62..0cdbe7403d0 100644 --- a/docs/src/_includes/components/docs-index.html +++ b/docs/src/_includes/components/docs-index.html @@ -1,7 +1,7 @@ {% set navPages = collections.docs | eleventyNavigation %} {% macro renderNavListItem(entry) -%}
  • - {{ entry.title }} + {{ entry.title }} {%- if entry.children.length -%}