WYSIWYG - 文本和代码编辑器

发布于 2025-01-04 06:47:49 字数 2018 浏览 4 评论 0原文

我有一个需求,我确信其他开发人员也一定有这个需求,而 StackOverflow 也确实有。

场景

我正在构建一个网站来发布代码示例,这些文章是我通过管理系统编写的,也可能是前端注册用户的文章。

使命

拥有一个所见即所得的编辑器,用户可以发布文章。显然,它可能是一段文本、一些标题……这不是问题,因为 TinyMCE 目前可以处理这个问题。 与文本混合的将是代码示例,最好是在代码标签中。

执行

所以我一直在尝试扩展TinyMCE。 我可以允许代码标签,但无法让它将突出显示的文本放入代码标签中,但可以将其放入预标签中。不是问题。 因此,将:粘贴

<xsl:template match="*" mode="jsonObjectOrElementProperty">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text>":</xsl:text>
    <xsl:apply-templates select="." mode="jsonObjectProperties"/>
  </xsl:template>

到 WYSIWIG 中并突出显示并选择“预格式化”即可,它将所有括号编码为 <和>那很好。它存储在我的数据库中并以这样的方式输出;

<pre>&lt;xsl:template match="*" mode="jsonObjectOrElementProperty"&gt;<br /> &lt;xsl:text&gt;"&lt;/xsl:text&gt;<br /> &lt;xsl:value-of select="name()"/&gt;<br /> &lt;xsl:text&gt;":&lt;/xsl:text&gt;<br /> &lt;xsl:apply-templates select="." mode="jsonObjectProperties"/&gt;<br /> &lt;/xsl:template&gt;</pre>

但是在编辑时,请使用现有内容加载文本区域;参见 HTML;

<textarea class="tinyMCE"><pre>&lt;xsl:template match="*" mode="jsonObjectOrElementProperty"&gt;<br /> &lt;xsl:text&gt;"&lt;/xsl:text&gt;<br /> &lt;xsl:value-of select="name()"/&gt;<br /> &lt;xsl:text&gt;":&lt;/xsl:text&gt;<br /> &lt;xsl:apply-templates select="." mode="jsonObjectProperties"/&gt;<br /> &lt;/xsl:template&gt;</pre></textarea>

TinyMCE 重新编码所有括号并删除所有和任何基于 XML 的代码、脚本标记作为无效元素,而不是将它们视为纯文本。

除此之外,它没有任何 CDATA 功能,因此它会忽略某些部分...

我需要一个插件,因为我无法从头开始编写它,我没有时间也没有预算。 StackOverflow 到底让我在创建这个问题时做了什么! 很好的 Stackoverflow

问题

有没有人对 TinyMCE 进行过这样的操作? 我可以窃取/借用 Stackoverflows 吗?检查源代码看不出它是一个插件,更多的是一个定制的东西。 有人知道任何其他允许代码片段的格式化插件吗?

I have a need, something I'm sure other developers must have and StackOverflow does have.

Scenario

I'm building a site to post code examples on, articles that are written by me through an admin system but also possibly Front-end reg'd users.

Mission

To have a WYSIWYG editor that a user can post articles. Obviously it may be a paragraph of text, some headings... not a problem as TinyMCE currently handles this.
Mixed in with the text will be code examples, preferably in a code tag.

Execution

So I have been playing with extending TinyMCE.
I can allow code tags, but cannot get it to put highlighted text into code tags, but can get it into pre tags. Not a problem.
So pasting:

<xsl:template match="*" mode="jsonObjectOrElementProperty">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text>":</xsl:text>
    <xsl:apply-templates select="." mode="jsonObjectProperties"/>
  </xsl:template>

into the WYSIWIG and highlighting and selecting Preformatted is ok, it encodes all the brackets to < and > and that is perfectly fine. It's stored in my DB and comes out as such;

<pre><xsl:template match="*" mode="jsonObjectOrElementProperty"><br /> <xsl:text>"</xsl:text><br /> <xsl:value-of select="name()"/><br /> <xsl:text>":</xsl:text><br /> <xsl:apply-templates select="." mode="jsonObjectProperties"/><br /> </xsl:template></pre>

BUT when editing, so loading up the textarea with existing content; see HTML;

<textarea class="tinyMCE"><pre><xsl:template match="*" mode="jsonObjectOrElementProperty"><br /> <xsl:text>"</xsl:text><br /> <xsl:value-of select="name()"/><br /> <xsl:text>":</xsl:text><br /> <xsl:apply-templates select="." mode="jsonObjectProperties"/><br /> </xsl:template></pre></textarea>

TinyMCE re-encodes all the brackets and removes ALL and ANY XML based code, Script tags as non-valid elements, rather than treating them as plain text.

Add to this, that it has no-kind of CDATA functionality so that it ignores some parts...

I need a plugin, as I can't write it from scratch, I havn't the time nor budget. For what StackOverflow has exactly let me do in the creation of this question!! Good Stackoverflow

Question

Has anybody had any luck with doing this to TinyMCE?
Can I steal/borrow Stackoverflows? checking the source can't see that its a plugin, more of a custom built thing.
Does anybody know of any other Formatting plugins that allow for code snippets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2025-01-11 06:47:49

Codemirror - 可能是您的解决方案?

Codemirror - could be a solution for you?

爱人如己 2025-01-11 06:47:49

我认为您需要调整您的 tinymce init 设置 valid_elements 和 valid_children (也不要忘记您的属性)。

I think you will need to adjust your tinymce init setting valid_elements and valid_children (don't forget your attributes there too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文