Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix skipping of text nodes in markdown parser #1

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions basalt-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.2.2 (2025-02-27)

### Added

- [Add blank implementations for `TextNode` and `Text`](https://github.com/erikjuhani/basalt/commit/a252f62930ec59f21255d08278762734eb312cef)

### Fixed

- [Fix skipping text nodes in markdown parser](https://github.com/erikjuhani/basalt/commit/3bc112edd2b452ea7093d0e71fcfa0d02bc0b9c4)

## 0.2.1 (2025-02-23)

### Added
Expand Down
2 changes: 1 addition & 1 deletion basalt-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Provides the core functionality for Basalt TUI application
readme = "README.md"
repository = "https://github.com/erikjuhani/basalt"
license = "MIT"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[dependencies]
Expand Down
104 changes: 73 additions & 31 deletions basalt-core/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ pub struct TextNode {
pub style: Option<Style>,
}

impl From<&str> for TextNode {
fn from(value: &str) -> Self {
value.to_string().into()
}
}

impl From<String> for TextNode {
fn from(value: String) -> Self {
Self {
content: value,
..Default::default()
}
}
}

impl TextNode {
/// Creates a new [`TextNode`] from `content` and optional [`Style`].
pub fn new(content: String, style: Option<Style>) -> Self {
Expand All @@ -164,7 +179,31 @@ pub struct Text(Vec<TextNode>);

impl From<&str> for Text {
fn from(value: &str) -> Self {
Self(vec![TextNode::new(String::from(value), None)])
TextNode::from(value).into()
}
}

impl From<String> for Text {
fn from(value: String) -> Self {
TextNode::from(value).into()
}
}

impl From<TextNode> for Text {
fn from(value: TextNode) -> Self {
Self([value].to_vec())
}
}

impl From<Vec<TextNode>> for Text {
fn from(value: Vec<TextNode>) -> Self {
Self(value)
}
}

impl From<&[TextNode]> for Text {
fn from(value: &[TextNode]) -> Self {
Self(value.to_vec())
}
}

Expand Down Expand Up @@ -393,9 +432,7 @@ impl<'a> Parser<'a> {
| Tag::MetadataBlock(_)
| Tag::DefinitionList
| Tag::DefinitionListTitle
| Tag::DefinitionListDefinition => self.push_node(Node::Paragraph {
text: Text::default(),
}),
| Tag::DefinitionListDefinition => {}
}
}

Expand Down Expand Up @@ -477,12 +514,8 @@ impl<'a> Parser<'a> {
mod tests {
use indoc::indoc;

fn text(str: &str) -> Text {
Text(vec![TextNode::new(String::from(str), None)])
}

fn p(str: &str) -> Node {
Node::Paragraph { text: text(str) }
Node::Paragraph { text: str.into() }
}

fn blockquote(nodes: Vec<Node>) -> Node {
Expand All @@ -492,28 +525,28 @@ mod tests {
fn item(str: &str) -> Node {
Node::Item {
kind: None,
text: text(str),
text: str.into(),
}
}

fn task(str: &str) -> Node {
Node::Item {
kind: Some(ItemKind::Unchecked),
text: text(str),
text: str.into(),
}
}

fn completed_task(str: &str) -> Node {
Node::Item {
kind: Some(ItemKind::HardChecked),
text: text(str),
text: str.into(),
}
}

fn heading(level: HeadingLevel, str: &str) -> Node {
Node::Heading {
level,
text: text(str),
text: str.into(),
}
}

Expand Down Expand Up @@ -568,24 +601,6 @@ mod tests {
h6("Heading 6"),
],
),
(
indoc! {r#"Paragraph

> BlockQuote
>
> - List item in BlockQuote
"#},
vec![
p("Paragraph"),
blockquote(vec![
p("BlockQuote"),
Node::Paragraph {
text: Text::default(),
},
item("List item in BlockQuote"),
]),
],
),
// TODO: Implement correct test case when `- [?] ` task item syntax is supported
// Now we interpret it as a regular paragraph
(
Expand All @@ -604,6 +619,33 @@ mod tests {
p("[?] Completed task"),
],
),
(
indoc! {r#"## Quotes

You _can_ quote text by adding a `>` symbols before the text.

> Human beings face ever more complex and urgent problems, and their effectiveness in dealing with these problems is a matter that is critical to the stability and continued progress of society.
>
>- Doug Engelbart, 1961
"#},
vec![
h2("Quotes"),
Node::Paragraph {
text: vec![
TextNode::new("You ".into(), None),
TextNode::new("can".into(), None),
TextNode::new(" quote text by adding a ".into(), None),
TextNode::new(">".into(), Some(Style::Code)),
TextNode::new(" symbols before the text.".into(), None),
]
.into(),
},
blockquote(vec![
p("Human beings face ever more complex and urgent problems, and their effectiveness in dealing with these problems is a matter that is critical to the stability and continued progress of society."),
item("Doug Engelbart, 1961")
]),
],
),
];

tests
Expand Down