可插入的、集中维护的文本/Markdown 片段

我在想是否有一种方法可以在一个地方定义一段文本,并通过一个简短的引用将其插入到帖子中。

这有点像系统自带的“管理 → 自定义 → 文本内容”,但适用于我自定义的变量,这些变量可以插入到帖子中。

我的使用场景是添加关于联盟链接的免责声明。随着多个帖子和主题中联盟链接数量的增加,我发现免责声明也随之 proliferating(不断增多)。我偶尔希望对免责声明进行改进,但随着数量增加,这项工作变得越来越繁琐。

我还在考虑让我的联盟链接更加通用,这样每个产品链接都有一个集中管理的链接(例如,该链接会出现在多个地方)。如果该产品销售链接发生变化,我只需要在其中一个地方进行修改。

显然,我可能可以使用某种 Rails 的查找和替换功能,但这不够简洁、安全,也无法实现“在线”更新。

因此,我希望有一个地方可以定义和维护一段文本,并在下一次页面加载时,所有使用该文本的地方都能立即反映更改。

继续阅读 @Johani 的解决方案!

3 个赞

一种可能的选择是使用主题组件。

这应放在 header.html

<script type="text/discourse-plugin" version="0.8">
var disclaimer = settings.Disclaimer_text,
  disclaimer_selector = 'div[data-theme="disclaimer"]';

$.fn.disclaimer = function() {
  if (!this.length) {
    return;
  } else {
    this.each(function() {
      $(this).html(disclaimer);
    });
    return this;
  }
};

api.decorateCooked($elem => $elem.children(disclaimer_selector).disclaimer());

// 创建作曲家按钮
let currentLocale = I18n.currentLocale();

I18n.translations[currentLocale].js.disclaimer_button = "添加免责声明";
I18n.translations[currentLocale].js.composer.disclaimer_prompt = ""; // 留空

api.onToolbarCreate(function(toolbar) {
  toolbar.addButton({
    trimLeading: true,
    id: "disclaimer-button",
    group: "insertions",
    icon: "exclamation-circle", // 更改图标
    title: "disclaimer_button",
    perform: function(e) {
      return e.applySurround(
        '<div data-theme="disclaimer">',
        "</div>",
        "disclaimer_prompt"
      );
    }
  });
});
</script>

然后将其放入组件的 settings.yml 文件中

Disclaimer_text:
  default: "Lorem ipsum dolor sit amet, ei purto complectitur has, per at quas senserit. Et malis libris eos, vix id pericula dissentiet, aliquid apeirian pro eu. Sed ex viderer inciderint, vitae officiis dissentiet eos no. Omnes percipit singulis in has, ne nam nibh tation inciderint. Quas nulla ei sit, ex eam rebum voluptaria. Id eam altera similique. Ex justo assentior persequeris mea, ei hinc paulo ubique mei. <br><br>Cu nam epicurei torquatos, et accusam accommodare vim. Vis sint saperet officiis et. Ad consequat posidonium mea, et duo paulo quidam maluisset, vel an electram expetendis. An vis repudiare tincidunt, mentitum convenire eloquentiam ut vis, summo partiendo pro ad."
  description:
    en: 输入所需的免责声明文本。使用 &lt;br&gt; 进行换行。

然后,您将在作曲家中看到一个如下所示的按钮:

点击该按钮将添加

<div data-theme="disclaimer"></div>

该内容将根据您在组件设置中定义的内容进行转换

最终结果如下所示:

您可以在 CSS 中使用

[data-theme="disclaimer"] {

}

以您喜欢的方式对其进行样式设置。

21 个赞

That’s awesome! Thanks @Johani

Added to Tips’n’Tricks.

2 个赞

Hi @merefield - funnily enough I was looking for similar functionality - inserting a standard snippet of text - but rather than a button I wanted a trigger string that would expand out to a piece of markdown. (OK, I suppose alternatively I could modify the button to a dropdown with a few options)

This is probably not a core Discourse feature but a plugin.

Ideal behaviour:

  • I type :stub: (I don’t mind about using something instead of the delimiting : characters, but wondered if hooking into the emoji interface might work since that has the kind of ‘completion’ type behaviour that I’d like)

  • Discourse replaces :stub: with

    > This article is a stub - please improve it by  **editing**  and  **adding links**  and  **detail**  (it’s a ‘wiki’ so any user can edit and add information) or by  **commenting**  below.
    
  • which is rendered as

    This article is a stub - please improve it by editing and adding links and detail (it’s a ‘wiki’ so any user can edit and add information) or by commenting below

  • and there should be somewhere in the UI to configure these snippets, a bit like the Custom Emoji interface

5 个赞

Great idea for a plugin! Would you be ok for the text to appear only in the preview and the final cooked view?

4 个赞

@pacharanero 的建议对我们来说也太棒了。在我们的案例中,它们将仅供员工使用,因此我们不希望在编辑器工具栏中显示任何内容,但输入一个简短的代码来插入自动更新块将是惊人的 :+1:

1 个赞

目前,要实现此行为的一种非 Discourse、非中心化管理的方式是使用跨平台剪贴板管理器,例如 CopyQ,并设置“命令/全局快捷方式”部分,以便您可以将任意文本插入剪贴板。

然后,这些片段可以在 CopyQ UI 中进行管理、分配给热键,或出现在右键单击的 CopyQ 菜单中。这不像嵌入 Discourse 的插件那么好用,但它的优点是可以在所有应用程序中工作,而且已经存在!

(请注意,这需要在您希望以这种方式使用的每台计算机上进行设置。您可以将这些命令和全局快捷方式导出和导入到文件中,以便在多台计算机或团队之间设置相同的设置)

对我来说完全没问题。我认为与 DiscoTOC 和 Placeholders 不同,它_确实_需要在预览窗格中正确渲染,但在编辑器中,它可以简单地显示所选的任意 :slug: 触发短语。

感谢您的回复。CopyQ 解决方案似乎不符合我的要求(至少我认为不符合)。

例如,我想在帖子中输入一个片段(例如 :slug:),然后在渲染时,它会渲染此 slug 的当前内容(其中可能包含 markdown)。如果我更改此 slug 的内容,那么所有使用它的帖子都会立即更新为包含新内容(帖子本身只包含 slug,内容在渲染时被切换进来)。

即使搜索不知道内容的存在,我也没关系(只显示最终的已处理版本对我来说就足够了)。

祝好!

这里是另一种方法……

1 个赞