Skip to content

Expose defaultHandlers to be reused #67

@JounQin

Description

@JounQin

Initial checklist

Problem

Maybe a bit similar to syntax-tree/mdast-util-to-markdown#34

I'm trying to implement a tiny util cf2md which Transform from confluence flavored HTML to Markdown with enhanced features.

In confluence's HTML, pre can be used without code inside, so I have to wrap its direct text nodes into a code node to reuse original h.handlers.pre, but there is no defaultHandlers provided, so I have to use import { code } from 'hast-util-to-mdast/lib/handlers/code.js' which is the original h.handlers.pre actually.

import { code } from 'hast-util-to-mdast/lib/handlers/code.js'

unified()
  // ...
  .use(rehypeRemark, {
    handlers: {
      pre(h, node, parent) {
        // I'd like to have `h.defaultHandlers.pre` here
        return code(h, {
          ...node,
          children: node.children.map(child =>
            child.type === 'text'
              ? {
                  type: 'element',
                  tagName: 'code',
                  children: [child],
                }
              : child,
          ),
        })
      },
    },
  })

See also https://github.com/rx-ts/cf2md/blob/main/src/index.ts#L82

Solution

Expose defaultHandlers in h

Alternatives

N/A

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