Skip to content

Section Headers not Displaying if '.html' Stripped from URL #2962

@kgsensei

Description

@kgsensei

Problem

If the built docs are being hosted on a site that automatically strips the .html from urls (e.g. example.com/page.html -> example.com/page) then the section headers aren't displayed for the current page and the current page isn't even highlighted.

Example with .html in URL:
Image

Same page w/o .html in URL:
Image

Steps

  1. Build mdBook docs
  2. Host docs on website/service that removes .html from URLs
  3. Navigate to a page with section headers

A good way to reproduce locally is mdbook serve (which has .html) vs. npx serve (which strips .html)
(My npx serve version is 14.2.0)

Possible Solution(s)

I fixed this problem with the following code in toc.js, albeit a bit hacky:

// Set the current, active page, and reveal it if it's hidden
let current_page = document.location.href.toString().split('#')[0].split('?')[0];
+ if (!current_page.endsWith(".html")) {
+   current_page += ".html";
+ }
if (current_page.endsWith('/')) {
    current_page += 'index.html';
}

This will essentially pretend the URL still has the .html, even if it doesn't actually. I saw no problems when testing this code, it does get wiped out whenever the docs are rebuilt though (understandably.)

Notes

Another possible solution would be to add a flag that allows customization of the generated links. For example:

[output.html]
hrefs-include-extension=false # (where default would be true)

Version

mdbook v0.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: A bug, incorrect or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions