Skip to content

Empty directive labels are dropped on stringify, merging with following text #15

Closed

Description

@ChristianMurphy

Initial checklist

Affected package

mdast-util-directive 3.1.0, mdast-util-to-markdown 2.1.2, mdast-util-from-markdown 2.0.3, micromark-extension-directive 3.0.2

Steps to reproduce

import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {directive} from 'micromark-extension-directive'
import {directiveFromMarkdown, directiveToMarkdown} from 'mdast-util-directive'

const input = ':a[]a.'
const opts = {extensions: [directive()], mdastExtensions: [directiveFromMarkdown()]}
const tree1 = fromMarkdown(input, opts)

tree₁:

{
  "type": "paragraph",
  "children": [
    { "type": "textDirective", "name": "a", "children": [] },
    { "type": "text", "value": "a." }
  ]
}

stringify:

const text2 = toMarkdown(tree1, {extensions: [directiveToMarkdown()]})
// text2: ':aa.\n'

Re-parse:

tree₂:

{
  "type": "paragraph",
  "children": [
    { "type": "textDirective", "name": "aa", "children": [] },
    { "type": "text", "value": "." }
  ]
}

The directive's name has changed from "a" to "aa" (it consumed the
following a). Rendered HTML differs accordingly:

<!-- tree1 -->
<p><span class="directive-a"></span>a.</p>

<!-- tree2 -->
<p><span class="directive-aa"></span>.</p>

Actual behavior

Empty-label directives merge with adjacent text on re-parse.

Expected behavior

Either always emit [] for directives with empty children, or emit enough
trailing context (a space, a zero-width non-joiner, a reference escape) to
prevent the name from merging with following text. The round-trip must
preserve name.

Runtime

node v24.13.1

Package manager

No response

Operating system

Ubuntu (WSL2)

Build and bundle tools

No response

Activity

  1. added
    👋 phase/newPost is being triaged automatically
    🤞 phase/openPost is being triaged manually
    and removed
    👋 phase/newPost is being triaged automatically
    on Apr 24, 2026
  2. github-actions commented on Oct 3, 2026

    @github-actions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions