diff --git a/src/book/book.rs b/src/book/book.rs index c0253b7a..98156f30 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf}; use super::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem}; use crate::config::BuildConfig; use crate::errors::*; +use crate::preprocess::SummaryPreprocessor; use crate::utils::bracket_escape; use log::debug; use serde::{Deserialize, Serialize}; @@ -21,7 +22,9 @@ pub fn load_book>(src_dir: P, cfg: &BuildConfig) -> Result .with_context(|| format!("Couldn't open SUMMARY.md in {:?} directory", src_dir))? .read_to_string(&mut summary_content)?; - let summary = parse_summary(&summary_content) + let preprocessed_summary_content = SummaryPreprocessor::resolve(src_dir, &summary_content); + + let summary = parse_summary(&preprocessed_summary_content) .with_context(|| format!("Summary parsing failed for file={:?}", summary_md))?; if cfg.create_missing { diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 0af21196..9c467074 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -71,7 +71,7 @@ impl Preprocessor for LinkPreprocessor { } } -fn replace_all( +pub(crate) fn replace_all( s: &str, path: P1, source: P2, diff --git a/src/preprocess/mod.rs b/src/preprocess/mod.rs index df01a3db..2bcbefb5 100644 --- a/src/preprocess/mod.rs +++ b/src/preprocess/mod.rs @@ -3,10 +3,12 @@ pub use self::cmd::CmdPreprocessor; pub use self::index::IndexPreprocessor; pub use self::links::LinkPreprocessor; +pub use self::summary::SummaryPreprocessor; mod cmd; mod index; mod links; +mod summary; use crate::book::Book; use crate::config::Config; diff --git a/src/preprocess/summary.rs b/src/preprocess/summary.rs new file mode 100644 index 00000000..792dab8e --- /dev/null +++ b/src/preprocess/summary.rs @@ -0,0 +1,13 @@ +use std::path::Path; + +/// A preprocessor dedicated just to the summary to resolve links +#[derive(Default)] +pub struct SummaryPreprocessor; + +impl SummaryPreprocessor { + /// Preprocess Summary to resolve links. + pub fn resolve(src_dir: &Path, content: &str) -> String { + let mut title = String::from("SUMMARY.md"); + super::links::replace_all(content, src_dir, src_dir, 0, &mut title) + } +}