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
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.
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?
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.