parent
9660612724
commit
bc6e40eede
15
README.md
15
README.md
|
@ -10,7 +10,6 @@ It uses the configured `git-repository-url` as the base.
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
* The `git` command line tool.
|
* The `git` command line tool.
|
||||||
* A configured git repository url in your `book.toml` configuration. See [Configuration](#configuration).
|
|
||||||
* Access to the git repository checkout while building your book.
|
* Access to the git repository checkout while building your book.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
@ -36,10 +35,22 @@ renderer = ["html"]
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[output.html]
|
[output.html]
|
||||||
# Required: Your repository URL used in the link.
|
# Optional: Your repository URL used in the link.
|
||||||
git-repository-url = "https://github.com/$user/$project"
|
git-repository-url = "https://github.com/$user/$project"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If `git-repository-url` is not configured the footer will not contain the commit and a link to it and instead only show the last changed date.
|
||||||
|
|
||||||
|
Without `git-repository-url` configured:
|
||||||
|
```HTML
|
||||||
|
<footer id="last-change">Last change: 2023-07-09</footer>
|
||||||
|
```
|
||||||
|
|
||||||
|
With `git-repository-url` configured:
|
||||||
|
```HTML
|
||||||
|
<footer id="last-change">Last change: 2023-07-09, commit: <a href="https://github.com/$user/$project/commit/$commit">0000000</a></footer>
|
||||||
|
```
|
||||||
|
|
||||||
To style the footer add a custom CSS file for your HTML output:
|
To style the footer add a custom CSS file for your HTML output:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
|
62
src/lib.rs
62
src/lib.rs
|
@ -21,26 +21,27 @@ impl Preprocessor for LastChanged {
|
||||||
log::debug!("Src root: {}", src_root.display());
|
log::debug!("Src root: {}", src_root.display());
|
||||||
log::debug!("Git root: {}", git_root.display());
|
log::debug!("Git root: {}", git_root.display());
|
||||||
|
|
||||||
let repository_url = match ctx.config.get("output.html.git-repository-url") {
|
let repository_string: Option<&str> = match ctx.config.get("output.html.git-repository-url")
|
||||||
None => {
|
{
|
||||||
log::error!("mdbook-last-changed was called, but no `output.html.git-repository-url` configured. Book is left unchanged.");
|
Some(val) => {
|
||||||
return Ok(book);
|
let url = match val {
|
||||||
}
|
toml::Value::String(s) => s,
|
||||||
Some(url) => url,
|
_ => {
|
||||||
};
|
log::trace!("git-repository-url is not a string: {val:?}");
|
||||||
let repository_url = match repository_url {
|
return Ok(book);
|
||||||
toml::Value::String(s) => s,
|
}
|
||||||
_ => {
|
};
|
||||||
log::trace!("git-repository-url is not a string: {repository_url:?}");
|
log::debug!("Repository URL: {}", url);
|
||||||
return Ok(book);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
log::debug!("Repository URL: {}", repository_url);
|
|
||||||
|
|
||||||
if !repository_url.contains("github.com") {
|
if !url.contains("github.com") {
|
||||||
log::trace!("git-repository-url is not a GitHub URL: {repository_url:?}");
|
log::trace!("git-repository-url is not a GitHub URL: {url:?}");
|
||||||
return Ok(book);
|
return Ok(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Some(&url)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
book.for_each_mut(|item: &mut BookItem| {
|
book.for_each_mut(|item: &mut BookItem| {
|
||||||
|
@ -51,7 +52,7 @@ impl Preprocessor for LastChanged {
|
||||||
|
|
||||||
if let BookItem::Chapter(ref mut chapter) = *item {
|
if let BookItem::Chapter(ref mut chapter) = *item {
|
||||||
res = Some(
|
res = Some(
|
||||||
last_changed(&git_root, &src_root, repository_url, chapter).map(|md| {
|
last_changed(&git_root, &src_root, repository_string, chapter).map(|md| {
|
||||||
chapter.content = md;
|
chapter.content = md;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -65,7 +66,7 @@ impl Preprocessor for LastChanged {
|
||||||
fn last_changed(
|
fn last_changed(
|
||||||
git_root: &Path,
|
git_root: &Path,
|
||||||
src_root: &Path,
|
src_root: &Path,
|
||||||
base_url: &str,
|
base_url: Option<&str>,
|
||||||
chapter: &mut Chapter,
|
chapter: &mut Chapter,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let content = &chapter.content;
|
let content = &chapter.content;
|
||||||
|
@ -94,13 +95,18 @@ fn last_changed(
|
||||||
|
|
||||||
let modification = get_last_modification(git_root, &path);
|
let modification = get_last_modification(git_root, &path);
|
||||||
let text = match modification {
|
let text = match modification {
|
||||||
Ok((date, commit)) => {
|
Ok((date, commit)) => match base_url {
|
||||||
let url = format!("{}/commit/{}", base_url, commit);
|
Some(url) => {
|
||||||
format!(
|
let url = format!("{}/commit/{}", url, commit);
|
||||||
"Last change: {}, commit: <a href=\"{}\">{}</a>",
|
format!(
|
||||||
date, url, commit
|
"Last change: {}, commit: <a href=\"{}\">{}</a>",
|
||||||
)
|
date, url, commit
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
format!("Last change: {}", date)
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::trace!("No modification found for {path:?}. Error: {e:?}");
|
log::trace!("No modification found for {path:?}. Error: {e:?}");
|
||||||
return Ok(content.into());
|
return Ok(content.into());
|
||||||
|
|
Loading…
Reference in New Issue