Skip to content

Relative links to "media" turn into html link #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cablespaghetti opened this issue Jun 7, 2025 · 2 comments
Open

Relative links to "media" turn into html link #281

cablespaghetti opened this issue Jun 7, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@cablespaghetti
Copy link

First of all, thanks so much for building this. I've been putting off resurrecting my blog because of many headaches related to upgrading Jekyll. This is exactly what I need in my life!

There are a number of images which link to high resolution versions of those images in my old posts that I'm migrating e.g.

[![MikroTik Hap ac2](/media/thumbs/homecloudpart1-mikrotik.webp)](/media/homecloudpart1-mikrotik.webp)

Unfortunately unless I make this an absolute link with the domain name etc I get this kind of link as a result with a non-existent HTML file.

http://localhost:8000/media-homecloudpart1-mikrotik-webp.html

For now I'm going to make everything absolute links but maybe this is a bug that can be rectified?

@cablespaghetti
Copy link
Author

Ok I just figured out that if I add a . to the beginning of the link it treats it as an image. Interesting.

@rochacbruno rochacbruno added the bug Something isn't working label Jun 9, 2025
@rochacbruno
Copy link
Owner

Hi @cablespaghetti thanks for reporting,

The problem is on

marmite/src/parser.rs

Lines 131 to 187 in f312fb9

pub fn fix_internal_links(html: &str) -> String {
let re = Regex::new(r#"<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>"#).unwrap();
re.replace_all(html, |caps: &regex::Captures| {
let link = caps.get(0).map_or("", |m| m.as_str());
let href = caps.get(1).map_or("", |m| m.as_str());
let text = caps.get(2).map_or("", |m| m.as_str());
if link.contains("class=\"anchor\"")
|| link.contains("data-footnote-ref")
|| link.contains("footnote-backref")
|| link.starts_with('/')
|| href.starts_with('.')
{
return link.to_string();
}
if let Ok(url) = Url::parse(href) {
if !url.scheme().is_empty() {
return link.to_string();
}
}
let new_href = if let Ok(parsed) = Url::parse(&format!("m://m/{href}")) {
let path = slugify(
parsed
.path()
.trim_start_matches('/')
.trim_end_matches(".md")
.trim_end_matches(".html"),
);
let fragment = match parsed.fragment() {
Some(f) => slugify(f),
None => String::new(),
};
let mut new_href = String::new();
if !path.is_empty() {
new_href.push_str(&format!("{path}.html"));
}
if !fragment.is_empty() {
new_href.push_str(&format!("#{fragment}"));
}
new_href
} else {
href.to_string()
};
let new_text = text
.trim_start_matches('#')
.trim_end_matches(".md")
.trim_end_matches(".html")
.replace('#', " > ");
link.replace(&format!("href=\"{href}\""), &format!("href=\"{new_href}\""))
.replace(&format!(">{text}</a>"), &format!(">{new_text}</a>"))
})
.to_string()
}

we have to update this function Regex to consider image paths, I think when you added . you bypassed the regexp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants