JSON link array does not match link order in post

AI 生成的摘要

The discussion revolves around an issue where the order of links in the JSON API response for a Discourse post does not match the order in which they appear in the post content. samnazarko reports that despite links being listed in a specific order in the post (e.g., Apple TV as the second to last link), the JSON output lists Apple TV first, suggesting a reordering. eviltrout explains that links are ordered by popularity (click count), and vinothkannans confirms this behavior via a code reference. samnazarko acknowledges the issue and considers workarounds, such as parsing the cooked HTML or proposing a new JSON field (e.g., links_raw) to preserve the original link order, and offers to contribute a pull request if the feature is accepted.

Hello

We are using Discourse to power our Wiki. It’s been working pretty well. Since updating to 1.7 we’ve run in to a slight snag. It seems that the ordering of the links to other Wiki pages is not necessarily respected.

If you see our Table of Contents for the Wiki here, you will see that Apple TV is the second to last link, and we would like it to be the second to last category to appear. Our script scrapes the links array. I have noticed that the JSON of the post lists Apple TV first. At first I thought Discourse may be ordering links by post ID, but this does not seem to be the case. I tried editing the post and removing all links, and then re-adding them, but this did not change the situation.

Is there an explanation for how the links array is ordered? Would it make sense for the links to be listed in the JSON in the order that they are referenced?

  "links":[  
     {  
        "url":"https://discourse.osmc.tv/t/apple-tv/10039",
        "title":"Apple TV",
        "fancy_title":null,
        "internal":true,
        "attachment":false,
        "reflection":false,
        "clicks":0,
        "user_id":1,
        "domain":"discourse.osmc.tv"
     },

]

Thanks

Sam

I believe those links are ordered in terms of popularity. The most clicked on links will be at the top.

Yes.

Thanks for the information. I’ll have to find an appropriate workaround.

Sam

You have link_counts, so you could add correct ordering there, but preserve links as is in a future version of Discourse, or maybe add a ‘links_raw’. If you would accept a new JSON property that offers links in the correct order then I can PR this.

I will have to work around this temporarily by taking cooked and taking links from that or patching Discourse.

Sam