# Editing posts using a Theme Component

**URL:** https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671
**Category:** Development
**Created:** [September 5, 2025, 3:18am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671 "2025-09-05T03:18:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [September 5, 2025, 3:18am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671/1 "2025-09-05T03:18:28Z")

</div>

So I stumbled on the idea of allowing those with editing post permissions (TL4+ users) to be able to select text and convert that part to a codeblock.

After referencing some parts of the Discourse AI plugin code and looking at what data is passed into the plugin outlet I’m using, I came up with the code to get the text selected, and wrap code fencss around it, using the `quote-share-buttons-before` outlet.

Then now, I’m kind of stuck. The checklist plugin (somehow) does this with an argument in the `checklistSyntax` function called `postDecorator`, which they can then get the model (I think it’s the `post` model?) using `.getModel()`. How that is passed in I don’t know, but I don’t think that is available for me in my setup?

If it’s truly the post model being used in the checklist plugin, I seem to have trouble finding where the simple ‘save’ function used in

```js
await postModel.save({
  raw: newRaw,
  edit_reason: i18n("checklist.edit_reason"),
});

```

exists in `models/post.js`, unless I’m understanding it wrong.

I’m currently using `this.args.outletArgs.data.editPost(this.post)` (which IIRC is in `models/topic.js`, but please correct me if I’m wrong) which only opens up the composer to edit the post, not just edit it directly.

So my question is this - how can I edit the post like the checklist plugin, fuss-free? Is there a way to do that if I’m putting a button in an outlet instead of `api.decorateCookedElement` like what checklist does? Preferably without using the REST API?

My repo:  
[https://github.com/NateDhaliwal/discourse-format-text-in-codeblocks](https://github.com/NateDhaliwal/discourse-format-text-in-codeblocks)

Thanks!

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [September 5, 2025, 9:41am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671/2 "2025-09-05T09:41:23Z")

</div>

Okay, I think I’m getting ~~closer~~ very close now.

~~I tried `this.post.save()` and it apparently “works” - it seems like the PUT request is going through (using ajax under the hood), but was met with some error with little to no information. But that was before this…~~

The `this.post.save()` works. The only problem is the one listed below:

My function is now like this:

````js
@action
async addCodeFences() {
  let selectedText = this.selectedText;
  let newText = "```" + "\n" + selectedText + "\n" + "```";

  let post = this.post;
  console.log(this.post.cooked); // Works

  let rawPost = post.raw; // Doesn't exist!!
  console.log(rawPost); // undefined!

  rawPost.replace(selectedText, "\n" + newText + "\n"); // Error, since rawPost is undefined!

  await this.post.save({
    raw: rawPost,
    edit_reason: I18n.t(themePrefix("add_code_fence_edit_reason"))
  });
}

````

For some reason, I cannot get the post’s raw contents, only the cooked. Besides doing an ajax request, is there a way to do it? I can’t seem to find `raw` as one of the attributes for the post… is there a reason why it isn’t included?

P.S. I’m trying not to turn to Ask Discourse here. I would really appreciate any help!

* * *

EDIT: I think each post is based of [this API request](https://docs.discourse.org/#tag/Topics/operation/getSpecificPostsFromTopic)? And the response doesn’t seem to have `raw` as one of the JSON keys 😮‍💨.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [September 5, 2025, 10:04am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671/3 "2025-09-05T10:04:59Z")

</div>

> [@NateDhaliwal](#):
>
> I think each post is based of [this API request](https://docs.discourse.org/#tag/Topics/operation/getSpecificPostsFromTopic)? And the response doesn’t seem to have `raw` as one of the JSON keys 😮‍💨.

Yeah that’s right - we don’t include `raw` in the default response. It’s not needed for normal rendering, so including it would be kinda wasteful in terms of data-transfer/speed.

The best bet would be to make an extra ajax request when you need it. As you found, the [checklist plugin is a good example of this](https://github.com/discourse/discourse/blob/6c8c1bb7b616d8861ce0d3a2321c1223778dac40/plugins/checklist/assets/javascripts/discourse/initializers/checklist.js#L96).

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [September 5, 2025, 10:05am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671/4 "2025-09-05T10:05:32Z")

</div>

Got it, thanks! I’ll work on that ajax request.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [October 5, 2025, 10:06am UTC](https://meta.discourse.org/t/editing-posts-using-a-theme-component/381671/5 "2025-10-05T10:06:21Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
