【发布时间】:2023-04-05 12:39:01
【问题描述】:
我正在构建 sn-p 块以在 <pre><code> 标记内添加代码。
但是,当查看代码时 - <pre> 标签有不必要的标签。
我的 sn-p 块反应代码
edit({ attributes, setAttributes, isSelected }) {
const { text } = attributes;
return (
<Fragment>
<RichText
identifier="text"
value={text}
placeholder={__('Text')}
onChange={nextText => {
setAttributes({
text: nextText,
});
}}
/>
</Fragment>
);
},
save({ attributes }) {
const { text } = attributes;
return (
<pre>
{text && (<code>{text}</code> )}
</pre>
);
},
};
从古腾堡编辑器插入的代码 - 用于测试。
Python program to illustrate destructor
class Employee:
Initializing
def __init__(self):
print('Employee created.')
Deleting (Calling destructor)
def __del__(self): print('Destructor called, Employee deleted.')
obj = Employee()
del obj
但是上面的测试 sn-p 被拉成:
渲染后的 sn-p 块,没有换行符和
标签。
问题源于 HTML 存储。存储期间格式错误的问题。
如何解决这个问题。这样代码就可以在<pre><code>块内用换行符存储和查看,而<pre>标签中没有不必要的类
【问题讨论】:
-
以下来自react docs 和WP stackexchange 的链接肯定会对您有所帮助。我建议仔细阅读它们
标签: javascript reactjs wordpress-gutenberg