From 04ee5c85a39fa75c9f78d9a47343329015d7aa3d Mon Sep 17 00:00:00 2001
From: Vincent Prouillet
Date: Fri, 10 Jun 2022 20:41:22 +0200
Subject: [PATCH] Reenable link rewriting for colocated assets
All credits go to https://github.com/getzola/zola/pull/1779
---
components/markdown/src/markdown.rs | 21 +++++++++++++++++++
components/markdown/tests/common.rs | 2 +-
.../markdown__can_render_basic_markdown.snap | 6 ++----
...codes__can_grab_lang_in_md_shortcodes.snap | 6 ++----
4 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/components/markdown/src/markdown.rs b/components/markdown/src/markdown.rs
index 1e33d3c1..4b1c9bd7 100644
--- a/components/markdown/src/markdown.rs
+++ b/components/markdown/src/markdown.rs
@@ -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 = "";
const ANCHOR_LINK_TEMPLATE: &str = "anchor-link.html";
static EMOJI_REPLACER: Lazy = 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 = 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(input: &mut Vec, 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("\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)));
diff --git a/components/markdown/tests/common.rs b/components/markdown/tests/common.rs
index a8513d8f..aaef9784 100644
--- a/components/markdown/tests/common.rs
+++ b/components/markdown/tests/common.rs
@@ -63,7 +63,7 @@ fn configurable_render(
&tera,
&config,
&config.default_language,
- "",
+ "https://www.getzola.org/test/",
&permalinks,
insert_anchor,
);
diff --git a/components/markdown/tests/snapshots/markdown__can_render_basic_markdown.snap b/components/markdown/tests/snapshots/markdown__can_render_basic_markdown.snap
index 97aa33eb..e839beab 100644
--- a/components/markdown/tests/snapshots/markdown__can_render_basic_markdown.snap
+++ b/components/markdown/tests/snapshots/markdown__can_render_basic_markdown.snap
@@ -1,8 +1,6 @@
---
-source: components/rendering/tests/markdown.rs
-assertion_line: 29
+source: components/markdown/tests/markdown.rs
expression: body
-
---
Hello world
Hello world
@@ -11,5 +9,5 @@ Hello
world
Non rendered emoji :smile:
a link
-
+
some html
diff --git a/components/markdown/tests/snapshots/shortcodes__can_grab_lang_in_md_shortcodes.snap b/components/markdown/tests/snapshots/shortcodes__can_grab_lang_in_md_shortcodes.snap
index 897f8b5a..5e96f764 100644
--- a/components/markdown/tests/snapshots/shortcodes__can_grab_lang_in_md_shortcodes.snap
+++ b/components/markdown/tests/snapshots/shortcodes__can_grab_lang_in_md_shortcodes.snap
@@ -1,8 +1,6 @@
---
-source: components/rendering/tests/shortcodes.rs
-assertion_line: 63
+source: components/markdown/tests/shortcodes.rs
expression: body
-
---
-
+