Skip to content

Commit d8264bd

Browse files
committed
Update markdown crate and use = version specifier
Also clean up some formatting in conversion code.
1 parent eeec4d1 commit d8264bd

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

godot-macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ proc-macro2 = "1.0.80" # Literal::c_string() added in 1.0.80.
2424
quote = "1.0.29"
2525

2626
# Enabled by `docs`
27-
markdown = { version = "1.0.0-alpha.19", optional = true }
27+
markdown = { version = "=1.0.0-alpha.21", optional = true }
2828
venial = "0.6"
2929

3030
[build-dependencies]

godot-macros/src/docs/markdown_converter.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,53 @@
77

88
//! Converts [Markdown](https://en.wikipedia.org/wiki/Markdown) to [BBCode](https://en.wikipedia.org/wiki/BBCode).
99
10-
use markdown::mdast::Node;
10+
use markdown::mdast as md;
1111
use markdown::{to_mdast, ParseOptions};
1212
use std::collections::HashMap;
1313

1414
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.
1616
let n = to_mdast(md, &ParseOptions::gfm()).unwrap();
17+
1718
let definitions = n
1819
.children()
1920
.unwrap() // root node always has children
2021
.iter()
2122
.filter_map(|n| match n {
22-
Node::Definition(definition) => Some((&*definition.identifier, &*definition.url)),
23+
md::Node::Definition(def) => Some((&*def.identifier, &*def.url)),
2324
_ => None,
2425
})
2526
.collect::<HashMap<_, _>>();
2627

2728
walk_node(&n, &definitions).unwrap_or_default()
2829
}
2930

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+
3234
let bbcode = match node {
3335
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+
3539
Delete(delete) => format!("[s]{}[/s]", walk_nodes(&delete.children, definitions, "")),
40+
3641
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+
3845
ImageReference(image) => {
3946
format!(
4047
"[img]{}[/img]",
4148
definitions.get(&&*image.identifier).unwrap()
4249
)
4350
}
44-
Link(markdown::mdast::Link { url, children, .. }) => {
51+
52+
Link(md::Link { url, children, .. }) => {
4553
format!("[url={url}]{}[/url]", walk_nodes(children, definitions, ""))
4654
}
47-
LinkReference(markdown::mdast::LinkReference {
55+
56+
LinkReference(md::LinkReference {
4857
identifier,
4958
children,
5059
..
@@ -53,23 +62,31 @@ fn walk_node(node: &Node, definitions: &HashMap<&str, &str>) -> Option<String> {
5362
definitions.get(&&**identifier).unwrap(),
5463
walk_nodes(children, definitions, "")
5564
),
65+
5666
Strong(strong) => format!("[b]{}[/b]", walk_nodes(&strong.children, definitions, "")),
67+
5768
Text(text) => text.value.clone(),
69+
5870
// TODO: more langs?
59-
Code(markdown::mdast::Code { value, .. }) => format!("[codeblock]{value}[/codeblock]"),
71+
Code(md::Code { value, .. }) => format!("[codeblock]{value}[/codeblock]"),
72+
6073
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()
6478
}
79+
6580
Html(html) => html.value.clone(),
81+
6682
_ => walk_nodes(&node.children()?, definitions, ""),
6783
};
84+
6885
Some(bbcode)
6986
}
7087

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 {
7390
nodes
7491
.iter()
7592
.filter_map(|n| walk_node(n, definitions))

0 commit comments

Comments
 (0)