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

Add support for nested fenced code blocks #119

Closed
nokome opened this issue Jul 2, 2024 · 4 comments
Closed

Add support for nested fenced code blocks #119

nokome opened this issue Jul 2, 2024 · 4 comments
Labels
question Further information is requested

Comments

@nokome
Copy link
Contributor

nokome commented Jul 2, 2024

First, thanks for this crate! (and more generally all the awesome work you do to help developers parse Markdown! 🙌🏽 )

In my usage, I need to handle nested fenced code blocks, but as the follow test shows, this is currently not supported. If you think this would be a good thing to support let me know and I can have a go at a PR.

use common_dev::pretty_assertions::assert_eq;
use markdown::{
    mdast::{Code, Node},
    to_mdast, ParseOptions,
};

#[test]
fn nested_code_block() {
    let options = ParseOptions::gfm();

    let mdast = to_mdast(
        r#"
```md

First code block

````python
# Nested code block
````

```
"#,
        &options,
    )
    .unwrap();

    match mdast.children().unwrap().first().unwrap() {
        Node::Code(Code { lang, value, .. }) => {
            assert_eq!(lang.as_deref(), Some("md"));
            // This currently passes but it shouldn't: the python code block is truncated
            assert_eq!(
                value,
                "
First code block

````python
# Nested code block"
            );
        }
        _ => assert!(false, "unexpected node type"),
    }
}
@ChristianMurphy
Copy link
Collaborator

Welcome @nokome! 👋
It is supported, the syntax is different from what you are trying.
It needs to have a descending number of backticks.
Larger on the outside smaller on the inside.

````md

First code block

```python
# Nested code block
```

````

@ChristianMurphy ChristianMurphy closed this as not planned Won't fix, can't repro, duplicate, stale Jul 2, 2024
@ChristianMurphy ChristianMurphy added the question Further information is requested label Jul 2, 2024
@nokome
Copy link
Contributor Author

nokome commented Jul 2, 2024

Thanks @ChristianMurphy for the quick response! That syntax is a little more awkward to implement for my use case but great to know it is supported.

@ChristianMurphy
Copy link
Collaborator

I assume your use case is wanting to use markdown.
The markdown crate, follows the markdown standard https://commonmark.org/
Descending number of backticks is how nesting code works in markdown.

If your use case includes making yet another markdown flavor, see #32 for work toward plugins and syntax extensions.

@wooorm
Copy link
Owner

wooorm commented Jul 3, 2024

Sticking with standards (in this case, CommonMark/GFM), means that your markdown files will work everywhere. Here on GH too. And in the syntax highlighter in your editor.
Going with a non-standard format would mean the content is not so portable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants