Remove markdown comments from string

I am new to rust and am trying to remove markdown comments from a (markdown) string. Are there any libraries to do this or any other suggestiongs that the community has?

What I have tried so far

  1. regex replace - if there are multiple comments in the strings, it doesn't work as expected (rust regex does not support lookarounds)
  2. explore any html/xml comment parsers. - wip, not optimistic that this will work out.

any help is very much appreciated. :slight_smile:

I don't think Markdown can be parsed by regular expressions. Most practical computer languages (markup or otherwise) can't, first and foremost because they allow nesting (i.e. recursion), which makes them non-regular.

Markdown is not HTML or XML, so you can't parse Markdown with an HTML or XML parser, either.

Why don't you just use a Markdown parser?


Also, to the best of my knowledge, Markdown has no concept of comments. Am I suspecting it right that you are actually trying to put HTML comments in Markdown and strip those? In that case, you will first need to parse the source with a Markdown parser that recognizes when it encounters some HTML. Then, you'll need to parse that inner HTML using an HTML parser to extract the comments.

1 Like

I forget where I picked it up, but you can write a dummy footnote in a particular way, and it won't display in the rendered output (useful for mdbook).

[//]: # (TODO: Cite RFC for Widget Frobnicator)

...though I doubt that's what the OP meant.

1 Like

That's right, I also found this technique searching on Stack Overflow for "markdown comment", and I found at least 3 different workarounds, but none of them seemed to be an intentional comments-in-markdown feature :man_shrugging:

1 Like

This is quite difficult to do without parsing the string into markdown and then removing the comments. The best way is probably to use a markdown parser, otherwise it will take a lot more work and you will likely to have errors.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.