r4ai / remark-callout

A remark plugin to add obsidian style callouts to markdown
https://r4ai.github.io/remark-callout/
MIT License
9 stars 0 forks source link

Support for block elements in headings #111

Open eikowagenknecht opened 4 months ago

eikowagenknecht commented 4 months ago

Obsidian itself also supports block elements in headings, like this:

grafik

Maybe this could be added here as well, for feature parity? :-)

r4ai commented 4 months ago

It would be great to support that feature!

Thanks for the feedback.

r4ai commented 4 months ago

After researching various options, it seems challenging to implement this feature due to the specifications of mdast and remark.

For example, consider the following markdown:

> [!note] title here
> body here

This is transformed into the following mdast, and this plugin generates the callout by manipulating this mdast.

{
  "type": "root",
  "children": [
    {
      "type": "blockquote",
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "[!note] # title here\nbody here",
              "position": {
                "start": {
                  "line": 1,
                  "column": 3,
                  "offset": 2
                },
                "end": {
                  "line": 2,
                  "column": 12,
                  "offset": 34
                }
              }
            }
          ],
          "position": {
            "start": {
              "line": 1,
              "column": 3,
              "offset": 2
            },
            "end": {
              "line": 2,
              "column": 12,
              "offset": 34
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 2,
          "column": 12,
          "offset": 34
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 2,
      "column": 12,
      "offset": 34
    }
  }
}

https://astexplorer.net/#/gist/10d8f7b32e0123fee005f49d8d75defa/6017a37f56a4098ff295b3c4730a217ef8132089

Notice that the title part of the callout exists as a paragraph in the mdast. According to the CommonMark specifications, only inline elements are allowed as children of a paragraph. Therefore, block elements cannot exist within a paragraph, making it impossible to use block elements as the title of a callout.

Furthermore, the heading included in the title part exists as text within the mdast. To recognize this as a heading element, the plugin would need to re-parse this string with remark, which is somewhat difficult to do accurately on the plugin side.

I am in favor of adding this feature, so if anyone is able to implement it, please feel free to mention me or send a PR. Thank you for proposing this feature!