# (Superseded) Plugin Tutorial #1 - How to manipulate the text in the composer?

**URL:** https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925
**Category:** Development
**Created:** [November 13, 2013, 5:31pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925 "2013-11-13T17:31:41Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [November 13, 2013, 5:31pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/1 "2013-11-13T17:31:41Z")

</div>

> ℹ This topic has been superseded by [Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/developing-discourse-plugins-part-1-create-a-basic-plugin/30515)

So, you want to manipulate the text in the composer but have no idea how? Let met guide you through the creation of a basic plugin that will turn the text into [Pirate Speak](http://en.wikipedia.org/wiki/International_Talk_Like_a_Pirate_Day).

> 🎉 **Update August 2018** : you can create a complete discourse plugin skeleton in your plugins directory with [Rails plugin generator](https://meta.discourse.org/t/rails-plugin-generator/95907) 🎉

First of all, you need to create a `plugin.rb` file that will be your entry point.

```ruby
# name: pirate-speak
# about: plugin that transform the text into pirate speak
# version: 0.1
# authors: Régis Hanol

register_asset "javascripts/pirate_speak.js", :server_side

```

I highly recommend that you add at least the following pieces of meta data:

- **name** : the name of your plugin
- **about** : a one line description of your plugin
- **version** : the version of your plugin

Then you can add the code of your plugin. Here, we’re only registering a `javascript` asset that will also be used on the server side. Why? Because we use the same javascript code for baking and cooking (ie. post processing) the posts on the client-side and on the server-side. If you want your text-manipulation plugin to work properly, you have to indicate that this file must be used when post-processing the posts on the server, hence the `:server_side` argument.

Let’s have a look at the file we’ve just registered:

```javascript
function piratize (text) {
  return text.replace(/\b(am|are|is)\b/ig, "be")
             .replace(/ing\b/ig, "in'")
             .replace(/v/ig, "'");
}

Discourse.Dialect.postProcessText(function (text) {
  text = [].concat(text);
  for (var i = 0; i < text.length; i++) {
    if (text[i].length > 0 && text[i][0] !== "<") {
      text[i] = piratize(text[i]);
    }
  }
  return text;
});

```

First, there’s our core function: `piratize`. It takes a string as argument and uses [regular expressions](http://en.wikipedia.org/wiki/Regular_expression) to transform the text into a basic pirate speak. You are more than welcome to improve that function and submit pull requests 😉

But the most important part is the call to the `Discourse.Dialect.postProcessText` function. This tells Discourse to call our [anonymous function](http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work) whenever the text in the composer/post needs to be post-processed. The `text` argument might be a string or an array of string. That’s why we use `text = [].concat(text)` at the beginning to convert that string into an array if needs be. Then we go through the array and call our `piratize` function for each element in the array except when the element is an html tag (ie. it starts with a `<`). Finally, we return the text array so that it can be used in the rest of the post-processing pipeline.

![](https://global.discourse-cdn.com/meta/original/2X/a/af21e5dfd814ad6d22b614ef4576a8f609f5acfa.png)

The finished [Pirate Speak plugin source code](https://github.com/discourse/discourse-pirate-speak) is available on GitHub if you want to have a look.

Another great example is @sam’s [ALL CAPS plugin](http://meta.discourse.org/t/brand-new-plugin-interface/8793/2) which converts the text to UPPERCASE.

Also, if you want **advanced** text manipulation plugins, you might want to check out

- [MathJax plugin](http://meta.discourse.org/t/brand-new-plugin-interface/8793/43) which adds support for math notations using [MathJax](http://www.mathjax.org/) library.
- [Spoiler Alert plugin](https://github.com/discourse/discourse-spoiler-alert) which allows anyone to prevent spoilers.

---

<div class="post-metadata">

### Author: ![ypxu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ypxu/32/2098_2.png) [@ypxu](https://meta.discourse.org/u/ypxu)
#### Post date: [November 13, 2013, 7:40pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/2 "2013-11-13T19:40:30Z")

</div>

Good tutorial!

I am also looking for ways to add hook into the backend controller (ruby-side). It would be great to have a tutorial as well.

---

<div class="post-metadata">

### Author: ![goetz](https://avatars.discourse-cdn.com/v4/letter/g/8baadc/32.png) [@goetz](https://meta.discourse.org/u/goetz)
#### Post date: [November 25, 2013, 10:20pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/3 "2013-11-25T22:20:06Z")

</div>

Thanks for the tutorial! This is really useful!

---

<div class="post-metadata">

### Author: ![PabloC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pabloc/32/49633_2.png) [@PabloC](https://meta.discourse.org/u/PabloC)
#### Post date: [December 6, 2013, 7:12pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/4 "2013-12-06T19:12:16Z")

</div>

I want to override the youtube one box. Is it possible to do it from with a plugin? I inspected a littlebit the libs and I’m not sure if I can do that with a plugin.

Just want to update the width of the videos, to has a wider view, more similar to youtube. I was wondering if that can be done with a plugin.

Tks!

---

<div class="post-metadata">

### Author: ![NomNuggetNom](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nomnuggetnom/32/122685_2.png) [@NomNuggetNom](https://meta.discourse.org/u/NomNuggetNom)
#### Post date: [June 17, 2014, 3:10pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/5 "2014-06-17T15:10:42Z")

</div>

How can you not run adjustments in code blocks or other raw text, without resorting to a `"<"` check?

---

<div class="post-metadata">

### Author: ![cfstras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cfstras/32/103104_2.png) [@cfstras](https://meta.discourse.org/u/cfstras)
#### Post date: [June 30, 2014, 3:54pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/6 "2014-06-30T15:54:05Z")

</div>

You could look at [this piece of code](https://gist.github.com/cfstras/8824566#file-plugin-rb-L68-L84) to override the whole plugin – extend my plugin, I’d be happy about pull reqs 😄

---

<div class="post-metadata">

### Author: ![tuananh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tuananh/32/33428_2.png) [@tuananh](https://meta.discourse.org/u/tuananh)
#### Post date: [July 8, 2014, 5:11pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/7 "2014-07-08T17:11:31Z")

</div>

i added this to `app.yml` to test and it doesn’t seem to work.

any idea how to check if the plugin is properly loaded?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 5, 2016, 7:30pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/9 "2016-08-05T19:30:06Z")

</div>

This tutorial doesn’t work with the new plugin scheme, right?

My trivial [doi resolver](https://github.com/pfaffman/discourse-doi-resolver) quit working a couple upgrades ago & I’m assuming that’s why.

I’m working on re-writing my plugin based on the current Spoiler Alert. Once I do that, should I submit a PR for Pirate Speak? It seems that having a trivial plugin example is pretty valuable for novices like me.

Similarly should this [brand new plugin interface](https://meta.discourse.org/t/brand-new-plugin-interface/8793/110) post be archived?

---

<div class="post-metadata">

### Author: ![Silvanus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/silvanus/32/62831_2.png) [@Silvanus](https://meta.discourse.org/u/Silvanus)
#### Post date: [October 1, 2016, 8:21pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/10 "2016-10-01T20:21:34Z")

</div>

I’m very new to git and plugin stuff, but I made a pull request according to @cpradio’s [comment](https://meta.discourse.org/t/typeerror-discourse-dialect-postprocesstext-is-not-a-function/49963/2).

[https://github.com/discourse/discourse-pirate-speak/pull/2](https://github.com/discourse/discourse-pirate-speak/pull/2)

I’ve also been trying to enable a [similar fix](https://github.com/jsilvanus/discourse-doi-resolver/commit/826705864baea31d359ab0673952b94809d85ce1) for the doi resolver, but haven’t been able to make it work in my dev setup…

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [September 22, 2022, 8:51pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/15 "2022-09-22T20:51:22Z")

</div>



---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [September 22, 2022, 8:51pm UTC](https://meta.discourse.org/t/superseded-plugin-tutorial-1-how-to-manipulate-the-text-in-the-composer/10925/16 "2022-09-22T20:51:24Z")

</div>


