How to override the buildQuote function?

AI-generated summary

The discussion revolves around overriding the buildQuote function in Discourse to add a newline before the [quote] tag. Canapin shares a working solution using a theme component with api.modifyClass to modify the composer controller, successfully adding the desired newline. asc asks if the solution still works, and Canapin confirms it does. asc later reports issues with modifyClass not firing, attributing the fix to ensuring the composerController is properly referenced via api.container.lookup. merefield links to related meta discussions about modifyClass inconsistencies, highlighting the challenges with this API.

Hi!

I’d like to try to modify the buildQuote function so it adds a new line before the opening [quote] tag.

Basically, modifying this:

  return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

to this:

  return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

Is it possible to do such a thing with a theme component? How do I override this function? :smiley:

I’ve managed to achieve it, but is there a prettier solution?

<script type="text/discourse-plugin" version="0.8.23">
    function buildQuote(post, contents, opts = {}) {
      if (!post || !contents) {
        return "";
      }
    
      const params = [
        opts.username || post.username,
        `post:${opts.post || post.post_number}`,
        `topic:${opts.topic || post.topic_id}`
      ];
    
      if (opts.full) params.push("full:true");
    
      return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
    }
    api.modifyClass('controller:composer', {
        actions: {
            importQuote(toolbarEvent) {
              const postStream = this.get("topic.postStream");
              let postId = this.get("model.post.id");
        
              // If there is no current post, use the first post id from the stream
              if (!postId && postStream) {
                postId = postStream.get("stream.firstObject");
              }
        
              // If we're editing a post, fetch the reply when importing a quote
              if (this.get("model.editingPost")) {
                const replyToPostNumber = this.get("model.post.reply_to_post_number");
                if (replyToPostNumber) {
                  const replyPost = postStream.posts.findBy(
                    "post_number",
                    replyToPostNumber
                  );
        
                  if (replyPost) {
                    postId = replyPost.id;
                  }
                }
              }
        
              if (postId) {
                this.set("model.loading", true);
        
                return this.store.find("post", postId).then(post => {
                  const quote = buildQuote(post, post.raw, {
                    full: true
                  });
        
                  toolbarEvent.addText(quote);
                  this.set("model.loading", false);
                });
              }
            }
        }
    });
</script>

Sorry for bumping an old topic, but is this still working for you/have you had to update it at all? I’ve been trying to get this to work and I can’t get it to fire at all, but I’ve been having a terrible time with anything at all to do with modifyClass.

I just tried it, and it is still working :+1:

I literally copy-pasted it in my theme, and it worked without any modification.

See api.modifyClass sometimes(!) not working - #12 by RGJ

I’m not sure if this has been resolved?

Also see related but different: Issues overriding getters in a controller (3.0.0)

Thanks @merefield and @Canapin . I’ve been trying to learn the Discourse plugin API fiddling around with what feels like they should be simple things like this but modifyClass is so inconsistent when it will/won’t work. I’ve got a little theme component (that one you were mentioned into @merefield ) where two modifyClass calls work but a third doesn’t, very frustrating trying to figure out if I’ve got something wrong or it’s just modifyClass.

Sorry for the OT on your topic and thanks for the quick reply @Canapin

Update:

No worry, I don’t know what OT means but we’re here to help!