Issue with meta description links in Discourse page source

Today, I have been trying to fix the following issue with the Discourse source code:

As you can see Discourse is not stripping newlines and extra white space in their meta description tags.

In addition, there is also the oddly injected HTML entities, in this case: "…"

I tried adding this code to one of my plugins for Discourse, but it did not work as expected, and in fact, this code crashed the topics view:

TopicsController.class_eval do
    after_action :strip_newlines_and_more_from_meta_description

    private

    def strip_newlines_and_more_from_meta_description
      @description_meta = @description_meta&.gsub("…", "")
      @description_meta = @description_meta&.gsub(/\n+/, " ")
      @description_meta = @description_meta&.gsub(/\s+/, " ")&.strip
    end
  end

Not sure why. The code works fine in the rails console when not used as an action controller filter.

I tried both:

TopicsController.class_eval do

and

TopicsController.instance_eval do

No joy yet.

Anyway, I'm going to take a break since this is not working as expected; but I might keep working on this when the site is quiet with few members logged in :slight_smile:

If anyone has Discourse running in a development environment, if you can get this code working so we can fix our meta data, that would be helpful.

Thanks.

Hmm.

I added some debug to log statements and the filter works:

>>>>NEONEO_BEGIN>>>>> @description_meta: Today, I have been trying to fix the following issue with the Discourse source code: 
 
As you can see Discourse is not stripping newlines and  extra white space in their meta description tags. 
In addition, there is als…  uri: /t/issue-with-meta-description-links-in-discourse-page-source/381000
>>>>NEONEO_END>>>>> @description_meta: Today, I have been trying to fix the following issue with the Discourse source code: As you can see Discourse is not stripping newlines and extra white space in their meta description tags. In addition, there is als…  uri: /t/issue-with-meta-description-links-in-discourse-page-source/381000

But for some reason, the Rails filter after_action appears to not work as expected.

My expectation was that before_action would filter before the page is rendered; but maybe not?

Will try moving the logic "up the chain", as follows and debug:

TopicsController.class_eval do
    before_action :strip_newlines_and_more_from_meta_description, on: [:perform_show_response]

    private

    def strip_newlines_and_more_from_meta_description
      Discourse.warn(">>>>NEONEO_BEGIN>>>>> @topic_view.topic.excerpt: #{@topic_view.topic.excerpt}", uri: request.env["REQUEST_URI"])
      tmp_description = @topic_view.topic.excerpt&.dup
      tmp_description = tmp_description&.gsub("…", "")
      tmp_description = tmp_description&.gsub("…", "")
      tmp_description = tmp_description&.gsub(/\n+/, " ")
      tmp_description = tmp_description&.gsub(/\s+/, " ")&.strip
      @topic_view.topic.excerpt = tmp_description
      Discourse.warn(">>>>NEONEO_END>>>>> @topic_view.topic.excerpt: #{@topic_view.topic.excerpt}", uri: request.env["REQUEST_URI"])
    end
  end

Hmm. None of those techniques worked as expected, so I am going to try this:


  # overwrite TopicsController private method to clean up @description_meta text
  class TopicsController
    def perform_show_response
      if request.head?
        head :ok
        return
      end

      topic_view_serializer = TopicViewSerializer.new(
        @topic_view,
        scope: guardian,
        root: false,
        include_raw: !!params[:include_raw],
      )

      respond_to do |format|
        format.html do
          @tags = SiteSetting.tagging_enabled ? @topic_view.topic.tags : []
          @breadcrumbs = helpers.categories_breadcrumb(@topic_view.topic) || []
          @description_meta = @topic_view.topic.excerpt.present? ? @topic_view.topic.excerpt : @topic_view.summary
          @description_meta = @description_meta&.gsub("…", "")
          @description_meta = @description_meta&.gsub("…", "")
          @description_meta = @description_meta&.gsub(/\n+/, " ")
          @description_meta = @description_meta&.gsub(/\s+/, " ")&.strip
          store_preloaded("topic_#{@topic_view.topic.id}", MultiJson.dump(topic_view_serializer))
          render :show
        end

        format.json do
          render_json_dump(topic_view_serializer)
        end
      end
    end
  end
end

EDIT: Which also did not work as expected :confused:

Success!

This monkey patch worked as expected:

  # overwrite TopicsController private method to clean up @description_meta text
 TopicsController.class_eval do
  def perform_show_response
    if request.head?
      head :ok
      return
    end

    topic_view_serializer = TopicViewSerializer.new(
      @topic_view,
      scope: guardian,
      root: false,
      include_raw: !!params[:include_raw],
    )

    respond_to do |format|
      format.html do
        @tags = SiteSetting.tagging_enabled ? @topic_view.topic.tags : []
        @breadcrumbs = helpers.categories_breadcrumb(@topic_view.topic) || []
        @description_meta = @topic_view.topic.excerpt.present? ? @topic_view.topic.excerpt : @topic_view.summary
        @description_meta = @description_meta&.gsub("…", " ")
        @description_meta = @description_meta&.gsub("…", " ")
        @description_meta = @description_meta&.gsub(/\n+/, " ")
        @description_meta = @description_meta&.gsub(/\s+/, " ")&.strip
        store_preloaded("topic_#{@topic_view.topic.id}", MultiJson.dump(topic_view_serializer))
        render :show
      end

      format.json do
        render_json_dump(topic_view_serializer)
      end
    end
  end

Took me the better part of three hours to work it out; but it's worked out and now, and the meta descriptions important for SEO are not mangled now:

Soon, I will release this new plugin which fixes this and other issues, which I call:

Plugin: discourse-selfish-seo-layout-changes

Anyway, popular SEO wisdom says the meta description is not of much SEO value and that Google does not use this meta tag; and if they did, they just omit the newlines. However, I prefer to get rid of the extra white space and newlines. So, this code is done for now; until I see other little issues that need to be addressed in the Discourse source code.

1 Like