Skip to content

Commit 2905847

Browse files
authored
MDX 설명에서 JSX 요소 제거 기능 추가 (#7)
1 parent 1dcc618 commit 2905847

File tree

11 files changed

+183
-30
lines changed

11 files changed

+183
-30
lines changed

Cargo.lock

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ serde_yaml_ng = { version = "0.10" }
2626
browser_sdk_schema = { path = "./crates/browser_sdk_schema" }
2727
browser_sdk_ts_codegen = { path = "./crates/browser_sdk_ts_codegen" }
2828
browser_sdk_ts_codegen_macros = { path = "./crates/browser_sdk_ts_codegen_macros" }
29+
browser_sdk_utils = { path = "./crates/browser_sdk_utils" }
30+
markdown = { git = "https://github.com/cirnov/markdown-rs" }
31+
mdast_util_to_markdown = { git = "https://github.com/cirnov/markdown-rs" }

crates/browser_sdk_dart_codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ edition = "2024"
55

66
[dependencies]
77
browser_sdk_schema.workspace = true
8+
browser_sdk_utils.workspace = true
89
convert_case = { workspace = true }

crates/browser_sdk_dart_codegen/src/ast/enum.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ mod tests {
6666
fn enum_with_variant() {
6767
let empty = Enum {
6868
name: Identifier::try_from("Test1").unwrap(),
69-
description: Some(Comment("Test1 Enum".into())),
69+
description: Some(Comment::try_from("Test1 Enum").unwrap()),
7070
variants: vec![
7171
EnumVariant {
7272
name: Identifier::try_from("VARIANT_A").unwrap(),
@@ -76,9 +76,9 @@ mod tests {
7676
EnumVariant {
7777
name: Identifier::try_from("VARIANT_B").unwrap(),
7878
value: "value_b".into(),
79-
description: Some(Comment(
80-
"This is a variant\nwith a multi-line description".into(),
81-
)),
79+
description: Some(Comment::try_from(
80+
"This is a variant\nwith a multi-line description",
81+
).unwrap()),
8282
},
8383
],
8484
union_parents: vec![],

crates/browser_sdk_dart_codegen/src/ast/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod intersection;
66
mod object;
77
mod union;
88

9+
use browser_sdk_utils::{MdastNodeExt, ToMdastExt};
910
pub use r#enum::*;
1011
pub use ident::*;
1112
pub use intersection::*;
@@ -22,14 +23,36 @@ impl fmt::Display for Indent {
2223
}
2324

2425
#[derive(Debug, Clone)]
25-
pub struct Comment(pub String);
26+
pub struct Comment(String);
2627

2728
impl Comment {
2829
pub fn lines(&self) -> impl Iterator<Item = &str> {
2930
self.0.trim().lines().map(str::trim)
3031
}
3132
}
3233

34+
impl TryFrom<&str> for Comment {
35+
type Error = String;
36+
37+
fn try_from(value: &str) -> Result<Self, Self::Error> {
38+
let cleaned = value
39+
.to_mdast()
40+
.map_err(|e| format!("Failed to parse markdown: {}", e))?
41+
.remove_jsx_elements()
42+
.to_markdown_string()
43+
.map_err(|e| format!("Failed to convert markdown to string: {}", e))?;
44+
Ok(Comment(cleaned))
45+
}
46+
}
47+
48+
impl TryFrom<String> for Comment {
49+
type Error = String;
50+
51+
fn try_from(value: String) -> Result<Self, Self::Error> {
52+
Comment::try_from(value.as_str())
53+
}
54+
}
55+
3356
#[derive(Debug, Clone)]
3457
pub struct TypeReference {
3558
pub path: String,

crates/browser_sdk_dart_codegen/src/ast/object.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ mod tests {
202202
fn empty_object() {
203203
let object = Object {
204204
name: Identifier::try_from("Test").unwrap(),
205-
description: Some(Comment("Test Object".into())),
205+
description: Some(Comment::try_from("Test Object").unwrap()),
206206
fields: vec![],
207207
is_one_of: false,
208208
union_parents: vec![UnionParent::Union {
@@ -229,7 +229,7 @@ class Test {
229229
fn object_with_fields() {
230230
let object = Object {
231231
name: Identifier::try_from("Address").unwrap(),
232-
description: Some(Comment("주소 정보".into())),
232+
description: Some(Comment::try_from("주소 정보").unwrap()),
233233
fields: vec![
234234
ObjectField {
235235
name: Identifier::try_from("country").unwrap(),
@@ -252,7 +252,7 @@ class Test {
252252
is_list: false,
253253
is_required: true,
254254
},
255-
description: Some(Comment("**일반주소**".into())),
255+
description: Some(Comment::try_from("**일반주소**").unwrap()),
256256
},
257257
ObjectField {
258258
name: Identifier::try_from("addressLine2").unwrap(),
@@ -262,7 +262,7 @@ class Test {
262262
is_list: false,
263263
is_required: true,
264264
},
265-
description: Some(Comment("**상세주소**".into())),
265+
description: Some(Comment::try_from("**상세주소**").unwrap()),
266266
},
267267
ObjectField {
268268
name: Identifier::try_from("city").unwrap(),
@@ -272,7 +272,7 @@ class Test {
272272
is_list: false,
273273
is_required: false,
274274
},
275-
description: Some(Comment("**도시**".into())),
275+
description: Some(Comment::try_from("**도시**").unwrap()),
276276
},
277277
ObjectField {
278278
name: Identifier::try_from("province").unwrap(),
@@ -282,7 +282,7 @@ class Test {
282282
is_list: false,
283283
is_required: false,
284284
},
285-
description: Some(Comment("**주, 도, 시**".into())),
285+
description: Some(Comment::try_from("**주, 도, 시**").unwrap()),
286286
},
287287
],
288288
is_one_of: false,
@@ -326,7 +326,7 @@ class Address {
326326
fn one_of_object() {
327327
let object = Object {
328328
name: Identifier::try_from("MonthOption").unwrap(),
329-
description: Some(Comment("**할부 개월 수 설정**".into())),
329+
description: Some(Comment::try_from("**할부 개월 수 설정**").unwrap()),
330330
fields: vec![
331331
ObjectField {
332332
name: Identifier::try_from("fixedMonth").unwrap(),
@@ -336,9 +336,9 @@ class Address {
336336
is_list: false,
337337
is_required: true,
338338
},
339-
description: Some(Comment(
340-
"**구매자가 선택할 수 없도록 고정된 할부 개월수**".into(),
341-
)),
339+
description: Some(Comment::try_from(
340+
"**구매자가 선택할 수 없도록 고정된 할부 개월수**",
341+
).unwrap()),
342342
},
343343
ObjectField {
344344
name: Identifier::try_from("availableMonthList").unwrap(),
@@ -348,9 +348,9 @@ class Address {
348348
is_list: true,
349349
is_required: true,
350350
},
351-
description: Some(Comment(
352-
"**구매자가 선택할 수 있는 할부 개월수 리스트**".into(),
353-
)),
351+
description: Some(Comment::try_from(
352+
"**구매자가 선택할 수 있는 할부 개월수 리스트**",
353+
).unwrap()),
354354
},
355355
],
356356
is_one_of: true,

crates/browser_sdk_dart_codegen/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl ResourceProcessor {
114114
name: field_name,
115115
serialized_name: name.to_string(),
116116
value_type,
117-
description: parameter.description.clone().map(Comment),
117+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
118118
};
119119
}
120120
ParameterType::ResourceRef(r) => {
@@ -135,7 +135,7 @@ impl ResourceProcessor {
135135
name: field_name,
136136
serialized_name: name.to_string(),
137137
value_type,
138-
description: parameter.description.clone().map(Comment),
138+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
139139
}
140140
}
141141

@@ -228,21 +228,21 @@ impl ResourceProcessor {
228228
match &parameter.r#type {
229229
ParameterType::Object { properties, hide_if_empty: _ } => Some(Entity::Object(Object {
230230
name: name.clone(),
231-
description: parameter.description.clone().map(Comment),
231+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
232232
fields: Self::build_field_list(properties.iter()),
233233
is_one_of: false,
234234
union_parents: vec![],
235235
})),
236236
ParameterType::EmptyObject => Some(Entity::Object(Object {
237237
name: name.clone(),
238-
description: parameter.description.clone().map(Comment),
238+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
239239
fields: vec![],
240240
is_one_of: false,
241241
union_parents: vec![],
242242
})),
243243
ParameterType::Enum { variants, .. } => Some(Entity::Enum(Enum {
244244
name: name.clone(),
245-
description: parameter.description.clone().map(Comment),
245+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
246246
variants: variants
247247
.iter()
248248
.map(|(value, variant)| EnumVariant {
@@ -252,21 +252,21 @@ impl ResourceProcessor {
252252
Identifier::try_from(value.as_str()).unwrap()
253253
},
254254
value: value.clone(),
255-
description: variant.description.clone().map(Comment),
255+
description: variant.description.clone().map(|d| Comment::try_from(d).unwrap()),
256256
})
257257
.collect(),
258258
union_parents: vec![],
259259
})),
260260
ParameterType::OneOf { properties, hide_if_empty: _ } => Some(Entity::Object(Object {
261261
name: name.clone(),
262-
description: parameter.description.clone().map(Comment),
262+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
263263
fields: Self::build_field_list(properties.iter()),
264264
is_one_of: true,
265265
union_parents: vec![],
266266
})),
267267
ParameterType::Union { types, hide_if_empty: _ } => Some(Entity::Union(Union {
268268
name: name.clone(),
269-
description: parameter.description.clone().map(Comment),
269+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
270270
variants: types
271271
.iter()
272272
.map(|parameter| match &parameter.r#type {
@@ -279,7 +279,7 @@ impl ResourceProcessor {
279279
.to_case(Case::Camel)
280280
.try_into()
281281
.unwrap(),
282-
description: parameter.description.clone().map(Comment),
282+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
283283
type_name: type_reference,
284284
}
285285
}
@@ -289,7 +289,7 @@ impl ResourceProcessor {
289289
})),
290290
ParameterType::Intersection { types, hide_if_empty: _ } => Some(Entity::Intersection(Intersection {
291291
name: name.clone(),
292-
description: parameter.description.clone().map(Comment),
292+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
293293
constituents: types
294294
.iter()
295295
.map(|parameter| match &parameter.r#type {
@@ -318,7 +318,7 @@ impl ResourceProcessor {
318318
hide_if_empty: _,
319319
} => Some(Entity::DiscriminatedUnion(DiscriminatedUnion {
320320
name: name.clone(),
321-
description: parameter.description.clone().map(Comment),
321+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
322322
discriminator: Identifier::try_from(discriminator.as_str()).unwrap(),
323323
variants: types
324324
.iter()
@@ -329,7 +329,7 @@ impl ResourceProcessor {
329329
discriminator_value: value.clone(),
330330
name: value.to_case(Case::Camel).try_into().unwrap(),
331331
type_name: type_reference,
332-
description: parameter.description.clone().map(Comment),
332+
description: parameter.description.clone().map(|d| Comment::try_from(d).unwrap()),
333333
}
334334
}
335335
_ => unreachable!(),

crates/browser_sdk_ts_codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ indexmap = { workspace = true }
1818
indoc = { workspace = true }
1919
browser_sdk_schema = { workspace = true }
2020
browser_sdk_ts_codegen_macros = { workspace = true }
21+
browser_sdk_utils.workspace = true
2122
pretty_assertions = { workspace = true }
2223
pathdiff = "0.2.1"

0 commit comments

Comments
 (0)