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!

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

2 Likes