Skip to content
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

RFC: Allow $generateHtmlFromNodes to not export classes #6968

Open
ivailop7 opened this issue Dec 16, 2024 · 1 comment
Open

RFC: Allow $generateHtmlFromNodes to not export classes #6968

ivailop7 opened this issue Dec 16, 2024 · 1 comment
Labels
enhancement Improvement over existing feature

Comments

@ivailop7
Copy link
Collaborator

ivailop7 commented Dec 16, 2024

Lexical: 0.21

export function $generateHtmlFromNodes(
editor: LexicalEditor,
selection?: BaseSelection | null,
): string {
if (
typeof document === 'undefined' ||
(typeof window === 'undefined' && typeof global.window === 'undefined')
) {
throw new Error(
'To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.',
);
}
const container = document.createElement('div');
const root = $getRoot();
const topLevelChildren = root.getChildren();
for (let i = 0; i < topLevelChildren.length; i++) {
const topLevelNode = topLevelChildren[i];
$appendNodesToHTML(editor, topLevelNode, container, selection);
}
return container.innerHTML;
}

The currently exported HTML would contain theme classes, which in many cases would mean something only to the current application that provides them in its theme configuration and add no meaningful value to any external copy/paste use case. Since the classes would be reapplied on editor state reparsing. It makes sense to allow skipping the export of the 'class' attribute when generating the HTML. It also has benefit in terms of smaller HTML footprint for storage, for those that store the HTML instead of the EditorState.

Edit: I do think, we should keep the 'style' attribute or make it extra configurable and not couple 'class' and 'style' together.

@ivailop7 ivailop7 added the enhancement Improvement over existing feature label Dec 16, 2024
@etrepum
Copy link
Collaborator

etrepum commented Dec 16, 2024

Maybe what we should do is have a more flexible system for configuring this kind of thing across the board. The EditorConfig allows you to specify one set of html.export configurations but maybe your app has multiple use cases for exportDOM (serialization, copy/paste, rendering an inert editor, some other export use case). Some kind of config argument that contains this intent enum (like excludeFromCopy, but actually implemented consistently) that's passed to exportDOM would be helpful. For export map override we could have an additional option passed to call the default implementation kinda like middleware since node.exportDOM may have an override already in EditorConfig.

Another thing to assist with this use case would be returning DOM instead of an HTML string so the user could querySelectorAll a quick solution to this for their use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement over existing feature
Projects
None yet
Development

No branches or pull requests

2 participants