Skip to content

Add mdbook preprocessor to rewrite template links#447

Open
cb341 wants to merge 1 commit intomainfrom
feature/mdbook-rewrite-links
Open

Add mdbook preprocessor to rewrite template links#447
cb341 wants to merge 1 commit intomainfrom
feature/mdbook-rewrite-links

Conversation

@cb341
Copy link
Copy Markdown
Contributor

@cb341 cb341 commented Apr 29, 2026

Rewrites relative links to templates/* (non-.md) into mdbook-rendered pages that show the file contents as syntax-highlighted code blocks. The preprocessor injects a virtual chapter for each referenced template and rewrites the link to .html, so clicking a template link in the served book opens a fast, locally-rendered page instead of triggering a raw-file download or a slow GitHub blob round trip.

Before the .rb files would simply get downloaded instead of rendered.
Rendering a code block has the advantage that sharing a link to the code automatically gets the theming of the ASG as well as a copy button.

Link Page
CleanShot 2026-04-30 at 14 59 06 CleanShot 2026-04-30 at 14 57 22

@cb341 cb341 self-assigned this Apr 29, 2026
@cb341 cb341 force-pushed the feature/mdbook-rewrite-links branch from 43e2a47 to 170ec5a Compare April 30, 2026 12:54
Comment thread ruby_on_rails/cucumber.md Outdated
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't know why this was missing

@cb341 cb341 marked this pull request as ready for review April 30, 2026 13:07
@cb341 cb341 requested a review from hunchr May 5, 2026 20:14
Comment thread bin/mdbook-rewrite-links Outdated
Comment on lines +15 to +20
LANGS = {
".rb" => "ruby", ".js" => "js", ".ts" => "ts", ".sh" => "sh",
".yml" => "yaml", ".yaml" => "yaml", ".json" => "json",
".scss" => "scss", ".css" => "css", ".html" => "html",
".feature" => "gherkin", ".erb" => "erb", ".jsx" => "jsx"
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is debatable.

We could either choose the easy route and serve it as RAW text.
OR we could take this approach and create virtual sections where the file contents are wrapped in a code block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The upside of the code block is the copy button and the more consistent look and feel.

Comment thread bin/mdbook-rewrite-links
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As this is mostly a behavioral change for mdbook itslef and doesn't affect the actual contents.

Comment thread bin/mdbook-rewrite-links Outdated
items.each(&walk)

inlined.each do |path, content|
fence = "`" * ([3, (content.scan(/`+/).map(&:length).max || 0) + 1].max)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

?

@cb341 cb341 force-pushed the feature/mdbook-rewrite-links branch from 38c69f5 to 8418df9 Compare May 6, 2026 06:49
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.

1 participant