diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-01-30 12:50:58 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-01-30 22:46:59 +0100 |
| commit | 8d1ce390e21ce0a5812a4211c893ec359906d6f1 (patch) | |
| tree | 5dbe1ad96af8ce8f9f01887340fe06025462e959 /src/syntax/ast.rs | |
| parent | d7072f378fef733ae994fd9a1e767df4e4dd878e (diff) | |
Rework strong and emph
- Star and underscore not parsed as strong/emph inside of words
- Stars/underscores must be balanced and they cannot go over paragraph break
- New `strong` and `emph` classes
Diffstat (limited to 'src/syntax/ast.rs')
| -rw-r--r-- | src/syntax/ast.rs | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 13c639f9..560d7c30 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -63,8 +63,6 @@ impl Markup { NodeKind::Space(_) => Some(MarkupNode::Space), NodeKind::Linebreak => Some(MarkupNode::Linebreak), NodeKind::Parbreak => Some(MarkupNode::Parbreak), - NodeKind::Strong => Some(MarkupNode::Strong), - NodeKind::Emph => Some(MarkupNode::Emph), NodeKind::Text(s) | NodeKind::TextInLine(s) => { Some(MarkupNode::Text(s.clone())) } @@ -72,8 +70,10 @@ impl Markup { NodeKind::EnDash => Some(MarkupNode::Text('\u{2013}'.into())), NodeKind::EmDash => Some(MarkupNode::Text('\u{2014}'.into())), NodeKind::NonBreakingSpace => Some(MarkupNode::Text('\u{00A0}'.into())), - NodeKind::Math(math) => Some(MarkupNode::Math(math.as_ref().clone())), + NodeKind::Strong => node.cast().map(MarkupNode::Strong), + NodeKind::Emph => node.cast().map(MarkupNode::Emph), NodeKind::Raw(raw) => Some(MarkupNode::Raw(raw.as_ref().clone())), + NodeKind::Math(math) => Some(MarkupNode::Math(math.as_ref().clone())), NodeKind::Heading => node.cast().map(MarkupNode::Heading), NodeKind::List => node.cast().map(MarkupNode::List), NodeKind::Enum => node.cast().map(MarkupNode::Enum), @@ -91,12 +91,12 @@ pub enum MarkupNode { Linebreak, /// A paragraph break: Two or more newlines. Parbreak, - /// Strong text was enabled / disabled: `*`. - Strong, - /// Emphasized text was enabled / disabled: `_`. - Emph, /// Plain text. Text(EcoString), + /// Strong content: `*Strong*`. + Strong(StrongNode), + /// Emphasized content: `_Emphasized_`. + Emph(EmphNode), /// A raw block with optional syntax highlighting: `` `...` ``. Raw(RawNode), /// A math formula: `$a^2 = b^2 + c^2$`. @@ -111,6 +111,32 @@ pub enum MarkupNode { Expr(Expr), } +node! { + /// Strong content: `*Strong*`. + StrongNode: Strong +} + +impl StrongNode { + /// The contents of the strong node. + pub fn body(&self) -> Markup { + self.0.cast_first_child().expect("strong node is missing markup body") + } +} + +node! { + /// Emphasized content: `_Emphasized_`. + EmphNode: Emph +} + +impl EmphNode { + /// The contents of the emphasis node. + pub fn body(&self) -> Markup { + self.0 + .cast_first_child() + .expect("emphasis node is missing markup body") + } +} + /// A raw block with optional syntax highlighting: `` `...` ``. #[derive(Debug, Clone, PartialEq)] pub struct RawNode { |
