markdown-it / markdown-it-container

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

set correct nesting when adding container #29

Closed arve0 closed 5 years ago

arve0 commented 5 years ago

To reproduce:

let md = require('markdown-it')();
let container = require('markdown-it-container');

md.use(container, 'column');

let src = `
::: column
Lorem ipsum
:::
`;

console.log(md.render(src));

gives tokens:

[
  Token {type: "container_column_open", tag: "div", attrs: null, map: Array(2), nesting: 1, …},
  Token {type: "paragraph_open", tag: "p", attrs: null, map: Array(2), nesting: 1, …}
  ...
]

Note that div and p both have nesting === 1, which is incorrect, as p is inside div.

This results in https://github.com/arve0/markdown-it-attrs/issues/72, attributes on the wrong opening tag.

rlidwka commented 5 years ago

Note that div and p both have nesting === 1, which is incorrect, as p is inside div.

It is correct. Nesting does not represent a level of nesting, it represent a change of level of nesting.

See here for more details.

I believe what you are looking for is level property which is hidden behind in your dump.

Full dump of tokens:

[ Token {
    type: 'container_column_open',
    tag: 'div',
    attrs: null,
    map: [ 1, 3 ],
    nesting: 1,
    level: 0,
    children: null,
    content: '',
    markup: ':::',
    info: ' column',
    meta: null,
    block: true,
    hidden: false },
  Token {
    type: 'paragraph_open',
    tag: 'p',
    attrs: null,
    map: [ 2, 3 ],
    nesting: 1,
    level: 1,
    children: null,
    content: '',
    markup: '',
    info: '',
    meta: null,
    block: true,
    hidden: false },
  Token {
    type: 'inline',
    tag: '',
    attrs: null,
    map: [ 2, 3 ],
    nesting: 0,
    level: 2,
    children: [ [Token] ],
    content: 'Lorem ipsum',
    markup: '',
    info: '',
    meta: null,
    block: true,
    hidden: false },
  Token {
    type: 'paragraph_close',
    tag: 'p',
    attrs: null,
    map: null,
    nesting: -1,
    level: 1,
    children: null,
    content: '',
    markup: '',
    info: '',
    meta: null,
    block: true,
    hidden: false },
  Token {
    type: 'container_column_close',
    tag: 'div',
    attrs: null,
    map: null,
    nesting: -1,
    level: 0,
    children: null,
    content: '',
    markup: ':::',
    info: '',
    meta: null,
    block: true,
    hidden: false } ]