Add support for subpaths.

This commit is contained in:
Tom Alexander 2023-12-23 21:29:59 -05:00
parent 24218f2979
commit 3b63bbdfde
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 22 additions and 4 deletions

View File

@ -43,7 +43,10 @@ intermediate!(
#[derive(Debug, Clone)]
pub(crate) enum LinkTarget {
Raw(String),
Post { post_id: Option<String> },
Post {
post_id: Option<String>,
subpath: String,
},
}
impl LinkTarget {
@ -59,7 +62,18 @@ impl LinkTarget {
match parsed.scheme() {
"post" => {
let post_id = parsed.host_str().map(str::to_owned);
Ok(LinkTarget::Post { post_id })
let subpath = {
let subpath = parsed.path();
if subpath.starts_with('/') {
&subpath[1..]
} else {
subpath
}
};
Ok(LinkTarget::Post {
post_id,
subpath: subpath.to_owned(),
})
}
_ => Ok(LinkTarget::Raw(input.to_owned())),
}
@ -73,15 +87,19 @@ impl LinkTarget {
) -> Result<Option<String>, CustomError> {
match self {
LinkTarget::Raw(raw_link) => Ok(Some(raw_link.clone())),
LinkTarget::Post { post_id } => {
LinkTarget::Post { post_id, subpath } => {
let path = post_id
.as_ref()
.map(|post_id| {
let path_to_post = render_context
.config
.get_relative_path_to_post(post_id)
.join(subpath);
get_web_path(
render_context.config,
render_context.output_root_directory,
render_context.output_file,
render_context.config.get_relative_path_to_post(post_id),
path_to_post,
)
})
.map_or(Ok(None), |r| r.map(Some))?;