markdown-it / markdown-it-container

Fenced container plugin for markdown-it markdown parser
MIT License
489 stars 74 forks source link

Allow getting the raw text content of the container #51

Closed mjbvz closed 1 month ago

mjbvz commented 1 month ago

I'd like a way to access the raw text content of a container. Currently the container content is parsed as markdown, which makes it difficult to recover the original text. For example:

::: container
---
title: name
---
:::

My request is to make it easier to get the string:

---
title: name
---

This is needed for VS Code's markdown mermaid support: https://github.com/mjbvz/vscode-markdown-mermaid/issues/260

Sorry if this is already possible. I tried debugging but couldn't find a good way to do this

puzrin commented 1 month ago

Probably, you could cheat with .map https://github.com/markdown-it/markdown-it-container/blob/master/index.mjs#L119, without any changes.

But according to memraid mentioning, you probably going to do something wrong. This plugin is specially designed for markdown content inside, and should not be used for other cases. Fenced block kind will be more suitable for your need, and gives access to content as you wish.

I'd suggest to clone fenced block's source and modify markers from "````" to what you need

mjbvz commented 1 month ago

The .map line number look correct but once you get to a renderer, is there any way to get the original input string?


The ::: mermaid block syntax comes from here

If this is a use you'd be open to supporting, I can look into a PR that expose the container source text on container_open, maybe as a new property on the token object

Otherwise I have copied the code from this repo but advance from the start marker to end without parsing: https://github.com/mjbvz/vscode-markdown-mermaid/commit/116ec61d49b14aa7c35fc9b257a508c254655b72

puzrin commented 1 month ago

I think you were confused by the fact that GitHub allocated separate syntax for fenced mermaid block. Some discussion was here https://talk.commonmark.org/t/mermaid-generation-of-diagrams-and-flowcharts-from-text-in-a-similar-manner-as-markdown/1882.

As I said, it's not a good idea to use this plugin for fenced block guards search. Inappropriate use causes inappropriate expectations and inappropriate demands :). From CM architecture point of view, cloning fenced block would be more simple and siutable.

mjbvz commented 1 month ago

Just to be clear, this is not a proposal for new syntax. Instead my proposal is needed to correctly handle the mermaid container-style syntax that already is used in the wild, for example on azure wikis

If you don't feel this aligns with this library or feel that other consumers of it would benefit, feel free to close this and I'll keep using my forked code instead for this use case

puzrin commented 1 month ago

I understand your proposal very well. But since this plugin is a markdown container (content parsed by markdown) and your demand comes from a fenced block (non-markdown fixed content, for external tools), I'd prefer to avoid a mixture and keep the code unchanged.

Don't be confused by ::: in readme. This plugin was created much earlier than Mermaid was adopted, and ::: is only for example. While things looks very similar to what you expect, those are for completely different use.