Skip to content

Commit

Permalink
Update markdown-rs, add error struct
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 23, 2024
1 parent 091a489 commit e1819ed
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 251 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include = ["src/", "license"]
serializable = ["serde"]

[dependencies]
markdown = "1.0.0-alpha.16"
markdown = "1.0.0-alpha.17"
serde = { version = "1", optional = true }
swc_core = { version = "0.90.0", features = [
"ecma_ast",
Expand Down
2 changes: 1 addition & 1 deletion examples/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate mdxjs;

/// Example that compiles the example MDX document from <https://mdxjs.com>
/// to JavaScript.
fn main() -> Result<(), String> {
fn main() -> Result<(), markdown::message::Message> {
println!(
"{}",
mdxjs::compile(
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cargo add mdxjs
```rs
extern crate mdxjs;

fn main() -> Result<(), String> {
fn main() -> Result<(), markdown::message::Message> {
println!(
"{}",
mdxjs::compile(
Expand Down
67 changes: 38 additions & 29 deletions src/hast_util_to_swc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn hast_util_to_swc(
tree: &hast::Node,
path: Option<String>,
location: Option<&Location>,
) -> Result<Program, String> {
) -> Result<Program, markdown::message::Message> {
let mut context = Context {
space: Space::Html,
comments: vec![],
Expand Down Expand Up @@ -119,7 +119,10 @@ pub fn hast_util_to_swc(
}

/// Transform one node.
fn one(context: &mut Context, node: &hast::Node) -> Result<Option<JSXElementChild>, String> {
fn one(
context: &mut Context,
node: &hast::Node,
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let value = match node {
hast::Node::Comment(x) => Some(transform_comment(context, node, x)),
hast::Node::Element(x) => transform_element(context, node, x)?,
Expand All @@ -135,7 +138,10 @@ fn one(context: &mut Context, node: &hast::Node) -> Result<Option<JSXElementChil
}

/// Transform children of `parent`.
fn all(context: &mut Context, parent: &hast::Node) -> Result<Vec<JSXElementChild>, String> {
fn all(
context: &mut Context,
parent: &hast::Node,
) -> Result<Vec<JSXElementChild>, markdown::message::Message> {
let mut result = vec![];
if let Some(children) = parent.children() {
let mut index = 0;
Expand Down Expand Up @@ -182,7 +188,7 @@ fn transform_element(
context: &mut Context,
node: &hast::Node,
element: &hast::Element,
) -> Result<Option<JSXElementChild>, String> {
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let space = context.space;

if space == Space::Html && element.tag_name == "svg" {
Expand Down Expand Up @@ -255,7 +261,7 @@ fn transform_mdx_jsx_element(
context: &mut Context,
node: &hast::Node,
element: &hast::MdxJsxElement,
) -> Result<Option<JSXElementChild>, String> {
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let space = context.space;

if let Some(name) = &element.name {
Expand Down Expand Up @@ -335,7 +341,7 @@ fn transform_mdx_expression(
context: &mut Context,
node: &hast::Node,
expression: &hast::MdxExpression,
) -> Result<Option<JSXElementChild>, String> {
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let expr = parse_expression_to_tree(
&expression.value,
&MdxExpressionKind::Expression,
Expand All @@ -360,7 +366,7 @@ fn transform_mdxjs_esm(
context: &mut Context,
_node: &hast::Node,
esm: &hast::MdxjsEsm,
) -> Result<Option<JSXElementChild>, String> {
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let mut module = parse_esm_to_tree(&esm.value, &esm.stops, context.location)?;
context.esm.append(&mut module.body);
Ok(None)
Expand All @@ -371,7 +377,7 @@ fn transform_root(
context: &mut Context,
node: &hast::Node,
_root: &hast::Root,
) -> Result<Option<JSXElementChild>, String> {
) -> Result<Option<JSXElementChild>, markdown::message::Message> {
let mut children = all(context, node)?;
let mut queue = vec![];
let mut nodes = vec![];
Expand Down Expand Up @@ -678,7 +684,7 @@ mod tests {
};

#[test]
fn comments() -> Result<(), String> {
fn comments() -> Result<(), markdown::message::Message> {
let mut comment_ast = hast_util_to_swc(
&hast::Node::Comment(hast::Comment {
value: "a".into(),
Expand Down Expand Up @@ -734,7 +740,7 @@ mod tests {
}

#[test]
fn elements() -> Result<(), String> {
fn elements() -> Result<(), markdown::message::Message> {
let mut element_ast = hast_util_to_swc(
&hast::Node::Element(hast::Element {
tag_name: "a".into(),
Expand Down Expand Up @@ -844,7 +850,7 @@ mod tests {
}

#[test]
fn element_attributes() -> Result<(), String> {
fn element_attributes() -> Result<(), markdown::message::Message> {
assert_eq!(
serialize(
&mut hast_util_to_swc(
Expand Down Expand Up @@ -976,7 +982,7 @@ mod tests {
}

#[test]
fn mdx_element() -> Result<(), String> {
fn mdx_element() -> Result<(), markdown::message::Message> {
let mut mdx_element_ast = hast_util_to_swc(
&hast::Node::MdxJsxElement(hast::MdxJsxElement {
name: None,
Expand Down Expand Up @@ -1064,7 +1070,7 @@ mod tests {
}

#[test]
fn mdx_element_name() -> Result<(), String> {
fn mdx_element_name() -> Result<(), markdown::message::Message> {
assert_eq!(
serialize(
&mut hast_util_to_swc(
Expand Down Expand Up @@ -1126,7 +1132,7 @@ mod tests {
}

#[test]
fn mdx_element_attributes() -> Result<(), String> {
fn mdx_element_attributes() -> Result<(), markdown::message::Message> {
assert_eq!(
serialize(
&mut hast_util_to_swc(
Expand Down Expand Up @@ -1238,8 +1244,11 @@ mod tests {
}),
None,
None
),
Err("0:0: Could not parse expression with swc: Unexpected eof".into()),
)
.err()
.unwrap()
.to_string(),
"Could not parse expression with swc: Unexpected eof (mdxjs-rs:swc)",
"should support an `MdxElement` (element, attribute w/ broken expression value)",
);

Expand Down Expand Up @@ -1275,16 +1284,16 @@ mod tests {
}),
None,
None
),
Err("0:0: Unexpected extra content in spread (such as `{...x,y}`): only a single spread is supported (such as `{...x}`)".into()),
).err().unwrap().to_string(),
"Unexpected extra content in spread (such as `{...x,y}`): only a single spread is supported (such as `{...x}`) (mdxjs-rs:swc)",
"should support an `MdxElement` (element, broken expression attribute)",
);

Ok(())
}

#[test]
fn mdx_expression() -> Result<(), String> {
fn mdx_expression() -> Result<(), markdown::message::Message> {
let mut mdx_expression_ast = hast_util_to_swc(
&hast::Node::MdxExpression(hast::MdxExpression {
value: "a".into(),
Expand Down Expand Up @@ -1341,7 +1350,7 @@ mod tests {
}

#[test]
fn mdx_esm() -> Result<(), String> {
fn mdx_esm() -> Result<(), markdown::message::Message> {
let mut mdxjs_esm_ast = hast_util_to_swc(
&hast::Node::MdxjsEsm(hast::MdxjsEsm {
value: "import a from 'b'".into(),
Expand Down Expand Up @@ -1399,19 +1408,19 @@ mod tests {
}),
None,
None
),
Err(
"0:0: Could not parse esm with swc: Expected 'from', got 'numeric literal (1, 1)'"
.into()
),
)
.err()
.unwrap()
.to_string(),
"Could not parse esm with swc: Expected 'from', got 'numeric literal (1, 1)' (mdxjs-rs:swc)",
"should support an `MdxjsEsm` (w/ broken content)",
);

Ok(())
}

#[test]
fn root() -> Result<(), String> {
fn root() -> Result<(), markdown::message::Message> {
let mut root_ast = hast_util_to_swc(
&hast::Node::Root(hast::Root {
children: vec![hast::Node::Text(hast::Text {
Expand Down Expand Up @@ -1467,7 +1476,7 @@ mod tests {
}

#[test]
fn text() -> Result<(), String> {
fn text() -> Result<(), markdown::message::Message> {
let mut text_ast = hast_util_to_swc(
&hast::Node::Text(hast::Text {
value: "a".into(),
Expand Down Expand Up @@ -1520,7 +1529,7 @@ mod tests {
}

#[test]
fn text_empty() -> Result<(), String> {
fn text_empty() -> Result<(), markdown::message::Message> {
let text_ast = hast_util_to_swc(
&hast::Node::Text(hast::Text {
value: String::new(),
Expand Down Expand Up @@ -1548,7 +1557,7 @@ mod tests {
}

#[test]
fn doctype() -> Result<(), String> {
fn doctype() -> Result<(), markdown::message::Message> {
let mut doctype_ast = hast_util_to_swc(
&hast::Node::Doctype(hast::Doctype { position: None }),
None,
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
swc::{parse_esm, parse_expression, serialize},
swc_util_build_jsx::{swc_util_build_jsx, Options as BuildOptions},
};
use markdown::{to_mdast, Constructs, Location, ParseOptions};
use markdown::{message, to_mdast, Constructs, Location, ParseOptions};

pub use crate::configuration::{MdxConstructs, MdxParseOptions, Options};
pub use crate::mdx_plugin_recma_document::JsxRuntime;
Expand All @@ -41,7 +41,7 @@ pub use crate::mdx_plugin_recma_document::JsxRuntime;
///
/// ```
/// use mdxjs::compile;
/// # fn main() -> Result<(), String> {
/// # fn main() -> Result<(), markdown::message::Message> {
///
/// assert_eq!(compile("# Hi!", &Default::default())?, "import { jsx as _jsx } from \"react/jsx-runtime\";\nfunction _createMdxContent(props) {\n const _components = Object.assign({\n h1: \"h1\"\n }, props.components);\n return _jsx(_components.h1, {\n children: \"Hi!\"\n });\n}\nfunction MDXContent(props = {}) {\n const { wrapper: MDXLayout } = props.components || {};\n return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {\n children: _jsx(_createMdxContent, props)\n })) : _createMdxContent(props);\n}\nexport default MDXContent;\n");
/// # Ok(())
Expand All @@ -52,7 +52,7 @@ pub use crate::mdx_plugin_recma_document::JsxRuntime;
///
/// This project errors for many different reasons, such as syntax errors in
/// the MDX format or misconfiguration.
pub fn compile(value: &str, options: &Options) -> Result<String, String> {
pub fn compile(value: &str, options: &Options) -> Result<String, message::Message> {
let parse_options = ParseOptions {
constructs: Constructs {
attention: options.parse.constructs.attention,
Expand Down

0 comments on commit e1819ed

Please sign in to comment.