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 crate::context::RenderContext;
|
||||||
use errors::{Context, Error, Result};
|
use errors::{Context, Error, Result};
|
||||||
use libs::pulldown_cmark::escape::escape_html;
|
use libs::pulldown_cmark::escape::escape_html;
|
||||||
|
use libs::regex::Regex;
|
||||||
use utils::site::resolve_internal_link;
|
use utils::site::resolve_internal_link;
|
||||||
use utils::slugs::slugify_anchors;
|
use utils::slugs::slugify_anchors;
|
||||||
use utils::table_of_contents::{make_table_of_contents, Heading};
|
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";
|
const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html";
|
||||||
static EMOJI_REPLACER: Lazy<EmojiReplacer> = Lazy::new(EmojiReplacer::new);
|
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.
|
/// Efficiently insert multiple element in their specified index.
|
||||||
/// The elements should sorted in ascending order by their 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;
|
*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)]
|
#[derive(Debug)]
|
||||||
pub struct Rendered {
|
pub struct Rendered {
|
||||||
pub body: String,
|
pub body: String,
|
||||||
@ -364,6 +377,14 @@ pub fn markdown_to_html(
|
|||||||
code_block = None;
|
code_block = None;
|
||||||
events.push(Event::Html("</code></pre>\n".into()));
|
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() => {
|
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"));
|
error = Some(Error::msg("There is a link that is missing a URL"));
|
||||||
events.push(Event::Start(Tag::Link(link_type, "#".into(), title)));
|
events.push(Event::Start(Tag::Link(link_type, "#".into(), title)));
|
||||||
|
@ -63,7 +63,7 @@ fn configurable_render(
|
|||||||
&tera,
|
&tera,
|
||||||
&config,
|
&config,
|
||||||
&config.default_language,
|
&config.default_language,
|
||||||
"",
|
"https://www.getzola.org/test/",
|
||||||
&permalinks,
|
&permalinks,
|
||||||
insert_anchor,
|
insert_anchor,
|
||||||
);
|
);
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: components/rendering/tests/markdown.rs
|
source: components/markdown/tests/markdown.rs
|
||||||
assertion_line: 29
|
|
||||||
expression: body
|
expression: body
|
||||||
|
|
||||||
---
|
---
|
||||||
<p>Hello world</p>
|
<p>Hello world</p>
|
||||||
<h1 id="hello-world">Hello world</h1>
|
<h1 id="hello-world">Hello world</h1>
|
||||||
@ -11,5 +9,5 @@ Hello
|
|||||||
world
|
world
|
||||||
Non rendered emoji :smile:
|
Non rendered emoji :smile:
|
||||||
<a href="image.jpg">a link</a>
|
<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>
|
<h1>some html</h1>
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: components/rendering/tests/shortcodes.rs
|
source: components/markdown/tests/shortcodes.rs
|
||||||
assertion_line: 63
|
|
||||||
expression: body
|
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