Skip to content

Commit a643b70

Browse files
authored
fix: make all links in the mdbook preprocessor relative (#392)
# Summary - absolute links don't work that well, just switch back to relative ones
1 parent 09f1f15 commit a643b70

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

crates/lad_backends/mdbook_lad_preprocessor/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use mdbook::{
66
preprocess::{Preprocessor, PreprocessorContext},
77
};
88
use sections::{Section, SectionData};
9+
use std::sync::OnceLock;
910
mod argument_visitor;
1011
mod markdown;
1112
mod sections;
@@ -29,6 +30,8 @@ impl From<&PreprocessorContext> for Options {
2930
}
3031

3132
const LAD_EXTENSION: &str = "lad.json";
33+
// global for options
34+
static OPTIONS: OnceLock<Options> = OnceLock::new();
3235

3336
pub struct LADPreprocessor;
3437

@@ -49,7 +52,6 @@ impl LADPreprocessor {
4952
/// and `chapter_index` is the index of the chapter among its siblings.
5053
fn process_lad_chapter(
5154
_context: &PreprocessorContext,
52-
options: &Options,
5355
chapter: &mdbook::book::Chapter,
5456
parent: Option<&mdbook::book::Chapter>,
5557
chapter_index: usize,
@@ -64,7 +66,7 @@ impl LADPreprocessor {
6466

6567
let parent_path = parent
6668
.and_then(|p| p.path.clone())
67-
.unwrap_or_else(|| options.root.clone().into())
69+
.unwrap_or_default()
6870
.with_extension("");
6971

7072
log::debug!("Parent path: {:?}", parent_path);
@@ -99,6 +101,9 @@ impl Preprocessor for LADPreprocessor {
99101
let options = Options::from(context);
100102

101103
log::debug!("Options: {:?}", options);
104+
OPTIONS
105+
.set(options)
106+
.map_err(|_| mdbook::errors::Error::msg("could not initialize options"))?;
102107

103108
// first replace children in parents
104109
book.for_each_mut(|item| {
@@ -113,7 +118,6 @@ impl Preprocessor for LADPreprocessor {
113118
if LADPreprocessor::is_lad_file(chapter) {
114119
match LADPreprocessor::process_lad_chapter(
115120
context,
116-
&options,
117121
chapter,
118122
Some(parent),
119123
idx,
@@ -147,7 +151,6 @@ impl Preprocessor for LADPreprocessor {
147151
}
148152
let new_chapter = match LADPreprocessor::process_lad_chapter(
149153
context,
150-
&options,
151154
chapter,
152155
None,
153156
chapter

crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'a> Section<'a> {
286286
}
287287
SectionData::InstancesSummary => {
288288
let instances = self.ladfile.globals.iter().collect::<Vec<_>>();
289-
let types_directory = PathBuf::from("/").join(self.parent_path.join("types"));
289+
let types_directory = PathBuf::from("./types");
290290
vec![SectionItem::InstancesSummary {
291291
instances,
292292
ladfile: self.ladfile,
@@ -355,7 +355,7 @@ impl<'a> Section<'a> {
355355
]
356356
}
357357
SectionData::FunctionDetail { function } => {
358-
let types_directory = self.parent_path.join("../types");
358+
let types_directory = PathBuf::from("../types");
359359
vec![SectionItem::FunctionDetails {
360360
function,
361361
ladfile: self.ladfile,

crates/lad_backends/mdbook_lad_preprocessor/tests/books/example_ladfile/expected/parent/lad/functions/hello_world.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
| Name | Type | Documentation |
88
| --- | --- | --- |
9-
| **arg1** | [usize](parent/lad/functions/../types/usize.md) | No Documentation 🚧 |
9+
| **arg1** | [usize](../types/usize.md) | No Documentation 🚧 |
1010

1111
#### Returns
1212

1313
| Name | Type | Documentation |
1414
| --- | --- | --- |
15-
| **arg0** | [usize](parent/lad/functions/../types/usize.md) | No Documentation 🚧 |
15+
| **arg0** | [usize](../types/usize.md) | No Documentation 🚧 |
1616

crates/lad_backends/mdbook_lad_preprocessor/tests/books/example_ladfile/expected/parent/lad/globals.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ Instances containing actual accessible values\.
1010

1111
| Instance | Type |
1212
| --- | --- |
13-
| `my_non_static_instance` | Vec\<[UnitType](/parent/lad/types/unittype.md)\> |
14-
| `map` | HashMap\<[String](/parent/lad/types/string.md), [String](/parent/lad/types/string.md) \| [String](/parent/lad/types/string.md)\> |
13+
| `my_non_static_instance` | Vec\<[UnitType](./types/unittype.md)\> |
14+
| `map` | HashMap\<[String](./types/string.md), [String](./types/string.md) \| [String](./types/string.md)\> |
1515

1616
### Static Instances
1717

1818
Static type references, existing for the purpose of typed static function calls\.
1919

2020
| Instance | Type |
2121
| --- | --- |
22-
| `my_static_instance` | StructType\<[usize](/parent/lad/types/usize.md)\> |
22+
| `my_static_instance` | StructType\<[usize](./types/usize.md)\> |
2323

0 commit comments

Comments
 (0)