forked from alemi/upub
feat(mdhtml): allow setting media proxy for imgs
This commit is contained in:
parent
43aea48816
commit
03314b1615
2 changed files with 24 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mdhtml"
|
name = "mdhtml"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = [ "alemi <me@alemi.dev>" ]
|
authors = [ "alemi <me@alemi.dev>" ]
|
||||||
description = "Parse and display a markdown-like HTML subset"
|
description = "Parse and display a markdown-like HTML subset"
|
||||||
|
|
|
@ -4,7 +4,10 @@ use comrak::{markdown_to_html, Options};
|
||||||
|
|
||||||
/// In our case, our sink only contains a tokens vector
|
/// In our case, our sink only contains a tokens vector
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
struct Sink(String);
|
struct Sink {
|
||||||
|
pub media_proxy: Option<String>,
|
||||||
|
pub buffer: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl TokenSink for Sink {
|
impl TokenSink for Sink {
|
||||||
type Handle = ();
|
type Handle = ();
|
||||||
|
@ -22,21 +25,28 @@ impl TokenSink for Sink {
|
||||||
| "img" | "a"
|
| "img" | "a"
|
||||||
) { return TokenSinkResult::Continue } // skip this tag
|
) { return TokenSinkResult::Continue } // skip this tag
|
||||||
|
|
||||||
self.0.push('<');
|
self.buffer.push('<');
|
||||||
|
|
||||||
if !tag.self_closing && matches!(tag.kind, TagKind::EndTag) {
|
if !tag.self_closing && matches!(tag.kind, TagKind::EndTag) {
|
||||||
self.0.push('/');
|
self.buffer.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
self.0.push_str(tag.name.as_ref());
|
self.buffer.push_str(tag.name.as_ref());
|
||||||
|
|
||||||
if !matches!(tag.kind, TagKind::EndTag) {
|
if !matches!(tag.kind, TagKind::EndTag) {
|
||||||
match tag.name.as_ref() {
|
match tag.name.as_ref() {
|
||||||
"img" => for attr in tag.attrs {
|
"img" => for attr in tag.attrs {
|
||||||
match attr.name.local.as_ref() {
|
match attr.name.local.as_ref() {
|
||||||
"src" => self.0.push_str(&format!(" src=\"{}\"", attr.value.as_ref())),
|
"src" => {
|
||||||
"title" => self.0.push_str(&format!(" title=\"{}\"", attr.value.as_ref())),
|
let src = if let Some(ref proxy) = self.media_proxy {
|
||||||
"alt" => self.0.push_str(&format!(" alt=\"{}\"", attr.value.as_ref())),
|
format!("{proxy}{}", attr.value.as_ref())
|
||||||
|
} else {
|
||||||
|
attr.value.to_string()
|
||||||
|
};
|
||||||
|
self.buffer.push_str(&format!(" src=\"{src}\""))
|
||||||
|
},
|
||||||
|
"title" => self.buffer.push_str(&format!(" title=\"{}\"", attr.value.as_ref())),
|
||||||
|
"alt" => self.buffer.push_str(&format!(" alt=\"{}\"", attr.value.as_ref())),
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -44,13 +54,13 @@ impl TokenSink for Sink {
|
||||||
let any_attr = !tag.attrs.is_empty();
|
let any_attr = !tag.attrs.is_empty();
|
||||||
for attr in tag.attrs {
|
for attr in tag.attrs {
|
||||||
match attr.name.local.as_ref() {
|
match attr.name.local.as_ref() {
|
||||||
"href" => self.0.push_str(&format!(" href=\"{}\"", attr.value.as_ref())),
|
"href" => self.buffer.push_str(&format!(" href=\"{}\"", attr.value.as_ref())),
|
||||||
"title" => self.0.push_str(&format!(" title=\"{}\"", attr.value.as_ref())),
|
"title" => self.buffer.push_str(&format!(" title=\"{}\"", attr.value.as_ref())),
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if any_attr {
|
if any_attr {
|
||||||
self.0.push_str(" rel=\"nofollow noreferrer\" target=\"_blank\"");
|
self.buffer.push_str(" rel=\"nofollow noreferrer\" target=\"_blank\"");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
@ -58,12 +68,12 @@ impl TokenSink for Sink {
|
||||||
}
|
}
|
||||||
|
|
||||||
if tag.self_closing {
|
if tag.self_closing {
|
||||||
self.0.push('/');
|
self.buffer.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
self.0.push('>');
|
self.buffer.push('>');
|
||||||
},
|
},
|
||||||
Token::CharacterTokens(txt) => self.0.push_str(txt.as_ref()),
|
Token::CharacterTokens(txt) => self.buffer.push_str(txt.as_ref()),
|
||||||
Token::CommentToken(_) => {},
|
Token::CommentToken(_) => {},
|
||||||
Token::DoctypeToken(_) => {},
|
Token::DoctypeToken(_) => {},
|
||||||
Token::NullCharacterToken => {},
|
Token::NullCharacterToken => {},
|
||||||
|
|
Loading…
Reference in a new issue