# 我的 Markdown 扩展在预览中有效，但在渲染后无效

**URL:** <https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544>\
**Category:** Development\
**Created:** [2019年二月6日 12:20 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544 "2019-02-06T12:20:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scottf/32/231686_2.png) [@scottf](https://meta.discourse.org/u/scottf)\
**Post date:** [2019年二月6日 12:20 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/1 "2019-02-06T12:20:34Z")

</div>

I have followed these instructions to create a Block BBCode markdown extension for Discourse.

The plugin works when previewing and editing a topic. However, when I actually post the Topic, the raw BBCode mark up appears in the post completely unprocessed.

Is there something I need to do to make sure the plugin operates on posted topics?

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2019年二月6日 14:12 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/2 "2019-02-06T14:12:31Z")

</div>

I’ve had this happen due to importing modules using Javascript `import` statement, and the differences in paths between the browser and the mini\_racer.

Can you share your plugin code so we can help you to debug?

---

<div class="post-metadata">

**Author:** ![scottf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scottf/32/231686_2.png) [@scottf](https://meta.discourse.org/u/scottf)\
**Post date:** [2019年二月6日 14:32 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/3 "2019-02-06T14:32:40Z")

</div>

Thank you for the reply Rafael.

The plugin’s code is here:

> **[Text Blaze / Discourse Snippets · GitLab](https://gitlab.com/textblaze/discourse-snippets)**
>
> GitLab.com

As you can see it is very simple. I am using ES Module imports, but only for adding a button to the editing bar. The Markdown extension ([Files · master · Text Blaze / Discourse Snippets · GitLab](https://gitlab.com/textblaze/discourse-snippets/blob/master/assets/javascripts/lib/markdown-it/snippets.js.es6)) doesn’t import anything.

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2019年二月6日 22:36 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/4 "2019-02-06T22:36:03Z")

</div>

I think this is simple naming issue due to:

> <https://github.com/discourse/discourse/blob/56a9f777cb54fdf1ce21fd605297d8f51a1398dd/lib/pretty_text.rb#L96-L98>

Move it from `markdown-it` to the `discourse-markdown` directory.

---

<div class="post-metadata">

**Author:** ![scottf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scottf/32/231686_2.png) [@scottf](https://meta.discourse.org/u/scottf)\
**Post date:** [2019年二月7日 07:33 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/5 "2019-02-07T07:33:00Z")

</div>

> [@sam](#):
>
> Move it from `markdown-it` to the `discourse-markdown` directory.

That fixed it. Thank you Sam.

---

<div class="post-metadata">

**Author:** ![sirdavidoff](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sirdavidoff](https://meta.discourse.org/u/sirdavidoff)\
**Post date:** [2021年十月11日 16:24 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/6 "2021-10-11T16:24:14Z")

</div>

我在尝试覆盖围栏渲染器（`md.renderer.rules.fence`）时遇到了同样的问题——它在预览窗口中有效，但在提交后会显示默认的 Discourse 渲染结果。

以下是代码：

```plaintext
export function setup(helper) {
  if (!helper.markdownIt) return;

  helper.allowList(['div.embedded_workflow', 'div.embedded_tip']);

  helper.registerPlugin((md) => {
    md.renderer.rules.fence = function (tokens, idx, options, env, slf) {
      let content = tokens[idx].content;
      return '<div class="embedded_workflow">' +
               '<div class="embedded_tip">将此代码复制并粘贴到 n8n 中以体验工作流</div>' +
               tokens[idx].content +
             '</div>'
    };
  });

}

```

您也可以在[此处](https://github.com/n8n-io/discourse-n8n-wf)查看完整的插件结构。任何帮助都将不胜感激！

---

<div class="post-metadata">

**Author:** ![carkod](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@carkod](https://meta.discourse.org/u/carkod)\
**Post date:** [2022年十二月9日 23:13 UTC](https://meta.discourse.org/t/my-markdown-extension-works-in-preview-but-not-on-cooked/108544/7 "2022-12-09T23:13:34Z")

</div>

它曾经对你有用吗？

在我的例子中，它在正确的文件夹 `/assets/javascripts/discourse-markdown` 中，但仍然只在预览中显示。

对其他人来说，它过了一会儿才起作用，这让我认为他们重新加载了服务器。我也这样做了，但没有成功。
