#how to convert with HTMLConverterFeature so it will conver /n to <br>?
26 messages · Page 1 of 1 (latest)
Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
Documentation:
- Lexical Rich Text - Migrating from Slate - Migration via migration script
- Lexical Rich Text - Converters - Lexical => HTML - Creating your own HTML Converter
- Lexical Rich Text - Converters - Lexical => HTML - Generating HTML anywhere on the server:
- Lexical Rich Text - Converters - Lexical => HTML
- Lexical Rich Text - Converters - HTML => Lexical
you can achieve this by quickly modifying the parser on text objects
so once you know that the lexical represents text
you can just treat it like a string, and split on '\n'
that result is now an array, so you intersperse a <br> for every item in that array
not really. i did a hack on the react app for something like this:
<div
dangerouslySetInnerHTML={{ __html: product.descriptionHtml.replace(/\n/g, '<br>') }}
/>
\n is a linebreak node in Lexical (you can enable TreeViewFeature to quickly debug this)
so you need to provide a renderer that handles line breaks
(my renderer example handles linebreaks)
yeah same logic
@limpid nexus as a newbie on payload, do you have a code example of this renderer?
what renderer are you using?
below is my collection:
{
name: 'description',
type: 'richText',
editor: lexicalEditor({
features: ({ defaultFeatures }:any) => [
...defaultFeatures,
// The HTMLConverter Feature is the feature which manages the HTML serializers.
// If you do not pass any arguments to it, it will use the default serializers.
HTMLConverterFeature({}),
],
}),
},
lexicalHTML('description', { name: 'descriptionHtml' }),
as in, you want to render to html and not reactnode?
here's my custom converter that does what you're asking, L15-17
// custom paragraph converter that respects align and indent https://github.com/payloadcms/payload/issues/5146
export const FormattedParagraphHTMLConverter: HTMLConverter<any> = {
async converter({ converters, node, parent }) {
const childrenText = await convertLexicalNodesToHTML({
converters,
lexicalNodes: node.children,
parent: {
...node,
parent,
},
})
// nb without this, we get `<p></p>` between each paragraph, impossible to do a hard newline
// might be a payload lexical bug?
if (/^\s*$/.test(childrenText)) {
return '<br>'
}
const pTag = styleOpeningTagForNode(node, 'p')
return `${pTag}${childrenText}</p>`
},
nodeTypes: ['paragraph', 'linebreak', 'customParagraph'],
}
I have a linebreak converter that only handles linebreaks
actually yes it's good if not using reaactnode as suggested by @limpid nexus
i will give a try thank you guys really helpfull!
in my case (and maybe OP's), certain linebreaks within a paragraph were getting converted to empty paragraphs rather than linebreaks during HTML conversion. I added this fix early on in my project and never really revisited. in hindsight it feels a little funky, i wonder if there's a more "proper" fix
Yeah that can sometimes happen, using Shift+Enter fixes the issue
I wonder if this is a behavior that can be a toggle option
Ya im planning on polishing / organizing my random collection of fixes like this and publishing to npm, maybe with some option like that
I think this issue may be related to importing from google docs
I think you should report these bugs to lexical