-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for image and link URL rewriting
- Loading branch information
Showing
5 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::sync::Arc; | ||
|
||
use super::*; | ||
|
||
const IMAGE_PROXY: &'static str = "https://safe.example.com?url="; | ||
const LINK_NOREFER: &'static str = "https://safe.example.com/norefer?url="; | ||
|
||
struct Rewriter { | ||
prefix: &'static str, | ||
} | ||
|
||
impl URLRewriter for Rewriter { | ||
fn to_html(&self, url: &str) -> String { | ||
format!("{}{}", self.prefix, url) | ||
} | ||
} | ||
|
||
#[test] | ||
fn image_url_rewriter() { | ||
html_opts_i( | ||
"![](http://unsafe.example.com/bad.png)", | ||
"<p><img src=\"https://safe.example.com?url=http://unsafe.example.com/bad.png\" alt=\"\" /></p>\n", | ||
true, | ||
|opts| opts.extension.image_url_rewriter = Some(Arc::new(Rewriter{prefix: IMAGE_PROXY})) | ||
); | ||
} | ||
|
||
#[test] | ||
fn link_url_rewriter() { | ||
html_opts_i( | ||
"[my link](http://unsafe.example.com/bad)", | ||
"<p><a href=\"https://safe.example.com/norefer?url=http://unsafe.example.com/bad\">my link</a></p>\n", | ||
true, | ||
|opts| opts.extension.link_url_rewriter = Some(Arc::new(Rewriter{prefix: LINK_NOREFER})) | ||
); | ||
} |