posthtml/posthtml-render

Node type not exported

lazd opened this issue · 1 comments

lazd commented

The same way postcss-parser exports NodeText and NodeTag, I would export posthtml-render to export Node. This would be helpful when constructing objects for use with posthtml-render.

Details

My use case is as follows:

  1. Manually create an entire Node tree in a TypeScript project (no parsing involved)
  2. Render the tree, return the HTML

I would expect to be able to do something like this:

import { Node, render } from 'posthtml-render';

const node: Node = {
  tag: 'div',
  content: [
    { tag: 'div', content: 'Hi' }
  ]
};

const html = render(node);

Instead, I have to do something like:

import render from 'posthtml-render';

import { NodeText, NodeTag } from 'posthtml-parser';

// re-declare the node union type manually (will be compatible since closeAs is optional)
type Node = NodeText | NodeTag;

const node: Node = {
  tag: 'div',
  content: [
    { tag: 'div', content: 'Hi' }
  ]
};

const html = render(node);

Of course, I could omit the Node type completely and just use an implicit any or object, but it is a better development experience to know the type of the object I'm constructing.

I realize that posthtml-render is mainly just a dependency of posthtml and this use case is probably uncommon, but this would open up some additional uses of the package.

Environment

OS node yarn package
macOS 10.15.7 v14.17.1 v1.22.4 v2.0.6

Note: I had to work around #62 to test the above code sample (change types to dist/index.d.ts in node_modules/posthtml-render/package.json).

lazd commented

Thanks for the quick fixes, @Scrum!