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

editorState.toHTML()会造成部分信息丢失 #278

Closed
weihomechen opened this issue Oct 29, 2018 · 10 comments
Closed

editorState.toHTML()会造成部分信息丢失 #278

weihomechen opened this issue Oct 29, 2018 · 10 comments

Comments

@weihomechen
Copy link

hi,我遇到一个问题,请帮忙看看,🙏

保存成html或raw格式,获取时使用

// content为保存的内容
const editorState = EditorState.createFrom(content);
const contentHtml = editorState.toHTML();

发现经过toHTML()转换后,得到的contentHtml会造成部分信息丢失,比如data-lang这些,不知道是不是我使用姿势有问题?

image

image

@margox
Copy link
Owner

margox commented Oct 29, 2018

你是使用了代码高亮扩展模块么?

@weihomechen
Copy link
Author

嗯,是的

@weihomechen
Copy link
Author

保存到数据库,再获取时这些信息还在,是经过toHTML后发现丢失了

@margox
Copy link
Owner

margox commented Oct 29, 2018

我知道了,editorState属性在传入到编辑器组件内部后,编辑器会对这个editorState进行一次setConvertOptions操作,来保证扩展模块里面的转换规则对这个editorState生效,如果editorState不是通过编辑器的onChange获得的话,那转换规则就不会对其生效,这似乎是一个很严重的问题,我尽快解决一下

@margox
Copy link
Owner

margox commented Oct 29, 2018

@weihomechen 发布了2.1.23,更新一下试试

@weihomechen
Copy link
Author

weihomechen commented Oct 29, 2018

@margox 辛苦了,经过toHTML()信息不会丢失了。 我想请教一下,得到html后,怎样展示在页面中比较好? 我使用reactdangerouslySetInnerHTML发现 code 内的 <br/> 会被过滤掉,无法实现换行的效果。
比如

 const editorState = EditorState.createFrom(content);
 const contentHtml = editorState.toHTML();
// contentHtml: <pre data-lang="javascript" class="lang-javascript"><code class="lang-javascript">const a = 1;<br/>const b = 2;</code></pre>

在jsx中使用:

 <div className={styles.contentContainer} dangerouslySetInnerHTML={{ __html: contentHtml }} />

目前效果:

image

如果手动编辑html,插入<br>,则是期望的效果:

image

如果您有相关的建议,还望赐教,谢谢

@margox
Copy link
Owner

margox commented Oct 29, 2018

你是在展示页用了prismjs进行高亮展示么?可以参考PrismJS/prism#598 (comment)
亲测有效

@weihomechen
Copy link
Author

好的~谢谢哈

@margox
Copy link
Owner

margox commented Oct 29, 2018

我去到扩展包的README里面去加个说明

@weihomechen
Copy link
Author

我就关闭这个issue啦😊

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

No branches or pull requests

2 participants