Skip to content

End-of-line attributes fail to apply to block tokens in the presence of inline Markdown #15

@kentrpg

Description

@kentrpg

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

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions