use serde_json::{Map, Value};
use crate::markdown_it::{JsError, MarkdownIt, StateCore, Token, js};
use super::super::utils::{contains_markdoc_tag_in_url, text_before_first_closed_tag};
use super::super::{LinkPluginOptions, ValidatedProtocols};
pub const INVALID_HREF_MESSAGE: &str =
"The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.";
pub const OPTIONS_KEY: &str = "$$markdocLinkPluginOptions";
fn push_href_error(token: &mut Token) {
let mut error = Map::new();
error.insert("id".into(), "href-format-invalid".into());
error.insert("level".into(), "error".into());
error.insert("message".into(), INVALID_HREF_MESSAGE.into());
token
.errors
.get_or_insert_with(Vec::new)
.push(Value::Object(error));
}
fn validated_protocols(md: &MarkdownIt) -> ValidatedProtocols {
match md
.options
.extra
.get(OPTIONS_KEY)
.and_then(|options| options.get("validatedProtocols"))
.and_then(Value::as_array)
{
Some(protocols) => ValidatedProtocols::Iterable(
protocols
.iter()
.filter_map(|p| p.as_str().map(str::to_owned))
.collect(),
),
None => ValidatedProtocols::NotIterable,
}
}
fn core(state: &mut StateCore) {
if super::super::thrown(state.env) {
return;
}
let protocols = validated_protocols(state.md);
for token in &mut state.tokens {
if token.kind != "inline" {
continue;
}
let content = js::to_utf16(&token.content);
let contains = match &protocols {
ValidatedProtocols::Iterable(protocols) => {
contains_markdoc_tag_in_url(&content, protocols)
}
ValidatedProtocols::NotIterable => {
if text_before_first_closed_tag(&content).is_some() {
state
.env
.throw(JsError::type_error("allowedProtocols is not iterable"));
return;
}
false
}
};
if contains {
push_href_error(token);
}
}
}
pub fn plugin(md: &mut MarkdownIt, options: &LinkPluginOptions) {
let mut stored = Map::new();
if let ValidatedProtocols::Iterable(protocols) = &options.validated_protocols {
stored.insert(
"validatedProtocols".into(),
Value::Array(protocols.iter().map(|p| Value::String(p.clone())).collect()),
);
}
md.options
.extra
.insert(OPTIONS_KEY.into(), Value::Object(stored));
md.core.ruler.push("link_url_validation", core, &[]);
}