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>,
|
||||
content: String,
|
||||
},
|
||||
List(Vec<ListItem>),
|
||||
List(Vec<Block>),
|
||||
Quote(Vec<Block>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ListItem {
|
||||
pub blocks: Vec<Block>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Inline {
|
||||
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
|
||||
if line_chars.parse_str("```") {
|
||||
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();
|
||||
text.push(c);
|
||||
let mut escaped = false;
|
||||
while let Some(&nc) = chars.peek() {
|
||||
if matches!(nc, '*' | '_' | '`' | '[') {
|
||||
if matches!(nc, '*' | '_' | '`' | '[') && !escaped {
|
||||
break;
|
||||
}
|
||||
let c = chars.next().ok_or(MdParseError::new("a character", ""))?;
|
||||
text.push(c);
|
||||
let next_c = chars.next().ok_or(MdParseError::new("a character", ""))?;
|
||||
if next_c == '\\' && !escaped {
|
||||
escaped = true;
|
||||
} else {
|
||||
escaped = false;
|
||||
text.push(next_c);
|
||||
}
|
||||
}
|
||||
inlines.push(Inline::Text(text));
|
||||
}
|
||||
@@ -138,15 +144,18 @@ mod test {
|
||||
|
||||
#[test]
|
||||
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();
|
||||
|
||||
assert_eq!(
|
||||
inl,
|
||||
vec![Inline::Link {
|
||||
vec![
|
||||
Inline::Text("a link to ".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());
|
||||
}
|
||||
|
||||
#[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