Skip to content

Conversation

@weihanglo
Copy link
Member

The PR is for

Add new preprocessor to convert README.md to index.md.

Intent

README.md is a de facto index file in markdown-based documentation. Many version control service such as GitHub, Gitlab and Bitbucket render README.md as index files while browsering source code on their web client. Hence, we may respect to README.md and convert it into index.md which would then convert into index.html.

I think this behavior is what people expect nowadays. It would be nice to include this feature.

weihanglo added 2 commits May 1, 2018 09:16
README.md is a de facto index file in markdown-based documentation.
Hence, we respect to README.md and convert it into index.html.
Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan left a comment

Choose a reason for hiding this comment

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

@weihanglo I really like the idea you're proposing here!

We probably want to add an integration test to make sure the README.md is actually rendered to index.html (look at the other tests in tests/rendered_output.rs for inspiration), but other than that I'm really happy with this PR.

fn run(&self, _ctx: &PreprocessorContext, book: &mut Book) -> Result<()> {
book.for_each_mut(|section: &mut BookItem| {
if let BookItem::Chapter(ref mut ch) = *section {
if ch.path.file_name().unwrap_or_default() == "README.md" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, do you think it's worth also checking for something like readme.md or README? So using the file stem instead and a case insensitive match.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh. I almost forgot there is a file_stem method can use. Sounds great!

@weihanglo
Copy link
Member Author

I have some concerns with opt-in this feature by default:

  1. What if a markdown file named index.md co-exists with README.md?
  2. mdbook picks the first prefix chapter item to generate the root index.html. How to solve the conflicts with another README.md?

Should we need to inspect this issue? Or just leave the decision for users. Let users opt-out the preprocessor or reorganize their book structure manually.

We can also check out how gitbook did on this issue. I tried and it seems that if both README.md and index.md exist, Gitbook will generate the last one listed in SUMMARY.md as the only index.html.

@Michael-F-Bryan
Copy link
Contributor

What if a markdown file named index.md co-exists with README.md?

I'd probably emit a warning (if possible) and then not apply the automatic conversion.

Should we need to inspect this issue? Or just leave the decision for users. Let users opt-out the preprocessor or reorganize their book structure manually.

I think this is going to happen quite rarely in practice. Most people have their books in a subdirectory instead of the root directory, so the chance of having index.md clobber README.md (or vice versa) are quite low. If we emit a warning then that should be enough for people to fix the problem.

I'd prefer for this preprocessor to be included by default, otherwise it'll be hard to discover and never get used.

@weihanglo
Copy link
Member Author

  • Integration tests are added at 009ebdc.
  • Case-insensitive matching is implemented at 92487a3.

As for file name conflict issue, I decided to convert all README.md and index.md without regarding any pitfall. Just as the same behavior you metioned above, it only emit some warnings to inform users to fix it.

@Michael-F-Bryan
Copy link
Contributor

I like the integration tests and the check for matching different variants of README.md 👍

This PR looks about ready to merge. If you've got time, would you be able to make a follow-up PR which documents the index preprocessor and what it does in the user guide?

@Michael-F-Bryan Michael-F-Bryan merged commit 6959964 into rust-lang:master May 4, 2018
@weihanglo
Copy link
Member Author

@Michael-F-Bryan
Thanks for you review! I am willing to document some notes about the index preprocessor.

@weihanglo weihanglo deleted the feature/index-preprocessor branch May 5, 2018 01:30
Ruin0x11 pushed a commit to Ruin0x11/mdBook that referenced this pull request Aug 30, 2020
* Add index preprocessor

README.md is a de facto index file in markdown-based documentation.
Hence, we respect to README.md and convert it into index.html.

* Fix warning for unused variables

* Update tests for config

* Match file stem case-insensitively for IndexPreprocessor

* Add tests for IndexPreprocessor

* Update book example to fit index preprocessor
ehuss added a commit to ehuss/mdBook that referenced this pull request Dec 10, 2025
This fixes a problem where custom preprocessors were not being
registered when running tests. This was caused by the test function
rebuilding the preprocessor map.

This removes the code that was rebuilding the preprocessors and removing
the IndexPreprocessor when running tests. Skipping IndexPreprocessor was
added back in rust-lang#741 to fix
rust-lang#724 which was caused by
rust-lang#685 which added the
IndexPreprocessor.

Additionally, rust-lang#1986 added
running *all* preprocessors.

The IndexPreprocessor was removed because in the past the code was
testing against the source directly, and the path from `chapter.path` is
the converted `index.md` file, and that filename does not exist in the
source. This isn't a problem anymore because due to
rust-lang#891 it is not reading from the
`src` directory.

Note that this results in a minor change where the chapter path changes
from `README.md` to `index.md` in the output and the `--chapter` option.
I think I'm ok with that change, though it would be easy to switch it
back if that's an issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants