Describe the bug
The current logic for applying block-level attributes is too strict, as it assumes the children array will contain exactly two elements: a text node and the props token.
if (
token.type === 'inline'
&& token.children?.length === 2
&& token.children[0].type === 'text'
&& token.children[1].type === 'mdc_inline_props'
) {
// ... logic to apply props
}
However, when the line also contains other inline Markdown syntax like bold or code, markdown-it parses these into additional inline-level tokens, causing the children array to have more than two elements.
For example, after calling state.push('mdc_inline_props', 'span', 0), the state.tokens array may look like:
// For "**markdown bold**{.text-red}"
state.tokens = [
{ type: 'strong_open', markup: '**', ... },
{ type: 'text', content: 'markdown bold', ... },
{ type: 'strong_close', markup: '**', ... },
{ type: 'mdc_inline_props', ... }
]
// For "markdown syntax `inline block`{.text-red}"
state.tokens = [
{ type: 'text', content: 'markdown syntax', ... },
{ type: 'code_inline', content: 'inline block', ... },
{ type: 'mdc_inline_props', ... }
]
As a result, the attributes intended for the parent block element are incorrectly applied to the immediately preceding inline element.
Markdown content
**markdown bold**{.text-red}
markdown syntax `inline block`{.text-red}
Actual HTML Rendered
<p><strong class="text-red">markdown bold</strong></p>
<p>markdown syntax <code class="text-red">inline block</code></p>
Expected HTML Rendered
<p class="text-red"><strong>markdown bold</strong></p>
<p class="text-red">markdown syntax <code>inline block</code></p>
Reproduction
PR #16
System Info
System:
OS: macOS 15.1.1
CPU: (8) arm64 Apple M2
Memory: 65.80 MB / 16.00 GB
Shell: 5.9 - /opt/homebrew/bin/zsh
Binaries:
Node: 22.5.1 - ~/.nvm/versions/node/v22.5.1/bin/node
npm: 10.8.2 - ~/.nvm/versions/node/v22.5.1/bin/npm
pnpm: 9.1.3 - ~/Documents/Slidev/markdown-it-mdc/node_modules/.bin/pnpm
bun: 1.2.6 - ~/.bun/bin/bun
Browsers:
Chrome: 137.0.7151.120
Safari: 18.1.1
Used Package Manager
pnpm
Validations
Contributions
Describe the bug
The current logic for applying block-level attributes is too strict, as it assumes the children array will contain exactly two elements: a text node and the props token.
However, when the line also contains other inline Markdown syntax like bold or
code, markdown-it parses these into additional inline-level tokens, causing the children array to have more than two elements.For example, after calling
state.push('mdc_inline_props', 'span', 0), thestate.tokensarray may look like:As a result, the attributes intended for the parent block element are incorrectly applied to the immediately preceding inline element.
Markdown content
Actual HTML Rendered
Expected HTML Rendered
Reproduction
PR #16
System Info
System: OS: macOS 15.1.1 CPU: (8) arm64 Apple M2 Memory: 65.80 MB / 16.00 GB Shell: 5.9 - /opt/homebrew/bin/zsh Binaries: Node: 22.5.1 - ~/.nvm/versions/node/v22.5.1/bin/node npm: 10.8.2 - ~/.nvm/versions/node/v22.5.1/bin/npm pnpm: 9.1.3 - ~/Documents/Slidev/markdown-it-mdc/node_modules/.bin/pnpm bun: 1.2.6 - ~/.bun/bin/bun Browsers: Chrome: 137.0.7151.120 Safari: 18.1.1Used Package Manager
pnpm
Validations
Contributions