Search code examples
ruby-on-railsrubyregexdiscourse

Regex to normalize topic links in Discourse forum


I am using Discourse forum software. As in its current state, Discourse presents links to topic in two ways, with and without a post number at the end.

Example:

forum.domain.com/t/some-topic/23
forum.domain.com/t/some-topic/23/5

The first one is what I want and the second one I want to not be displayed in the forum at all.

I've written a post about it on Discourse forum but didn't receive an answer what Regex to put in the permalink normalization input field in the admin section.

I was told that there is an option to do it using permalink normalization like so (It's an example shown in the admin under the Regex input text, I didn't write it):

permalink normalizations

Apply the following regex before matching permalinks,
for example: /(topic.)\?./\1 will strip query strings from topic routes.
Format is regex+string use \1 etc. to access captures

I don't know what Regex I should use in order to remove the numerical value of the post number from links. I need it only for topic links.

This is the routes.rb routing library and this is the permalink.rb library (I think that the permalink library should help get a better clue how to achieve this). I have no idea how to approach this, because it seems that I need some knowledge of the Discourse routing to make it work. For example, I don't understand why (topic.) is part of the regex, what does it mean, so their example doesn't help me to find a solution.

In the admin I have an input field in which I nee to put the normalization regex code.

I need help with the Regex. I need the regex to work with all topics.

Things I've tried that didn't work out:

/(\/\d+)\/\d+$/\1

/(t/[^/]+/\d+).*/\1

/(\/\d+)\/[0-9]+$/\1

/(\/\d+)\/[0-9]+/\1

/(\/\d+)\/\d+$/\1/

/(forum.domain.com(\/\w+)*\/\d+)\/\d+(?=\s|$)/\1

Note: The Permalink Normalization input field treats the character | as a separator to separate between several Regex expressions.


Solution

  • I think this may be the expression you are looking for to put inside de settings field:

    /(t\/.*\/\d+)(\/\d+)/\1
    

    You can see it working on Rubular.

    However, the code that generates the url is not using the normalization code, so the expression is being ignored.

    You could try normalizing the permalink there:

    def last_post_url
      url = "#{Discourse.base_uri}/t/#{slug}/#{id}/#{posts_count}"
      url = Permalink.normalize_url url
      url
    end