Skip to content

Commit 9987d15

Browse files
wooormbyhemechi
andcommitted
Add serde (de)serializing to configuration
Closes GH-70. Co-authored-by: George Fischer <[email protected]>
1 parent 4fe5322 commit 9987d15

File tree

6 files changed

+183
-116
lines changed

6 files changed

+183
-116
lines changed

src/configuration.rs

Lines changed: 135 additions & 110 deletions
Large diffs are not rendered by default.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! — enable logging (includes `dep:log`);
2121
//! you can show logs with `RUST_LOG=debug`
2222
//! * **`serde`**
23-
//! — enable serde to serialize the AST (includes `dep:serde`)
23+
//! — enable serde to serialize ASTs and configuration (includes `dep:serde`)
2424
2525
#![no_std]
2626
#![deny(clippy::pedantic)]

src/util/line_ending.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use alloc::{str::FromStr, string::String};
1616
/// # }
1717
/// ```
1818
#[derive(Clone, Debug, Default, Eq, PartialEq)]
19+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1920
pub enum LineEnding {
2021
/// Both a carriage return (`\r`) and a line feed (`\n`).
2122
///
@@ -25,6 +26,7 @@ pub enum LineEnding {
2526
/// a␍␊
2627
/// b
2728
/// ```
29+
#[cfg_attr(feature = "serde", serde(rename = "\r\n"))]
2830
CarriageReturnLineFeed,
2931
/// Sole carriage return (`\r`).
3032
///
@@ -34,6 +36,7 @@ pub enum LineEnding {
3436
/// a␍
3537
/// b
3638
/// ```
39+
#[cfg_attr(feature = "serde", serde(rename = "\r"))]
3740
CarriageReturn,
3841
/// Sole line feed (`\n`).
3942
///
@@ -44,6 +47,7 @@ pub enum LineEnding {
4447
/// b
4548
/// ```
4649
#[default]
50+
#[cfg_attr(feature = "serde", serde(rename = "\n"))]
4751
LineFeed,
4852
}
4953

tests/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ fn image() -> Result<(), message::Message> {
240240
"![](javascript:alert(1))",
241241
&Options {
242242
compile: CompileOptions {
243-
allow_dangerous_protocol: false,
244243
allow_any_img_src: true,
244+
allow_dangerous_protocol: false,
245245
..Default::default()
246246
},
247247
..Default::default()

tests/serde.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@ enum Error {
99
Serde(serde_json::Error),
1010
}
1111

12+
#[test]
13+
#[cfg(feature = "serde")]
14+
fn serde_constructs() -> Result<(), Error> {
15+
use pretty_assertions::assert_eq;
16+
17+
assert_eq!(
18+
serde_json::to_string(&Constructs::default()).unwrap(),
19+
r#"{"attention":true,"autolink":true,"blockQuote":true,"characterEscape":true,"characterReference":true,"codeIndented":true,"codeFenced":true,"codeText":true,"definition":true,"frontmatter":false,"gfmAutolinkLiteral":false,"gfmFootnoteDefinition":false,"gfmLabelStartFootnote":false,"gfmStrikethrough":false,"gfmTable":false,"gfmTaskListItem":false,"hardBreakEscape":true,"hardBreakTrailing":true,"headingAtx":true,"headingSetext":true,"htmlFlow":true,"htmlText":true,"labelStartImage":true,"labelStartLink":true,"labelEnd":true,"listItem":true,"mathFlow":false,"mathText":false,"mdxEsm":false,"mdxExpressionFlow":false,"mdxExpressionText":false,"mdxJsxFlow":false,"mdxJsxText":false,"thematicBreak":true}"#
20+
);
21+
22+
Ok(())
23+
}
24+
25+
#[test]
26+
#[cfg(feature = "serde")]
27+
fn serde_compile_options() -> Result<(), Error> {
28+
use pretty_assertions::assert_eq;
29+
30+
assert_eq!(
31+
serde_json::to_string(&markdown::CompileOptions::gfm()).unwrap(),
32+
r#"{"allowAnyImgSrc":false,"allowDangerousHtml":false,"allowDangerousProtocol":false,"defaultLineEnding":"\n","gfmFootnoteBackLabel":null,"gfmFootnoteClobberPrefix":null,"gfmFootnoteLabelAttributes":null,"gfmFootnoteLabelTagName":null,"gfmFootnoteLabel":null,"gfmTaskListItemCheckable":false,"gfmTagfilter":true}"#
33+
);
34+
35+
Ok(())
36+
}
37+
38+
#[test]
39+
#[cfg(feature = "serde")]
40+
fn serde_parse_options() -> Result<(), Error> {
41+
use pretty_assertions::assert_eq;
42+
43+
assert_eq!(
44+
serde_json::to_string(&ParseOptions::gfm()).unwrap(),
45+
r#"{"constructs":{"attention":true,"autolink":true,"blockQuote":true,"characterEscape":true,"characterReference":true,"codeIndented":true,"codeFenced":true,"codeText":true,"definition":true,"frontmatter":false,"gfmAutolinkLiteral":true,"gfmFootnoteDefinition":true,"gfmLabelStartFootnote":true,"gfmStrikethrough":true,"gfmTable":true,"gfmTaskListItem":true,"hardBreakEscape":true,"hardBreakTrailing":true,"headingAtx":true,"headingSetext":true,"htmlFlow":true,"htmlText":true,"labelStartImage":true,"labelStartLink":true,"labelEnd":true,"listItem":true,"mathFlow":false,"mathText":false,"mdxEsm":false,"mdxExpressionFlow":false,"mdxExpressionText":false,"mdxJsxFlow":false,"mdxJsxText":false,"thematicBreak":true},"gfmStrikethroughSingleTilde":true,"mathTextSingleDollar":true}"#
46+
);
47+
48+
Ok(())
49+
}
50+
1251
#[test]
1352
fn serde_blockquote() -> Result<(), Error> {
1453
assert_serde(
@@ -680,9 +719,9 @@ fn serde_paragraph() -> Result<(), Error> {
680719
)
681720
}
682721

683-
/// Assert serde of Mdast constructs.
722+
/// Assert serde of mdast constructs.
684723
///
685-
/// Refer below links for the MDAST JSON construct types.
724+
/// Refer below links for the mdast JSON construct types.
686725
/// * <https://github.com/syntax-tree/mdast#nodes>
687726
/// * <https://github.com/syntax-tree/mdast-util-mdx#syntax-tree>
688727
/// * <https://github.com/syntax-tree/mdast-util-frontmatter#syntax-tree>
@@ -705,6 +744,7 @@ fn assert_serde(input: &str, expected: &str, options: ParseOptions) -> Result<()
705744
source,
706745
serde_json::from_value(actual_value).map_err(Error::Serde)?
707746
);
747+
708748
Ok(())
709749
}
710750

tests/test_utils/swc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Bridge between `markdown-rs` and SWC.
22
3-
extern crate markdown;
4-
53
use crate::test_utils::swc_utils::{create_span, RewritePrefixContext};
64
use markdown::{MdxExpressionKind, MdxSignal};
75
use std::rc::Rc;

0 commit comments

Comments
 (0)