mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Python preprocessor example is ignorant of part titles

Open GetPsyched opened this issue 1 year ago • 0 comments

Problem

I am using mdBook in my Python libary project. The docs have a nice snippet of code for making a preprocessor in Python (thanks for that btw). However, I kept getting a KeyError saying that Chapter is not a valid key in the section. I couldn't even debug this since printing the output of the json would just be eaten by mdBook.

That is until today; instead of running print(book) I ran logging.error(book) which thankfully showed the entire json structure.

The reason for the KeyError was because the first entry was a part title.

Here's a small snippet of the JSON:

{
  "sections": [
      {"PartTitle": "User Guide"},
      {
          "Chapter": {
              "name": "Introduction",
              "content": "stuff",
              "number": [1],
              "sub_items": [],
              "path": "readme.md",
              "source_path": "readme.md",
              "parent_names": [],
          }
      },
      # more entries
  ]
}

Proposed Solution

The JSON structure should be mentioned along with the preprocessor snippet.

And this code just doesn't work unless you change the index. https://github.com/rust-lang/mdBook/blob/01778fc90a8909ad8a51571816f2e236d0e13099/guide/src/for_developers/preprocessors.md?plain=1#L118-L119

Notes

No response

GetPsyched avatar Jan 21 '24 07:01 GetPsyched