markdown-it / markdown-it-container

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

Feature: add title to container block #52

Closed AlbertHilb closed 2 months ago

AlbertHilb commented 2 months ago

I'd like to have the possibility to add a title containing inline markdown elements to a container block.

::: Theorem **Pythagorean** theorem
Content
:::

That should be rendered as

<div class="Theorem">
<header><b>Pythagorean</b> theorem</header>
<p>Content</p>
</div>

A feature similar to that requested in the issue #8 but with in addition the ability to use inline markdown.

To achieve this, I propose to add a new optional parameter, let's say pattern, to the configuration object of the container. pattern should be a regular expression containing named capturing groups. For example:

pattern: /\s*Theorem\s+(?<title>.*)/

The regular expression is matched against the part of text line following the markup which starts the container. In my example the string would be " Theorem **Pythagorean** theorem".

If a group named "title" is found the content of the group is added as an inline token inside a container_title block, right after the opening token of the container.

If other named groups are found, they are added as attributes of the opening token of the container.

If you like my propose I can send a PR to implement it.

puzrin commented 2 months ago

https://github.com/markdown-it/markdown-it-container?tab=readme-ov-file#api there is a validate option to check title content. See example in readme.

AlbertHilb commented 2 months ago

Thank you, @puzrin, for your quick reply.

I'm aware of the validate option. However the main aim of my propose is to select a part of the text line opening the container that should be parsed as inline markdown.

With that ability one can use this extension to implement, for instance, the CommonMark Container Block Directive syntax:

::: name [inline-content] {key=val}
contents, which are sometimes further block elements
:::

which requires the string enclosed in square brackets to be parsed as inline markdown.

I don't think that can be achieved with the validate function as it is now.

Maybe it is possible to change the return values allowed for validate to include a dictionary containing the string that should be parsed as inline markdown, the name of block the result should be inserted into and the HTML tag to be used to render that block.

puzrin commented 2 months ago

You can call an inline parser in the renderer for info string. That's NOT the responsibility of this plugin to support non-standard markups. It was created to cover need of proper nested content parse. It supports commonmark "spirit" and can be used as base, if someone wish to customize. But I don't think it's good idea to incorporate everything right here.