added special character escaping to md
All checks were successful
Test the running changes / Test (push) Successful in 42s
All checks were successful
Test the running changes / Test (push) Successful in 42s
This commit is contained in:
@@ -16,15 +16,10 @@ pub enum Block {
|
|||||||
language: Option<String>,
|
language: Option<String>,
|
||||||
content: String,
|
content: String,
|
||||||
},
|
},
|
||||||
List(Vec<ListItem>),
|
List(Vec<Block>),
|
||||||
Quote(Vec<Block>),
|
Quote(Vec<Block>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct ListItem {
|
|
||||||
pub blocks: Vec<Block>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Inline {
|
pub enum Inline {
|
||||||
Text(String),
|
Text(String),
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ pub fn parse_blocks(input: &str) -> Result<Vec<Block>, MdParseError> {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// unordered list TODO
|
||||||
|
if line_chars.parse_str("- ") {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
// code
|
// code
|
||||||
if line_chars.parse_str("```") {
|
if line_chars.parse_str("```") {
|
||||||
let lang_line: String = line_chars.collect();
|
let lang_line: String = line_chars.collect();
|
||||||
|
|||||||
@@ -36,12 +36,18 @@ pub fn parse_inlines(input: &str) -> Result<Vec<Inline>, MdParseError> {
|
|||||||
_ => {
|
_ => {
|
||||||
let mut text = String::new();
|
let mut text = String::new();
|
||||||
text.push(c);
|
text.push(c);
|
||||||
|
let mut escaped = false;
|
||||||
while let Some(&nc) = chars.peek() {
|
while let Some(&nc) = chars.peek() {
|
||||||
if matches!(nc, '*' | '_' | '`' | '[') {
|
if matches!(nc, '*' | '_' | '`' | '[') && !escaped {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let c = chars.next().ok_or(MdParseError::new("a character", ""))?;
|
let next_c = chars.next().ok_or(MdParseError::new("a character", ""))?;
|
||||||
text.push(c);
|
if next_c == '\\' && !escaped {
|
||||||
|
escaped = true;
|
||||||
|
} else {
|
||||||
|
escaped = false;
|
||||||
|
text.push(next_c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
inlines.push(Inline::Text(text));
|
inlines.push(Inline::Text(text));
|
||||||
}
|
}
|
||||||
@@ -138,15 +144,18 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn single_hyperlink() {
|
fn single_hyperlink() {
|
||||||
let md = "[my site](https://example.com)";
|
let md = "a link to [my site](https://example.com)";
|
||||||
let inl = parse_inlines(md).unwrap();
|
let inl = parse_inlines(md).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
inl,
|
inl,
|
||||||
vec![Inline::Link {
|
vec![
|
||||||
text: vec![Inline::Text("my site".to_string())],
|
Inline::Text("a link to ".to_string()),
|
||||||
href: "https://example.com".to_string()
|
Inline::Link {
|
||||||
}]
|
text: vec![Inline::Text("my site".to_string())],
|
||||||
|
href: "https://example.com".to_string()
|
||||||
|
}
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,4 +166,18 @@ mod test {
|
|||||||
|
|
||||||
assert!(inl.is_err());
|
assert!(inl.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn escape_brackets() {
|
||||||
|
let md = r"some \[text\]";
|
||||||
|
let inl = parse_inlines(md).unwrap();
|
||||||
|
assert_eq!(inl, vec![Inline::Text("some [text]".to_string())]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn escape_escape() {
|
||||||
|
let md = r"backslash \\";
|
||||||
|
let inl = parse_inlines(md).unwrap();
|
||||||
|
assert_eq!(inl, vec![Inline::Text(r"backslash \".to_string())]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user