use crate::markdown_it::js;
use crate::next::schema::value::{self, JsString, TagValue, Value};
use crate::next::stack;
use crate::types::{JsObject, MarkdocError, Result};
use indexmap::IndexMap;
use std::borrow::Cow;
pub const NODE_LIMITS: [(&str, usize, usize); 5] = [
("tags", 2674, 4823),
("arrays", 2674, 4823),
("attr_arrays", 1843, 8643),
("attr_objects", 3799, 4820),
("attr_tags", 1383, 2411),
];
#[derive(Clone, Copy)]
pub(crate) struct Cost(pub u64, pub u64);
const KB: u64 = 1024;
const COST_RENDER: Cost = Cost(395050272, 4 * KB);
const COST_RENDER_ATTR: Cost = Cost(485851254, 6 * KB);
const COST_DEEP_OBJECT: Cost = Cost(277902093, 5 * KB / 2);
const COST_DEEP_ARRAY: Cost = Cost(572763223, 5 * KB / 2);
pub(crate) fn level(cost: Cost) -> Result<stack::Level> {
stack::enter(cost.0, cost.1).ok_or_else(|| throw(value::RANGE_ERROR))
}
pub trait React {
type Element;
type Component: Clone;
fn create_element(
&mut self,
element_type: ElementType<'_, Self::Component>,
props: Option<Prop<'_, Self::Element>>,
children: Vec<Rendered<'_, Self::Element>>,
) -> Self::Element;
}
#[derive(Clone, Debug)]
pub enum Name<'a> {
String(&'a JsString),
Value(Cow<'a, Value>),
}
impl Name<'_> {
pub fn to_value(&self) -> Value {
match self {
Name::String(s) => Value::String((*s).clone()),
Name::Value(v) => v.as_ref().clone(),
}
}
pub fn as_js_str(&self) -> Option<&JsString> {
match self {
Name::String(s) => Some(s),
Name::Value(v) => v.as_js_str(),
}
}
}
#[derive(Clone, Debug)]
pub enum ElementType<'a, C> {
Fragment,
Name(Name<'a>),
Component(Option<C>),
Inherited(String),
}
pub type Props<'a, E> = Vec<(Cow<'a, str>, Prop<'a, E>)>;
#[derive(Clone, Debug)]
pub enum Prop<'a, E> {
Value(Cow<'a, Value>),
Array(Vec<Prop<'a, E>>),
Object(Props<'a, E>),
Element(E),
}
impl<E> Prop<'_, E> {
fn into_owned(self) -> Prop<'static, E> {
match self {
Prop::Value(v) => Prop::Value(Cow::Owned(v.into_owned())),
Prop::Array(items) => Prop::Array(items.into_iter().map(Prop::into_owned).collect()),
Prop::Object(props) => Prop::Object(
props
.into_iter()
.map(|(k, v)| (Cow::Owned(k.into_owned()), v.into_owned()))
.collect(),
),
Prop::Element(e) => Prop::Element(e),
}
}
}
#[derive(Clone, Debug)]
pub enum Rendered<'a, E> {
Element(E),
Value(Cow<'a, Value>),
}
pub enum Components<'a, C> {
Map(&'a IndexMap<String, C>),
Function(&'a dyn Fn(&JsString) -> Option<C>),
}
impl<C> Clone for Components<'_, C> {
fn clone(&self) -> Self {
*self
}
}
impl<C> Copy for Components<'_, C> {}
pub type ResolveTagName<'a, C> =
&'a dyn for<'n> Fn(&Name<'n>, Components<'_, C>) -> Result<ElementType<'n, C>>;
pub struct RenderOptions<'a, C> {
pub components: Option<Components<'a, C>>,
pub resolve_tag_name: Option<ResolveTagName<'a, C>>,
}
impl<C> Default for RenderOptions<'_, C> {
fn default() -> Self {
RenderOptions {
components: None,
resolve_tag_name: None,
}
}
}
fn throw(message: impl Into<String>) -> MarkdocError {
MarkdocError::TransformError(message.into())
}
pub fn tag_name<'n, C: Clone>(
name: &Name<'n>,
components: Components<'_, C>,
) -> Result<ElementType<'n, C>> {
let Some(string) = name.as_js_str() else {
return Ok(ElementType::Name(name.clone()));
};
let Some(first) = string.code_units().next() else {
return Err(throw(
"TypeError: Cannot read properties of undefined (reading 'toUpperCase')",
));
};
let changes = match u8::try_from(first) {
Ok(ascii) if ascii.is_ascii() => ascii.is_ascii_lowercase(),
_ => js::to_upper_case(&[first]) != [first],
};
if changes {
return Ok(ElementType::Name(name.clone()));
}
Ok(match components {
Components::Function(f) => ElementType::Component(f(string)),
Components::Map(map) => match string.as_str() {
Some(n) if map.contains_key(n) => ElementType::Component(map.get(n).cloned()),
Some(n) if value::is_object_prototype_name(n) => ElementType::Inherited(n.into()),
_ => ElementType::Component(None),
},
})
}
pub fn render<'a, R: React>(
node: &'a Value,
react: &mut R,
options: &RenderOptions<'_, R::Component>,
) -> Result<Rendered<'a, R::Element>> {
let empty = IndexMap::new();
let mut renderer = Renderer {
react,
components: options.components.unwrap_or(Components::Map(&empty)),
resolve_tag_name: options.resolve_tag_name,
};
renderer.render(node, COST_RENDER)
}
struct Renderer<'r, 'o, R: React> {
react: &'r mut R,
components: Components<'o, R::Component>,
resolve_tag_name: Option<ResolveTagName<'o, R::Component>>,
}
impl<R: React> Renderer<'_, '_, R> {
fn deep_render<'a>(&mut self, value: &'a Value) -> Result<Prop<'a, R::Element>> {
let cost = match value {
Value::Array(_) => COST_DEEP_ARRAY,
Value::Object(_) if !TagValue::is_tag(value) => COST_DEEP_OBJECT,
Value::Object(_) | Value::Tag(_) => {
return Ok(match self.render(value, COST_RENDER_ATTR)? {
Rendered::Element(e) => Prop::Element(e),
Rendered::Value(v) => Prop::Value(v),
});
}
_ => return Ok(Prop::Value(Cow::Borrowed(value))),
};
let _level = level(cost)?;
stack::grow(|| match value {
Value::Array(items) => {
let mut out = Vec::with_capacity(items.present_len());
for (_, item) in items.present() {
out.push(self.deep_render(item)?);
}
Ok(Prop::Array(out))
}
Value::Object(o) => self.deep_render_entries(o.iter().map(|(k, v)| (k.as_str(), v))),
_ => unreachable!("an array or an object"),
})
}
fn deep_render_entries<'a>(
&mut self,
entries: impl Iterator<Item = (&'a str, &'a Value)>,
) -> Result<Prop<'a, R::Element>> {
let mut output: Props<'a, R::Element> = Vec::new();
for (k, v) in entries {
let rendered = self.deep_render(v)?;
if k != "__proto__" {
set(&mut output, Cow::Borrowed(k), rendered);
}
}
Ok(Prop::Object(output))
}
fn render<'a>(&mut self, node: &'a Value, cost: Cost) -> Result<Rendered<'a, R::Element>> {
if !matches!(node, Value::Array(_)) && !TagValue::is_tag(node) {
return Ok(Rendered::Value(Cow::Borrowed(node)));
}
let _level = level(cost)?;
stack::grow(|| self.render_inner(node))
}
fn render_inner<'a>(&mut self, node: &'a Value) -> Result<Rendered<'a, R::Element>> {
if let Value::Array(items) = node {
let mut children = Vec::with_capacity(items.present_len());
for (_, item) in items.present() {
children.push(self.render(item, COST_RENDER)?);
}
let element = self
.react
.create_element(ElementType::Fragment, None, children);
return Ok(Rendered::Element(element));
}
if !TagValue::is_tag(node) {
return Ok(Rendered::Value(Cow::Borrowed(node)));
}
let Destructured {
name,
mut attrs,
class_name,
children,
} = destructure(node)?;
if class_name.to_boolean() {
set(&mut attrs, Cow::Borrowed("className"), class_name);
}
let element_type = match self.resolve_tag_name {
Some(resolve) => resolve(&name, self.components)?,
None => tag_name(&name, self.components)?,
};
let props = if attrs.is_empty() {
None
} else {
Some(self.deep_render_attrs(attrs)?)
};
let items = children.items()?;
let mut rendered = Vec::with_capacity(items.len());
for child in items {
rendered.push(self.render(child, COST_RENDER)?);
}
let element = self.react.create_element(element_type, props, rendered);
Ok(Rendered::Element(element))
}
fn deep_render_attrs<'a>(&mut self, attrs: Attrs<'a>) -> Result<Prop<'a, R::Element>> {
let is_tag = attrs
.iter()
.any(|(k, v)| k == "$$mdtype" && matches!(v.as_str(), Some("Tag")));
if is_tag || attrs.iter().any(|(_, v)| matches!(v, Cow::Owned(_))) {
let object = Value::object(
attrs
.into_iter()
.map(|(k, v)| (k.into_owned(), v.into_owned()))
.collect::<JsObject<Value>>(),
);
return Ok(self.deep_render(&object)?.into_owned());
}
let _level = level(COST_DEEP_OBJECT)?;
stack::grow(|| {
let mut output: Props<'a, R::Element> = Vec::with_capacity(attrs.len());
for (k, v) in attrs {
let Cow::Borrowed(v) = v else {
unreachable!("borrowed values only")
};
let rendered = self.deep_render(v)?;
if k != "__proto__" {
set(&mut output, k, rendered);
}
}
Ok(Prop::Object(output))
})
}
}
fn set<'a, T>(object: &mut Vec<(Cow<'a, str>, T)>, key: Cow<'a, str>, value: T) {
match object.iter_mut().find(|(k, _)| *k == key) {
Some((_, v)) => *v = value,
None => object.push((key, value)),
}
}
pub(crate) enum Children<'a> {
Items(&'a [Value]),
Other(&'a Value),
}
impl<'a> Children<'a> {
pub(crate) fn items(&self) -> Result<&'a [Value]> {
match self {
Children::Items(items) => Ok(items),
Children::Other(children @ (Value::Undefined | Value::Null)) => Err(throw(format!(
"TypeError: Cannot read properties of {} (reading 'map')",
if children.is_undefined() {
"undefined"
} else {
"null"
}
))),
Children::Other(_) => Err(throw("TypeError: children.map is not a function")),
}
}
}
pub(crate) struct Destructured<'a> {
pub name: Name<'a>,
pub attrs: Attrs<'a>,
pub class_name: Cow<'a, Value>,
pub children: Children<'a>,
}
pub(crate) fn destructure(node: &Value) -> Result<Destructured<'_>> {
const UNDEFINED: &Value = &Value::Undefined;
let (name, attributes, children): (Name<'_>, Option<&Value>, Children<'_>) = match node {
Value::Tag(tag) => {
let (class_name, attrs) = attributes_of(&tag.attributes);
return Ok(Destructured {
name: match &tag.name {
Value::String(name) => Name::String(name),
other => Name::Value(Cow::Borrowed(other)),
},
attrs,
class_name,
children: Children::Items(&tag.children),
});
}
Value::Object(o) => (
Name::Value(Cow::Borrowed(o.get("name").unwrap_or(UNDEFINED))),
o.get("attributes"),
match o.get("children") {
None | Some(Value::Undefined) => Children::Items(&[]),
Some(array @ Value::Array(items)) => match items.as_dense() {
Some(items) => Children::Items(items),
None => Children::Other(array),
},
Some(other) => Children::Other(other),
},
),
_ => unreachable!("a tag is a Tag or an object"),
};
let (class_name, attrs) = match attributes {
None | Some(Value::Undefined) => (Cow::Borrowed(UNDEFINED), Vec::new()),
Some(Value::Null) => {
return Err(throw(
"TypeError: Cannot destructure property 'class' of '{}' as it is null.",
));
}
Some(Value::Object(o)) => attributes_of(o),
Some(other) => {
let class_name = value::get_property(other, "class").map_err(throw)?;
let attrs = value::own_entries(other)
.into_iter()
.filter(|(k, _)| k != "class")
.map(|(k, v)| (Cow::Owned(k), Cow::Owned(v)))
.collect();
(Cow::Owned(class_name), attrs)
}
};
Ok(Destructured {
name,
attrs,
class_name,
children,
})
}
pub(crate) type Attrs<'a> = Vec<(Cow<'a, str>, Cow<'a, Value>)>;
fn attributes_of(attributes: &value::Object) -> (Cow<'_, Value>, Attrs<'_>) {
const UNDEFINED: &Value = &Value::Undefined;
let class_name = match attributes.get("class") {
Some(class) => Cow::Borrowed(class),
None if attributes.proto().is_object_prototype() => Cow::Borrowed(UNDEFINED),
None => Cow::Owned(value::object_get(attributes, "class").unwrap_or(Value::Undefined)),
};
let attrs = attributes
.iter()
.filter(|(k, _)| *k != "class")
.map(|(k, v)| (Cow::Borrowed(k.as_str()), Cow::Borrowed(v)))
.collect();
(class_name, attrs)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::next::index::{self, thrown};
use crate::next::parser::ParserArgs;
use crate::next::schema::Config;
#[derive(Default)]
struct Recorder(Vec<String>);
fn text(value: &Value) -> String {
value::json_stringify(value, "")
.unwrap()
.unwrap_or("undefined".into())
}
fn prop(p: &Prop<'_, usize>) -> String {
match p {
Prop::Value(v) => text(v),
Prop::Element(e) => format!("el{e}"),
Prop::Array(items) => {
format!("[{}]", items.iter().map(prop).collect::<Vec<_>>().join(","))
}
Prop::Object(o) => format!(
"{{{}}}",
o.iter()
.map(|(k, v)| format!("{k}:{}", prop(v)))
.collect::<Vec<_>>()
.join(",")
),
}
}
impl React for Recorder {
type Element = usize;
type Component = String;
fn create_element(
&mut self,
element_type: ElementType<'_, String>,
props: Option<Prop<'_, usize>>,
children: Vec<Rendered<'_, usize>>,
) -> usize {
let t = match element_type {
ElementType::Fragment => "Fragment".to_string(),
ElementType::Name(n) => text(&n.to_value()),
ElementType::Component(c) => c.unwrap_or("undefined".into()),
ElementType::Inherited(name) => format!("fn {name}"),
};
let children: Vec<String> = children
.iter()
.map(|c| match c {
Rendered::Element(e) => format!("el{e}"),
Rendered::Value(v) => text(v),
})
.collect();
self.0.push(format!(
"{t} {} [{}]",
props.as_ref().map_or("null".into(), prop),
children.join(",")
));
self.0.len() - 1
}
}
fn tree(json: serde_json::Value) -> Value {
Value::from_json(&json)
}
#[test]
fn a_document_as_upstream_renders_it() {
let ast =
index::parse("# Hi {% .a %}\n\nSome *em* text\n", &ParserArgs::default()).unwrap();
let values = index::transform(&ast, &Config::default()).unwrap();
let mut react = Recorder::default();
let out = render(&values, &mut react, &RenderOptions::default()).unwrap();
assert!(matches!(out, Rendered::Element(3)));
assert_eq!(
react.0,
[
r#""h1" {className:"a"} ["Hi "]"#,
r#""em" null ["em"]"#,
r#""p" null ["Some ",el1," text"]"#,
r#""article" null [el0,el2]"#,
]
);
}
#[test]
fn components_class_name_and_tags_in_attributes() {
let t = tree(serde_json::json!({"$$mdtype": "Tag", "name": "Callout",
"attributes": {"class": "c", "className": "k",
"x": [1, {"$$mdtype": "Tag", "name": "b", "attributes": {}, "children": []}]},
"children": ["t"]}));
let map: IndexMap<String, String> =
[("Callout".to_string(), "MyCallout".to_string())].into();
let options = RenderOptions {
components: Some(Components::Map(&map)),
resolve_tag_name: None,
};
let mut react = Recorder::default();
render(&t, &mut react, &options).unwrap();
assert_eq!(
react.0,
[
r#""b" null []"#,
r#"MyCallout {className:"c",x:[1,el0]} ["t"]"#
]
);
let f = |n: &JsString| Some(format!("C:{}", n.to_string_lossy()));
let options = RenderOptions {
components: Some(Components::Function(&f)),
resolve_tag_name: None,
};
let foo = tree(
serde_json::json!({"$$mdtype": "Tag", "name": "Foo", "attributes": {}, "children": []}),
);
let mut react = Recorder::default();
render(&foo, &mut react, &options).unwrap();
assert_eq!(react.0, ["C:Foo null []"]);
let getter = tree(
serde_json::json!({"$$mdtype": "Tag", "name": "__defineGetter__",
"attributes": {}, "children": []}),
);
let mut react = Recorder::default();
render(&getter, &mut react, &RenderOptions::default()).unwrap();
assert_eq!(react.0, ["fn __defineGetter__ null []"]);
}
#[test]
fn what_upstream_throws() {
let cases = [
(
serde_json::json!({"$$mdtype": "Tag", "name": "a", "attributes": null}),
"TypeError: Cannot destructure property 'class' of '{}' as it is null.",
),
(
serde_json::json!({"$$mdtype": "Tag", "name": "a", "children": "xy"}),
"TypeError: children.map is not a function",
),
(
serde_json::json!({"$$mdtype": "Tag", "name": ""}),
"TypeError: Cannot read properties of undefined (reading 'toUpperCase')",
),
];
for (json, message) in cases {
let error = render(
&tree(json),
&mut Recorder::default(),
&RenderOptions::default(),
)
.unwrap_err();
assert_eq!(thrown(&error), message);
}
}
#[test]
fn nesting_stops_where_a_fresh_node_does() {
std::thread::Builder::new()
.stack_size(2 << 20)
.spawn(|| {
let tags = |n: usize| {
let mut x = Value::from("x");
for _ in 0..n {
x = Value::from(TagValue::new("div", JsObject::new(), vec![x]));
}
x
};
let n = NODE_LIMITS[0].1;
let o = RenderOptions::default();
if stack::can_grow() {
assert!(render(&tags(n), &mut Recorder::default(), &o).is_ok());
} else {
assert!(render(&tags(50), &mut Recorder::default(), &o).is_ok());
}
let error = render(&tags(n + 1), &mut Recorder::default(), &o).unwrap_err();
assert_eq!(
thrown(&error),
"RangeError: Maximum call stack size exceeded"
);
})
.unwrap()
.join()
.unwrap();
}
}