rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
18.01k stars 1.62k forks source link

Metadata on a per-page basis #2142

Open cr0mll opened 1 year ago

cr0mll commented 1 year ago

Problem

There should simply be a way to specify custom metadata for each page and maybe have some predefined metadata attributes such as keywords, author information, or time of creation/modification which mdbook handles on its own.

For example, page keywords are only specified for the book as a whole - you cannot have separate keywords for each page which can make it difficult for people using search engines to find the correct page.

However, this can be augmented by also allowing one to specify for example author information for each page. This is useful for projects with multiple contributors which would like to give proper credit.

Proposed Solution

The way https://chirpy.cotes.page/ do things seems like an appropriate way to implement. Metadata is inserted into each page as key-value pairs in a markdown comment at the top of each page.

The value of each metadata attribute should be accessible as a template in index.hbs with {{ attribute_name }}.

Notes

This is not a request to implement specific metadata tags such as keywords or author information, but simply a request to expose custom metadata for each page as templates in index.hbs.

noraj commented 1 year ago

Metadata is inserted into each page as key-value pairs in a markdown comment at the top of each page.

It's called front-matter (like in hexo https://hexo.io/docs/front-matter). Generally the easiest front-matter for markdown format is using YAML.

Potentially inspirational projects:

Also Zola Rust staticgen has some front-matters: https://github.com/search?q=repo%3Agetzola%2Fzola%20frontmatter&type=code

sspaeti commented 8 months ago

I think this feature is very relevant if you want to share your book's subpage on socials and have an accurate description and feature image. I made a suggestion on how something like that could be implemented with front-matter in this comment: https://github.com/rust-lang/mdBook/issues/1416#issuecomment-1953878338.

I guess it wouldn't be too hard, we'd need a front-matter parser (here some I found):

And just include these in the index.hbs as illustrated in my comment. What do you think?

UPDATE: I made a PR that works for me, still a bit hacky, but can be improved: https://github.com/rust-lang/mdBook/pull/2321.