Skip to content

fix: mdbook preprocessor links not taking into account root url #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/bencher_on_pr_or_fork_upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
workflows: [Run benchmarks on PR]
types: [completed]

permissions: write-all

jobs:
track_fork_pr_branch:
if: github.event.workflow_run.conclusion == 'success'
Expand Down
28 changes: 27 additions & 1 deletion crates/lad_backends/mdbook_lad_preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ mod argument_visitor;
mod markdown;
mod sections;

#[derive(Debug)]
struct Options {
pub root: String,
}

impl From<&PreprocessorContext> for Options {
fn from(context: &PreprocessorContext) -> Self {
let root = context
.config
.get_preprocessor("lad-preprocessor")
.and_then(|t| t.get("root"))
.and_then(|v| v.as_str())
.map(|s| s.to_owned())
.unwrap_or_default();
Options { root }
}
}

const LAD_EXTENSION: &str = "lad.json";

pub struct LADPreprocessor;
Expand All @@ -31,6 +49,7 @@ impl LADPreprocessor {
/// and `chapter_index` is the index of the chapter among its siblings.
fn process_lad_chapter(
_context: &PreprocessorContext,
options: &Options,
chapter: &mdbook::book::Chapter,
parent: Option<&mdbook::book::Chapter>,
chapter_index: usize,
Expand All @@ -45,9 +64,11 @@ impl LADPreprocessor {

let parent_path = parent
.and_then(|p| p.path.clone())
.unwrap_or_default()
.unwrap_or_else(|| options.root.clone().into())
.with_extension("");

log::debug!("Parent path: {:?}", parent_path);

let new_chapter = Section::new(
parent_path,
&ladfile,
Expand Down Expand Up @@ -75,6 +96,9 @@ impl Preprocessor for LADPreprocessor {
mut book: mdbook::book::Book,
) -> mdbook::errors::Result<mdbook::book::Book> {
let mut errors = Vec::new();
let options = Options::from(context);

log::debug!("Options: {:?}", options);

// first replace children in parents
book.for_each_mut(|item| {
Expand All @@ -89,6 +113,7 @@ impl Preprocessor for LADPreprocessor {
if LADPreprocessor::is_lad_file(chapter) {
match LADPreprocessor::process_lad_chapter(
context,
&options,
chapter,
Some(parent),
idx,
Expand Down Expand Up @@ -122,6 +147,7 @@ impl Preprocessor for LADPreprocessor {
}
let new_chapter = match LADPreprocessor::process_lad_chapter(
context,
&options,
chapter,
None,
chapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ description = "Documentation for the Bevy Scripting library"


[preprocessor.lad-preprocessor]

root = "root"

[output.markdown]
1 change: 1 addition & 0 deletions docs/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ enable = true
level = 1

[preprocessor.lad-preprocessor]
root = "bevy_mod_scripting"
Loading