مقاطع نص أو ماركداون قابلة للإدراج ومُدارة مركزيًا

I was wondering if there was any way I could define a string of text in a single place and insert it within posts with a single, short reference.

It’s a bit like the out of the box Admin -> Customize -> Text Content, but for my own custom variables that can be inserted into posts.

My use case was adding a disclaimer for my affiliate links. As the number of affiliate links grew across many posts and Topics I noted that my disclaimers were proliferating along with them. I occasionally like to make improvements to the disclaimers and this gets more onerous as the number grows.

I’m also considering making my affiliate links more generic so I have one centrally managed link for each product link, for example, which appears multiple times. If that product link changes, I only have to change it in one place.

Obviously I might be able to do some kind of rails find & replace, but that’s not as clean/safe nor ‘online’.

So I wanted a place to define & maintain a text string in a single place and have changes immediately reflected everywhere that is used on next page view.

Read on for @Johani’s solution!:

3 إعجابات

One possible option is to use a theme component.

This goes in 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());

// create composer button
let currentLocale = I18n.currentLocale();

I18n.translations[currentLocale].js.disclaimer_button = "Add disclaimer";
I18n.translations[currentLocale].js.composer.disclaimer_prompt = ""; // leave empty

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

and this goes into your component’s settings.yml file

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: Enter desired disclaimer text. Use &lt;br&gt; for linebreaks.

You would then have a button in the composer that looks like so:

and clicking the button would add

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

which get’s converted to whatever you set in the component’s settings

and the end result looks like this

and you can use

[data-theme="disclaimer"] {

}

in CSS to style it however you like.

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، أو تعيينها لمفتاح اختصار، أو ظهورها في قائمة CopyQ بزر الماوس الأيمن. إنها ليست لطيفة تمامًا مثل إضافة مضمنة في Discourse، ولكن بعد ذلك لديها ميزة العمل عبر جميع التطبيقات، وهي موجودة بالفعل!

(لاحظ أنه سيتعين إعداد هذا على كل جهاز كمبيوتر ترغب في استخدامه بهذه الطريقة. يمكنك تصدير واستيراد هذه الأوامر والاختصارات العامة إلى ملف، لتمكين إعداد نفس الإعدادات على أجهزة متعددة أو عبر فريق)

سيكون ذلك مقبولاً تمامًا بالنسبة لي. أعتقد أنه على عكس DiscoTOC والعناصر النائبة، فإنه سيحتاج إلى عرضه بشكل صحيح في جزء المعاينة، ولكن في المنشئ يمكنه ببساطة عرض أي عبارة مشغلة :slug: تم اختيارها.

شكراً على الرد. حل CopyQ لا يفعل ما أبحث عنه (على الأقل، لا أعتقد ذلك).

على سبيل المثال، أود إدخال مقتطف في منشور (مثل :slug:) ثم كلما تم عرضه، يتم عرض المحتوى الحالي لهذا الـ slug (والذي قد يحتوي على markdown). إذا قمت بتغيير محتوى هذا الـ slug، فسيتم تحديث جميع المنشورات التي تستخدمه على الفور للحصول على المحتوى الجديد (يحتوي المنشور نفسه على الـ slug فقط ويتم تبديل المحتوى عند العرض).

سأكون سعيدًا إذا لم يكن البحث يعرف بوجود المحتوى أيضًا (وإظهار النسخة النهائية المطهوة فقط جيد بالنسبة لي).

تحياتي

إليك نهج مختلف…

إعجاب واحد (1)