Manually integrate changes from c43aa69e2b4f87a0c76eb8fd37c221042014d3c1

This was a force-pushed commit that probably doesn't exist for long before
beign collected.

Co-authored-by: Filipp <samoylovfp@gmail.com>
This commit is contained in:
Sebastian Thiel
2023-05-08 07:53:14 +02:00
parent fc1754fbfa
commit 5cccc5ba83
3 changed files with 21 additions and 8 deletions

View File

@@ -16,3 +16,4 @@ test = false
[dependencies]
pulldown-cmark-to-cmark = "10.0.0"
pulldown-cmark = "0.9.0"
url = "2.3.1"

View File

@@ -14,15 +14,25 @@ fn main() {
let mut output = String::with_capacity(2048);
let url_base = std::env::var("URL_BASE").unwrap_or_default();
// FIXME: for urls starting with /, use only the netloc
let url_base = url_base
.strip_suffix('/')
.map(String::from)
.unwrap_or(url_base);
fn fix_url<'a>(base: &str, url: CowStr<'a>) -> CowStr<'a> {
let url_root = if url_base.is_empty() {
String::new()
} else {
let parsed = url::Url::parse(&url_base).unwrap();
let scheme = parsed.scheme();
let host = parsed.host_str().unwrap_or_default();
let port = parsed.port().map(|p| format!(":{p}")).unwrap_or_default();
format!("{scheme}://{host}{port}")
};
fn fix_url<'a>(root: &str, base: &str, url: CowStr<'a>) -> CowStr<'a> {
if url.starts_with('/') {
format!("{base}{url}").into()
format!("{root}{url}").into()
} else if url.starts_with("..") {
format!("{base}/{url}").into()
} else {
@@ -40,14 +50,16 @@ fn main() {
CodeBlock(pulldown_cmark::CodeBlockKind::Indented) => Start(CodeBlock(
pulldown_cmark::CodeBlockKind::Fenced("text".into()),
)),
Link(lt, url, title) => {
Start(Link(*lt, fix_url(&url_base, url.clone()), title.clone()))
}
Link(lt, url, title) => Start(Link(
*lt,
fix_url(&url_root, &url_base, url.clone()),
title.clone(),
)),
_ => e,
}
}
End(Tag::Link(lt, url, title)) => {
End(Tag::Link(lt, fix_url(&url_base, url), title))
End(Tag::Link(lt, fix_url(&url_root, &url_base, url), title))
}
_ => e,
}