7
7
8
8
//! Converts [Markdown](https://en.wikipedia.org/wiki/Markdown) to [BBCode](https://en.wikipedia.org/wiki/BBCode).
9
9
10
- use markdown:: mdast:: Node ;
10
+ use markdown:: mdast as md ;
11
11
use markdown:: { to_mdast, ParseOptions } ;
12
12
use std:: collections:: HashMap ;
13
13
14
14
pub fn to_bbcode ( md : & str ) -> String {
15
- // to_mdast() never errors with normal arkdown , so unwrap is safe.
15
+ // to_mdast() never errors with normal Markdown , so unwrap is safe.
16
16
let n = to_mdast ( md, & ParseOptions :: gfm ( ) ) . unwrap ( ) ;
17
+
17
18
let definitions = n
18
19
. children ( )
19
20
. unwrap ( ) // root node always has children
20
21
. iter ( )
21
22
. filter_map ( |n| match n {
22
- Node :: Definition ( definition ) => Some ( ( & * definition . identifier , & * definition . url ) ) ,
23
+ md :: Node :: Definition ( def ) => Some ( ( & * def . identifier , & * def . url ) ) ,
23
24
_ => None ,
24
25
} )
25
26
. collect :: < HashMap < _ , _ > > ( ) ;
26
27
27
28
walk_node ( & n, & definitions) . unwrap_or_default ( )
28
29
}
29
30
30
- fn walk_node ( node : & Node , definitions : & HashMap < & str , & str > ) -> Option < String > {
31
- use Node :: * ;
31
+ fn walk_node ( node : & md:: Node , definitions : & HashMap < & str , & str > ) -> Option < String > {
32
+ use md:: Node :: * ;
33
+
32
34
let bbcode = match node {
33
35
Root ( root) => walk_nodes ( & root. children , definitions, "[br][br]" ) ,
34
- InlineCode ( markdown:: mdast:: InlineCode { value, .. } ) => format ! ( "[code]{value}[/code]" ) ,
36
+
37
+ InlineCode ( md:: InlineCode { value, .. } ) => format ! ( "[code]{value}[/code]" ) ,
38
+
35
39
Delete ( delete) => format ! ( "[s]{}[/s]" , walk_nodes( & delete. children, definitions, "" ) ) ,
40
+
36
41
Emphasis ( emphasis) => format ! ( "[i]{}[/i]" , walk_nodes( & emphasis. children, definitions, "" ) ) ,
37
- Image ( markdown:: mdast:: Image { url, .. } ) => format ! ( "[img]{url}[/img]" , ) ,
42
+
43
+ Image ( md:: Image { url, .. } ) => format ! ( "[img]{url}[/img]" , ) ,
44
+
38
45
ImageReference ( image) => {
39
46
format ! (
40
47
"[img]{}[/img]" ,
41
48
definitions. get( &&* image. identifier) . unwrap( )
42
49
)
43
50
}
44
- Link ( markdown:: mdast:: Link { url, children, .. } ) => {
51
+
52
+ Link ( md:: Link { url, children, .. } ) => {
45
53
format ! ( "[url={url}]{}[/url]" , walk_nodes( children, definitions, "" ) )
46
54
}
47
- LinkReference ( markdown:: mdast:: LinkReference {
55
+
56
+ LinkReference ( md:: LinkReference {
48
57
identifier,
49
58
children,
50
59
..
@@ -53,23 +62,31 @@ fn walk_node(node: &Node, definitions: &HashMap<&str, &str>) -> Option<String> {
53
62
definitions. get( &&* * identifier) . unwrap( ) ,
54
63
walk_nodes( children, definitions, "" )
55
64
) ,
65
+
56
66
Strong ( strong) => format ! ( "[b]{}[/b]" , walk_nodes( & strong. children, definitions, "" ) ) ,
67
+
57
68
Text ( text) => text. value . clone ( ) ,
69
+
58
70
// TODO: more langs?
59
- Code ( markdown:: mdast:: Code { value, .. } ) => format ! ( "[codeblock]{value}[/codeblock]" ) ,
71
+ Code ( md:: Code { value, .. } ) => format ! ( "[codeblock]{value}[/codeblock]" ) ,
72
+
60
73
Paragraph ( paragraph) => walk_nodes ( & paragraph. children , definitions, "" ) ,
61
- // bbcode supports lists but docs dont
62
- List ( _) | BlockQuote ( _) | FootnoteReference ( _) | FootnoteDefinition ( _) | Table ( _) => {
63
- "" . into ( )
74
+
75
+ // BBCode supports lists, but docs don't.
76
+ List ( _) | Blockquote ( _) | FootnoteReference ( _) | FootnoteDefinition ( _) | Table ( _) => {
77
+ String :: new ( )
64
78
}
79
+
65
80
Html ( html) => html. value . clone ( ) ,
81
+
66
82
_ => walk_nodes ( & node. children ( ) ?, definitions, "" ) ,
67
83
} ;
84
+
68
85
Some ( bbcode)
69
86
}
70
87
71
- /// Calls [`walk_node`] over every node its given , joining them with the supplied separator.
72
- fn walk_nodes ( nodes : & [ Node ] , definitions : & HashMap < & str , & str > , separator : & str ) -> String {
88
+ /// Calls [`walk_node`] over every node it receives , joining them with the supplied separator.
89
+ fn walk_nodes ( nodes : & [ md :: Node ] , definitions : & HashMap < & str , & str > , separator : & str ) -> String {
73
90
nodes
74
91
. iter ( )
75
92
. filter_map ( |n| walk_node ( n, definitions) )
0 commit comments