Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I use mdast-util-from-markdown through remark-parse and I'd like to get the empty paragraph in my AST.
E.g. when using parsing something like


foo

baa

I get

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "foo",
          "position": {
            "start": {
              "line": 2,
              "column": 1,
              "offset": 1
            },
            "end": {
              "line": 2,
              "column": 4,
              "offset": 4
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 2,
          "column": 1,
          "offset": 1
        },
        "end": {
          "line": 2,
          "column": 4,
          "offset": 4
        }
      }
    },
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "bar",
          "position": {
            "start": {
              "line": 4,
              "column": 1,
              "offset": 6
            },
            "end": {
              "line": 4,
              "column": 4,
              "offset": 9
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 4,
          "column": 1,
          "offset": 6
        },
        "end": {
          "line": 4,
          "column": 4,
          "offset": 9
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 6,
      "column": 1,
      "offset": 11
    }
  }
}

So the empty paragraphs around my text get "lost" but I would like to use them to visualise the markdown text as is (including the empty paragraphs on line 1 3 and 5).

What's the best way achieve this?

PS: Hopefully this is the most appropriate place to ask, if not let me know! Thanks

You must be logged in to vote

There are no empty paragraphs in markdown 🤷‍♂️

Could you expand more on what and why you want this?

Replies: 1 comment · 10 replies

Comment options

There are no empty paragraphs in markdown 🤷‍♂️

Could you expand more on what and why you want this?

You must be logged in to vote
10 replies
@nicknikolov
Comment options

I see. I think you're spot on and I have a conceptual misunderstanding.

As to my use case, it's pretty simple. I have a markdown editor on the client. I just want to load markdown files and preserve the structure, so

foo 

bar

stays as is, rather than becoming

foo
bar

But I suppose, judging by what you're telling me, this is a problem with the transformer rather than the markdown AST?

@nicknikolov
Comment options

Oops I missed Christian's comment above which actually helps a lot, thanks!

@wooorm
Comment options

As to my use case, it's pretty simple. I have a markdown editor on the client. I just want to load markdown files and preserve the structure, so ...

If you want the raw text, then why are you parsing it with a markdown parser? If you want the plain text, get the file and don’t pass it to a markdown parser, but display that raw file instead?

@nicknikolov
Comment options

No, I do parse and render the AST rather than the raw text. But Christian is right I think it's up to the transformer/client. For example, since I render in HTML so I would either have to add bottom margin to show the split or something like that (maybe add an extra p tag).

I think the confusion came from the fact that I had one conceptual understanding of what a paragraph is based on HTML and that is slightly different with Markdown.

In HTML you can do

<p>foo</p>
<p></p>
<p>bar</p>
<p></p>

and this is what I wanted my pipeline to produce. But it turns out those empty lines signify the split of the paragraph in Markdown so in the end you get only two. So I will need to think about how to manage that difference.

@wooorm
Comment options

Nice. Sounds like you’ve got a good understanding of what’s going on and how to solve it.

One thing though: In HTML you also shouldn’t add empty paragraphs. HTML has meaning. So this might be more of a CSS thing, which adds presentation!

Answer selected by wooorm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.