178. Twitter mentions

JavaScript

中等难度  -通过 / -执行

On Twitter(now X), we always see mentions, hashtags and links in blue links, like this one below.

One flexible way to implement this is to store the format info separately besides the user input in plain text, and connect them through the indices.

In above example, we can use following data structure.

{    text: 'Shaku syntax is now supported on https://t.co/UpkmJ7yGKG! Give it a try to annotate your code ! kudos to @JSer_ZANP for making such a great tool !',    entities: {        "urls": [            {                "displayUrl": "BFE.dev", // url to display                "url": "https://t.co/UpkmJ7yGKG", // actual url to redirect                "indices": [                     33, // start index, inclusive                    56 // end index, exclusive                ]            }        ],        "mentions": [            {                "screeName": "JSer_ZANP", // display screen name                "indices": [                     105, // start index, inclusive                    115 // end index, exclusive                ]            }        ]    }}

You are now required to create a function to render above data structure into html string. The fields should explain by itself.

  1. There is no emoji in the text. You don't need to worry about the index offset.
  2. For mention, you should redirect to https://x.com/{screeName}
  3. There are no overlaps within the entities.

始终思考更好的解决办法