-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v9 Access to text formatters from custom formatters #263
Comments
Hmm. I never considered the old I'm curious what use cases you see for them being accessible from client code? |
The example in the typing test driver is unrealistic. There could be other cases where the wrapper is checking the node name or data (emit an emoji before all headers starting with 'new' and then call the header formatter). Are there users? I don't know. Maybe @CarsonF can comment. He may have added the formatter support. |
Is it possible the formatters are accessible at |
That's how it works internally: node-html-to-text/packages/base/src/index.js Lines 151 to 152 in 8a2dde6
At that point it contains all built-in and custom formatters.
There was a demand for recursive formatters (#154) (Albeit it's not as significant now, after the introduction of selectors). I haven't got to think about it yet, but perhaps we might have everything for it already in place. |
As a POC, mocking up
allowed this to work:
I know this is passing the wrong options. If options are passed, do they replace the existing options or get merged? I guess merge would be consistent with how a selector's options are handled now. |
Yeah I think the goal was that formatters could wrap another formatter to patch functionality. |
From this comes naturally that
The example from { selectors: [
{ selector: 'table', format: 'dataTable' },
{ selector: 'table[role="presentation"]', format: 'block' }, // or 'inline'
] } I'm trying to come up with a no-nonsense example before I add tests and documentation for recursive formatters. |
(To think about it now, maybe I can deep freeze the HtmlToTextOptions object. It would've been nice if it could give some performance boost, but it doesn't seem to be the case.) |
Okay, but what if I wanted a merge? The FormatOptions can only be obtained via I am just trying to think about what someone wanting to do recursion might want. I am perfectly fine with doing nothing, especially since the known user of this functionality may no longer need it. The call in the POC looks like a hack. |
formatters: {
'foo': function (elem, walk, builder, formatOptions) {
if (elem...) {
walk(elem.children, builder);
} else {
builder.options.formatters['bar'](elem, walk, builder, { ...formatOptions, a: 42 });
}
}
},
selectors: [
{
selector: 'div',
format: 'foo', // can call 'bar' inside
options: { ... } // any format options meaningful for your custom 'foo' formatter with its internals,
// defined here for 'div' selector specifically
}
] Since you as the author of the
There are really no connections between different selectors, nothing to merge with from within the formatter. It might be confusing ¯\_(ツ)_/¯ |
Yes it can be. That is why I chose h3, a well-known selector in my example for merging. No ambiguity as to whether it exists but the format options are not available. Not asking for changes here. Maybe this will help someone in the future who is considering calling other formatters. I will finish up the typescript v9 support once the tests and doc are available. I can run pre-release tests if necessary. Thanks for your responsiveness to this. |
Also have to wait for another typing to be updated. 🙄 |
I might be able to make it closer in behavior to cascade style sheets at some point in the future. Will try to do tests and docs asap. |
done |
Thanks for getting the updates done. The test code didn't quite have full coverage so I am reviewing that now. Should get the typings done this week. |
The use of |
Thank you for QA |
PR 63576 has been opened. |
@CarsonF the typing updates for v9 may break your code but could you please take a look at them? @KillyMXI suggested a simple fix. |
Congrats on the new release. Thanks for your work on this.
I am working on updating the typescript typings. One feature of the typings was access to the formatter functions. They were exported here. I don't use the functions but they are part of the test program:
I am assuming this was part of the test because it was needed and I can still imagine a use case for it.
The old documentation does not mention being able to access the formatters. At least I do not recall that it did. I am opening this issue in case someone is using the formatters.
Questions:
The current typescript typings work with the new release except for the formatters and removed deprecated options.
The text was updated successfully, but these errors were encountered: