discourse/app
锦心 5b05cdfbd9
FIX: Add post id to the anchor to prevent two identical anchors (#28070)
* FIX: Add post id to the anchor to prevent two identical anchors

We generate anchors for headings in posts. This works fine if there is
only one post in a topic with anchors. The problem comes when you have
two or more posts with the same heading. PrettyText generates anchors
based on the heading text using the raw context of each post, so it is
entirely possible to generate the same anchor for two posts in the same
topic, especially for topics with template replies

    Post1:
    # heading
    context
    Post2:
    # heading
    context

When both posts are on the page at the same time, the anchor will only
work for the first post, according to the [HTML specification](https://html.spec.whatwg.org/multipage/browsing-the-web.html#scroll-to-the-fragment-identifier).

> If there is an a element in the document tree whose root is document
> that has a name attribute whose value is equal to fragment, then
> return the *first* such element in tree order.

This bug is particularly serious in forums with non-Latin languages,
such as Chinese. We do not generate slugs for Chinese, which results in
the heading anchors being completely dependent on their order.

```ruby
[2] pry(main)> PrettyText.cook("# 中文")
=> "<h1><a name=\"h-1\" class=\"anchor\" href=\"#h-1\"></a>中文</h1>"
```

Therefore, the anchors in the two posts must be in exactly the same by
order, causing almost all of the anchors in the second post to be
invalid.

This commit solves this problem by adding the `post_id` to the anchor.
The new anchor generation method will add `p-{post_id}` as a prefix when
post_id is available:

```ruby
[3] pry(main)> PrettyText.cook("# 中文", post_id: 1234)
=> "<h1><a name=\"p-1234-h-1\" class=\"anchor\" href=\"#p-1234-h-1\"></a>中文</h1>"
```

This way we can ensure that each anchor name only appears once on the
same topic. Using post id also prevents the potential possibility of the
same anchor name when splitting/merging topics.
2024-07-25 13:50:30 +08:00
..
assets FIX: Add post id to the anchor to prevent two identical anchors (#28070) 2024-07-25 13:50:30 +08:00
controllers FIX: store information about the login method in the database. (#28054) 2024-07-24 17:19:58 +10:00
helpers UX: Use localized time format in embedded comments (#28014) 2024-07-22 18:42:36 +08:00
jobs PERF: Avoid using ObjectSpace.each_object in Jobs::Onceoff.enqueue_all (#28072) 2024-07-25 13:30:56 +08:00
mailers UX: Use a dropdown for SSL mode for group SMTP (#27932) 2024-07-18 10:33:14 +10:00
models FIX: Add post id to the anchor to prevent two identical anchors (#28070) 2024-07-25 13:50:30 +08:00
serializers DEV: show admin moderation flags UI (#28071) 2024-07-25 15:24:17 +10:00
services FEATURE: Clean up previously logged information after permanently deleting posts (#28033) 2024-07-23 15:27:11 +08:00
views FIX: Exclude reply count on posts due to required Comment nesting (#27892) 2024-07-15 09:40:47 +08:00