Reenable link rewriting for colocated assets
All credits go to https://github.com/getzola/zola/pull/1779
This commit is contained in:
parent
e8fe51a344
commit
04ee5c85a3
@ -9,6 +9,7 @@ use libs::tera;
|
||||
use crate::context::RenderContext;
|
||||
use errors::{Context, Error, Result};
|
||||
use libs::pulldown_cmark::escape::escape_html;
|
||||
use libs::regex::Regex;
|
||||
use utils::site::resolve_internal_link;
|
||||
use utils::slugs::slugify_anchors;
|
||||
use utils::table_of_contents::{make_table_of_contents, Heading};
|
||||
@ -22,6 +23,13 @@ const CONTINUE_READING: &str = "<span id=\"continue-reading\"></span>";
|
||||
const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html";
|
||||
static EMOJI_REPLACER: Lazy<EmojiReplacer> = Lazy::new(EmojiReplacer::new);
|
||||
|
||||
/// Although there exists [a list of registered URI schemes][uri-schemes], a link may use arbitrary,
|
||||
/// private schemes. This regex checks if the given string starts with something that just looks
|
||||
/// like a scheme, i.e., a case-insensitive identifier followed by a colon.
|
||||
///
|
||||
/// [uri-schemes]: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
||||
static STARTS_WITH_SCHEMA_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[0-9A-Za-z\-]+:").unwrap());
|
||||
|
||||
/// Efficiently insert multiple element in their specified index.
|
||||
/// The elements should sorted in ascending order by their index.
|
||||
///
|
||||
@ -42,6 +50,11 @@ fn insert_many<T>(input: &mut Vec<T>, elem_to_insert: Vec<(usize, T)>) {
|
||||
*input = inserted;
|
||||
}
|
||||
|
||||
/// Colocated asset links refers to the files in the same directory.
|
||||
fn is_colocated_asset_link(link: &str) -> bool {
|
||||
!link.starts_with('/') && !link.starts_with('#') && !STARTS_WITH_SCHEMA_RE.is_match(link)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Rendered {
|
||||
pub body: String,
|
||||
@ -364,6 +377,14 @@ pub fn markdown_to_html(
|
||||
code_block = None;
|
||||
events.push(Event::Html("</code></pre>\n".into()));
|
||||
}
|
||||
Event::Start(Tag::Image(link_type, src, title)) => {
|
||||
if is_colocated_asset_link(&src) {
|
||||
let link = format!("{}{}", context.current_page_permalink, &*src);
|
||||
events.push(Event::Start(Tag::Image(link_type, link.into(), title)));
|
||||
} else {
|
||||
events.push(Event::Start(Tag::Image(link_type, src, title)));
|
||||
}
|
||||
}
|
||||
Event::Start(Tag::Link(link_type, link, title)) if link.is_empty() => {
|
||||
error = Some(Error::msg("There is a link that is missing a URL"));
|
||||
events.push(Event::Start(Tag::Link(link_type, "#".into(), title)));
|
||||
|
@ -63,7 +63,7 @@ fn configurable_render(
|
||||
&tera,
|
||||
&config,
|
||||
&config.default_language,
|
||||
"",
|
||||
"https://www.getzola.org/test/",
|
||||
&permalinks,
|
||||
insert_anchor,
|
||||
);
|
||||
|
@ -1,8 +1,6 @@
|
||||
---
|
||||
source: components/rendering/tests/markdown.rs
|
||||
assertion_line: 29
|
||||
source: components/markdown/tests/markdown.rs
|
||||
expression: body
|
||||
|
||||
---
|
||||
<p>Hello world</p>
|
||||
<h1 id="hello-world">Hello world</h1>
|
||||
@ -11,5 +9,5 @@ Hello
|
||||
world
|
||||
Non rendered emoji :smile:
|
||||
<a href="image.jpg">a link</a>
|
||||
<img src="image.jpg" alt="alt text" /></p>
|
||||
<img src="https://www.getzola.org/test/image.jpg" alt="alt text" /></p>
|
||||
<h1>some html</h1>
|
||||
|
@ -1,8 +1,6 @@
|
||||
---
|
||||
source: components/rendering/tests/shortcodes.rs
|
||||
assertion_line: 63
|
||||
source: components/markdown/tests/shortcodes.rs
|
||||
expression: body
|
||||
|
||||
---
|
||||
<p><img src="cover.en.png" alt="Book cover in en" /></p>
|
||||
<p><img src="https://www.getzola.org/test/cover.en.png" alt="Book cover in en" /></p>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user