Skip to content

Commit

Permalink
fix: delete export in mdx output
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Jun 20, 2023
1 parent 8998532 commit 386ebc1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 106 deletions.
3 changes: 3 additions & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Output {
pub html: String,
pub title: String,
pub toc: Vec<Toc>,
pub frontmatter: String,
}

#[napi(object)]
Expand Down Expand Up @@ -51,6 +52,7 @@ impl From<CompileResult> for Output {
html: res.html,
title: res.title,
toc: res.toc.into_iter().map(|item| item.into()).collect(),
frontmatter: res.frontmatter,
}
}
}
Expand All @@ -77,6 +79,7 @@ impl Task for Compiler {
.map(|item| item.into())
.collect::<Vec<Toc>>(),
)?;
obj.set_named_property("frontmatter", output.frontmatter)?;
Ok(obj)
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/mdx_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct CompileResult {
pub html: String,
pub title: String,
pub toc: Vec<TocItem>,
pub frontmatter: String,
}

pub fn compile(
Expand Down Expand Up @@ -91,13 +92,11 @@ pub fn compile(
development,
provider_import_source: Some("@mdx-js/react".to_string()),
};
let build_options = BuildOptions { development };

let location = Location::new(value.as_bytes());
let mut mdast =
to_mdast(value.as_str(), &parse_options).expect(format!("value: {}", value).as_str());
let toc_result = mdx_plugin_toc(&mut mdast);
mdx_plugin_frontmatter(&mut mdast);
let frontmatter = mdx_plugin_frontmatter(&mut mdast);
let mut hast = mdast_util_to_hast(&mdast);
mdx_plugin_header_anchor(&mut hast);
mdx_plugin_container(&mut hast);
Expand All @@ -121,14 +120,15 @@ pub fn compile(
mdx_plugin_recma_document(&mut program, &document_options, Some(&location))
.expect(format!("file: {}", filepath,).as_str());
mdx_plugin_recma_jsx_rewrite(&mut program, &rewrite_options, Some(&location));

swc_util_build_jsx(&mut program, &build_options, Some(&location)).unwrap();
// We keep the origin jsx here.
// swc_util_build_jsx(&mut program, &build_options, Some(&location)).unwrap();
let code = serialize(&mut program.module, Some(&program.comments));
CompileResult {
code,
links,
html,
title: toc_result.title,
toc: toc_result.toc,
frontmatter,
}
}
60 changes: 3 additions & 57 deletions crates/plugin_frontmatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn yaml_to_json(yaml_str: &str) -> String {
serde_json::to_string(&parsed_value).unwrap()
}

pub fn mdx_plugin_frontmatter(node: &mut mdast::Node) {
pub fn mdx_plugin_frontmatter(node: &mut mdast::Node) -> String {
if let mdast::Node::Root(root) = node {
let mut front_matter = String::new();
let mut front_matter_node_index = None;
Expand All @@ -28,13 +28,9 @@ pub fn mdx_plugin_frontmatter(node: &mut mdast::Node) {
if let Some(i) = front_matter_node_index {
root.children.remove(i);
}
let front_matter_exports = mdast::Node::MdxjsEsm(mdast::MdxjsEsm {
value: format!("export const frontmatter = {}", yaml_to_json(&front_matter)),
position: None,
stops: vec![],
});
root.children.insert(0, front_matter_exports);
return yaml_to_json(&front_matter);
}
"{}".into()
}

#[cfg(test)]
Expand All @@ -47,54 +43,4 @@ mod tests {
let expected = "{}".to_string();
assert_eq!(yaml_to_json(yaml), expected);
}

#[test]
fn test_yaml_to_json_simple() {
let yaml = "
name: John Doe
age: 30
";
let expected = r#"{"name":"John Doe","age":30}"#.to_string();
assert_eq!(yaml_to_json(yaml), expected);
}

#[test]
fn test_yaml_to_json_nested() {
let yaml = "
person:
name: John Doe
age: 30
";
let expected = r#"{"person":{"name":"John Doe","age":30}}"#.to_string();
assert_eq!(yaml_to_json(yaml), expected);
}

#[test]
fn test_mdx_plugin_front_matter() {
use markdown::mdast::{MdxjsEsm, Node, Root, Yaml};

// Input AST
let mut ast = Node::Root(Root {
children: vec![Node::Yaml(Yaml {
value: "title: My Blog\nauthor: John Doe\n".to_string(),
position: None,
})],
position: None,
});

// Expected output AST
let expected_ast = Node::Root(Root {
children: vec![Node::MdxjsEsm(MdxjsEsm {
value: "export const frontmatter = {\"title\":\"My Blog\",\"author\":\"John Doe\"}"
.to_string(),
position: None,
stops: vec![],
})],
position: None,
});

// Apply function and check output
mdx_plugin_frontmatter(&mut ast);
assert_eq!(ast, expected_ast);
}
}
47 changes: 3 additions & 44 deletions crates/plugin_toc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,6 @@ pub fn mdx_plugin_toc(node: &mut mdast::Node) -> TocResult {
});
}
}
// add toc exports in the module
// such as `export const toc = [{ title: 'title', level: 1 }]`
let title_exports = mdast::Node::MdxjsEsm(mdast::MdxjsEsm {
value: format!("export const title = '{}';", title),
position: None,
stops: vec![],
});
let toc_code = toc
.iter()
.map(|item| {
format!(
"{{ text: \"{}\", depth: {}, id: \"{}\" }}",
item.text, item.depth, item.id
)
})
.collect::<Vec<String>>()
.join(",");
let toc_exports = mdast::Node::MdxjsEsm(mdast::MdxjsEsm {
value: format!("export const toc = [{}];", toc_code),
position: None,
stops: vec![],
});
root.children.extend(vec![title_exports, toc_exports]);
}

TocResult { title, toc }
Expand Down Expand Up @@ -208,27 +185,9 @@ mod tests {
position: None,
});

mdx_plugin_toc(&mut root);
let result = mdx_plugin_toc(&mut root);

if let mdast::Node::Root(root) = root {
assert_eq!(root.children.len(), 8);
assert_eq!(
root.children[6],
mdast::Node::MdxjsEsm(mdast::MdxjsEsm {
value: "export const title = 'HelloWorld';".to_string(),
position: None,
stops: vec![],
})
);
assert_eq!(
root.children[7],
mdast::Node::MdxjsEsm(mdast::MdxjsEsm {
value: "export const toc = [{ text: \"HelloWorld\", depth: 2, id: \"helloworld-1\" },{ text: \"HelloWorld\", depth: 3, id: \"helloworld-2\" },{ text: \"HelloWorld\", depth: 4, id: \"helloworld-3\" }];"
.to_string(),
position: None,
stops: vec![],
})
);
}
assert_eq!(result.title, "HelloWorld");
assert_eq!(result.toc.len(), 3);
}
}
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Output {
html: string
title: string
toc: Array<Toc>
frontmatter: string
}
export interface CompileOptions {
value: string
Expand Down

0 comments on commit 386ebc1

Please sign in to comment.