Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: avoid generating subdirectories for each page on new docs site #15967

Merged
merged 6 commits into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/.eleventy.js
Expand Up @@ -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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m concerned about this approach. I don’t think it’s at all clear where we should and should not be using this filter, and I fear this will cause more errors in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule would be to use this filter in all places where page.url is written into the output content. In places where it is used in calculations, there is no need to use the filter (example on the current website). This is the same approach as on the current website.

If you have an idea for another approach, I could try it. We could maybe write the files without .html extension, but that would probably require additional configuration on Netlify to treat extensionless files as html.

if (url.endsWith(".html")) {
return url.slice(0, -".html".length);
}

return url;
});

//------------------------------------------------------------------------------
// Plugins
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions docs/package.json
Expand Up @@ -38,5 +38,8 @@
"rimraf": "^3.0.2",
"sass": "^1.52.1",
"slugify": "^1.6.3"
},
"engines": {
"node": ">=14.0.0"
}
}
2 changes: 1 addition & 1 deletion docs/src/_includes/components/docs-index.html
@@ -1,7 +1,7 @@
{% set navPages = collections.docs | eleventyNavigation %}
{% macro renderNavListItem(entry) -%}
<li class="docs-index__item" {% if entry.children.length %}data-has-children {% endif %}>
<a href="{{ entry.url | url }}" {% if entry.url == page.url %} aria-current="true" {% endif %}>{{ entry.title }}</a>
<a href="{{ entry.url | url | prettyURL }}" {% if entry.url == page.url %} aria-current="true" {% endif %}>{{ entry.title }}</a>
{%- if entry.children.length -%}
<ul data-child-list>
{%- for child in entry.children %}{{ renderNavListItem(child) }}{% endfor -%}
Expand Down
4 changes: 2 additions & 2 deletions docs/src/_includes/layouts/base.njk
Expand Up @@ -9,7 +9,7 @@
<!-- SEO -->
<title>{{ title }}</title>
<meta name="description" content="{{ desc }}">
<!--<link rel="canonical" href="https://docs.eslint.org{{ page.url | url }}">-->
<!--<link rel="canonical" href="https://docs.eslint.org{{ page.url | url | prettyURL }}">-->

<!-- favicon -->
<link rel="icon" href="/favicon.ico" sizes="any"><!-- 32×32 -->
Expand All @@ -25,7 +25,7 @@
<meta property="og:image:alt" content="{{ coverAlt }}">
<meta property="og:locale" content="en_US">
<meta property="og:type" content="website">
<meta property="og:url" content="{{ metadata.url }}{{ page.url }}">
<meta property="og:url" content="{{ metadata.url }}{{ page.url | url | prettyURL }}">
<!-- Twitter -->
<meta name="twitter:title" content="{{ title }}">
<meta name="twitter:card" content="summary_large_image">
Expand Down
2 changes: 1 addition & 1 deletion docs/src/_includes/layouts/components.html
Expand Up @@ -27,7 +27,7 @@
<ul class="index__list" id="js-index-list">
{% for item in collections.library %}
<li class="index__item">
<a href="{{ item.url | url }}" {{ helpers.getLinkActiveState(item.url,
<a href="{{ item.url | url | prettyURL }}" {{ helpers.getLinkActiveState(item.url,
page.url) | safe }}>{{ item.data.title }}</a>
</li>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/library/library.json
@@ -1,4 +1,4 @@
{
"layout": "components.html",
"permalink": "/component-library/{{ page.fileSlug }}/index.html"
"permalink": "/component-library/{{ page.fileSlug }}.html"
}
3 changes: 3 additions & 0 deletions docs/src/src.json
@@ -0,0 +1,3 @@
{
"permalink": "{{ page.filePathStem }}.html"
}
2 changes: 1 addition & 1 deletion docs/src/static/sitemap.njk
Expand Up @@ -6,7 +6,7 @@ eleventyExcludeFromCollections: true
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in collections.all %}
<url>
<loc>{{ site.url }}{{ page.url | url }}</loc>
<loc>{{ site.url }}{{ page.url | url | prettyURL }}</loc>
<lastmod>{{ page.date.toISOString() }}</lastmod>
<changefreq>{{ page.data.changeFreq or &quot;monthly&quot; }}</changefreq>
</url>
Expand Down