Modifying Discourse Docs to only allow for sorting by Title

Discourse Docs is the primary reason that I chose to use Discourse vs other forum software. However, it has one major shortcoming for me - by default it sorts by activity. I’d like to remove the ability to sort by activity altogether, but can’t figure out how to do so.

I’ve found this section of code, and removed the elsif statement for activity sorting and it doesn’t help.

      if @filters[:order] == "title"
        if @filters[:ascending].present?
          results = results.reorder('topics.title')
        else
          results = results.reorder('topics.title DESC')
        end
      elsif @filters[:order] == "activity"
        if @filters[:ascending].present?
          results = results.reorder('topics.last_posted_at')
        else
          results = results.reorder('topics.last_posted_at DESC')
        end
      end

I’ve also done the same in this file and it also still provides the same sorting abilities.

   context 'when ordering results' do
      context 'by title' do
        it 'should return the list ordered descending' do
          get "/docs.json?order=title"

          expect(response.status).to eq(200)

          json = response.parsed_body
          topics = json['topics']['topic_list']['topics']

          expect(topics[0]['id']).to eq(topic2.id)
          expect(topics[1]['id']).to eq(topic.id)
        end

        it 'should return the list ordered ascending with an additional parameter' do
          get "/docs.json?order=title&ascending=true"

          expect(response.status).to eq(200)

          json = response.parsed_body
          topics = json['topics']['topic_list']['topics']

          expect(topics[0]['id']).to eq(topic.id)
          expect(topics[1]['id']).to eq(topic2.id)
        end
      end

      context 'by date' do
        before do
          topic2.update(last_posted_at: Time.zone.now + 100)
        end

        it 'should return the list ordered descending' do
          get "/docs.json?order=activity"

          expect(response.status).to eq(200)

          json = response.parsed_body
          topics = json['topics']['topic_list']['topics']

          expect(topics[0]['id']).to eq(topic.id)
          expect(topics[1]['id']).to eq(topic2.id)
        end

        it 'should return the list ordered ascending with an additional parameter' do
          get "/docs.json?order=activity&ascending=true"

          expect(response.status).to eq(200)

          json = response.parsed_body
          topics = json['topics']['topic_list']['topics']

          expect(topics[0]['id']).to eq(topic2.id)
          expect(topics[1]['id']).to eq(topic.id)
        end
      end
    end

I did the edits both directly in the file on the server, as well as in a fork of Discourse Docs which i then installed.

Does anyone have any tips? I really thought it would be a 3 minute job but have spent many hours on this now…

Thanks!

1 Like

Nevermind, i figured it out. For some reason there was a filter on in my vs code that was hiding javascript files. So when I searched for things like Order, Sort, Activity etc…, I wasn’t getting results for the js files (which obviously handle an interactive function like sorting…).

I forked the repo and made changes. Anyone is welcome to use it. It removes the ability to sort by activity, defaults the topic list to sort by title ascending, and defaults the category filters to sort by alpha ascending.

I dont know how to add settings for user customizability, so this will have to suffice until this functionality is brought into the official plugin

4 Likes

Would be great so this merged into official version, how about a PR? :slight_smile: That way the order would be adjusted accordingly in the Discourse Docs Card Filter Component, correct?

1 Like

I really don’t know how to do that. Feel free to do so! Some sort of admin setting to select between alphabetical and activity and/or count would be ideal.

1 Like

Thanks for this, does anyone know how to set this up correctly, I’m still a beginners configuring plugins.

As far as I understand this addition (alphabetical sort) was added by @Nick_Chomey as a Fork of the original plugin.

What would be the best way for me to use this.

Can I simply modify some code in the original Docs plugin? or would I need to uninstall the original plugin and re-install this fork?

Thanks!

1 Like

Yes, uninstall Discourse Docs and install this one as you would for any plugin. I don’t monitor/maintain it regularly for updates to docs, but would be happy to do so whenever anyone prompts me. Or you could fork it and monitor changes yourself.

Of course, the best option is if Discourse just includes this as a setting/feature within the official plugin.

1 Like

Hey there, thanks for making changes to Docs (if even locally)!

I’ve moved this to feature and tagged it docs so we can better track this going forward.

3 Likes

Great, thanks!

As you will see, I just added some alphabetical sorting functionality and commented-out the activity sorting. So its not a solution for everyone. But I’m sure you can easily integrate my addition and add a setting to allow us to choose/toggle which types of sorting are available - I had no idea how to do so.

3 Likes