挿入可能、中央管理型テキスト/Markdown スニペット

テキストの文字列を単一の場所で定義し、投稿内に短い参照1つで挿入する方法はないでしょうか。

標準機能の「管理 → カスタマイズ → テキストコンテンツ」に少し似ていますが、投稿に挿入できる独自のカスタム変数のようなものです。

私のユースケースは、アフィリエイトリンクに対する免責事項の追加です。多くの投稿やトピックにアフィリエイトリンクが増えるにつれて、免責事項も同様に増殖していることに気づきました。時折、免責事項を改善したいと思うことがありますが、数が多くなるほどその作業は負担になっていきます。

また、アフィリエイトリンクをより汎用的にして、各製品リンクに対して一元管理されたリンク1つ(それが複数箇所に現れる場合でも)を持つことも検討しています。もし製品リンクが変更された場合、1か所だけを変更すれば済みます。

もちろん、Rails の検索と置換のような何らかの方法で対応できるかもしれませんが、それはクリーンでも安全でもなく、「オンライン」での運用としても適切ではありません。

そこで、テキスト文字列を単一の場所で定義・管理し、変更が次回ページ表示時にすべての使用箇所に即座に反映される場所が欲しいと思いました。

以下、@Johani による解決策をご覧ください!

「いいね!」 3

考えられるオプションの1つは、テーマコンポーネントを使用することです。

これは 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());

// composer ボタンの作成
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; を使用します。

これにより、composer に以下のようなボタンが表示されます。

このボタンをクリックすると、以下のコードが追加されます。

<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:」)を入力し、それがレンダリングされるたびに、このスラグの現在のコンテンツ(マークダウンが含まれる可能性がある)がレンダリングされるようにしたいのです。このスラグのコンテンツを変更すると、これを使用するすべての投稿は、新しいコンテンツを持つように即座に更新されます(投稿自体にはスラグのみが含まれ、コンテンツはレンダリング時に切り替えられます)。

検索がコンテンツの存在を知らなくても構いません(最終的に調理されたバージョンのみが表示されれば、それで十分です)。

Cheers

別のアプローチを試します…

「いいね!」 1