@ambassify/twitter-text

中文文档
4.0.0发布于 2023/09/19ESM + CJS近 30 天下载 5332

Ambassify's for of the official twitter text linkification library

原始使用文档
文档目录18 节
  1. Changes we made
  2. NPM Users
  3. Extraction Examples
  4. Auto-linking Examples
  5. Tweet Parsing
  6. Marked for Deprecation
  7. Usernames
  8. Lists
  9. Hashtags
  10. URLs
  11. International
  12. Hit Highlighting
  13. Testing
  14. Conformance
  15. Other Tests
  16. Packaging
  17. Reporting Bugs
  18. Copyright and License

@ambassify/twitter-text

This is Ambassify's fork of the twitter-text library.

We extracted the JS portion of the library as we don't need other languages and their dependencies trigger dependabot alerts which are required to be fixed on Ambassify repositories. The effort required to fix them for those languages we don't use is too high.

Our hope is that if there are ever any changes to the upstream package, they'll also fix their security issues and outdated builds.

It's available on NPM as npm install @ambassify/twitter-text

Changes we made

Orginal README

NPM Users

Install it with: npm install twitter-text

The twttr.txt namespace is exported, making it available as such:

var twitter = require('twitter-text')
twitter.autoLink(twitter.htmlEscape('#hello < @world >'))

Extraction Examples

// basic extraction
var usernames = twttr.txt.extractMentions("Mentioning @twitter and @jack")
// usernames == ["twitter", "jack"]

Auto-linking Examples

twttr.txt.autoLink("link @user, please #request");

twttr.txt.autoLink("link @user, and expand url... http://t.co/0JG5Mcq", {
    urlEntities: [
        {
          "url": "http://t.co/0JG5Mcq",
          "display_url": "blog.twitter.com/2011/05/twitte…",
          "expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",
          "indices": [
            30,
            48
          ]
        }
    ]});

See Tweet Entities for more info getting url entities from Twitter's API.

Tweet Parsing

Previous versions of Twitter-Text provided different helper methods for Tweet validation, Tweet length, and remaining characters calculation. To simplify the API and obtain this information with just one call, Twitter-Text now exposes a new “parseTweet” method that will return the following fields:

  • weightedLength: Integer that indicates the weighted length calculated by the algorithm above.
  • permillage: Integer value corresponding to the ratio of consumed weighted length to the maximum weighted length.
  • valid: Boolean indicating whether it is a valid Tweet.
  • dispayRangeStart: Integer with start index on the Tweet string
  • displayRangeEnd: Integer with end index on the Tweet string (inclusive)
  • validDisplayRangeStart: Integer indicating the valid start index on the Tweet string
  • validDisplayRangeEnd: Integer indicating the valid end index on the Tweet string. This can be lesser than displayRangeEnd (inclusive).
var tweet = "This is a test Tweet";
twttr.txt.parseTweet(tweet);
/* Returns:
  {
    weightedLength: 20,
    permillage: 71,
    valid: true,
    displayRangeEnd: 19,
    displayRangeStart: 0,
    validRangeEnd: 19,
    validRangeStart: 0
  }
*/

Details about Twitter's weighted counting scheme are available on the official developer website.

Marked for Deprecation

getTweetLength returns the weighted length of a Tweet that is calculated by parseTweet. It will be removed in a subsequent release. Please use parseTweet instead.

Usernames

Username extraction and linking matches all valid Twitter usernames but does not verify that the username is a valid Twitter account.

Lists

Auto-link and extract list names when they are written in @user/list-name format.

Hashtags

Auto-link and extract hashtags, where a hashtag contains any latin letter or number but cannot be solely numbers.

URLs

Asian languages like Chinese, Japanese or Korean may not use a delimiter such as a space to separate normal text from URLs making it difficult to identify where the URL ends and the text starts.

For this reason Twitter-Text currently does not support extracting or auto-linking of URLs immediately followed by non-Latin characters.

Example: "http://twitter.com/は素晴らしい" . The normal text is "は素晴らしい" and is not part of the URL even though it isn't space separated.

International

Special care has been taken to be sure that auto-linking and extraction work in Tweets of all languages. This means that languages without spaces between words should work equally well.

Hit Highlighting

Use to provide emphasis around the "hits" returned from the Search API, built to work against text that has been auto-linked already.

Testing

For all Twitter-Text tests, run:

npm run test

This will run conformance and javascript tests.

Conformance

The main test suite is twitter-text-conformance. Tests are run in your browser, using QUnit. To run the conformance suite, from the project root, run:

rake test:conformance

Your default browser will open the test suite. You should open the test suite in your other browsers as you see fit.

Other Tests

There are a few tests specific to twitter-text-js that are not part of the conformance suite. To run these, from the project root, run:

rake test

Your default browser will open the test suite.

Packaging

Official versions are kept in the pkg/ directory. To roll a new version, (ex. v1.1.0), run the following from project root:

rake package[1.1.0]

This will make a new file at pkg/twitter-text-1.1.0.js.

Reporting Bugs

Please direct bug reports to the twitter-text issue tracker on GitHub

Copyright 2012-2020 Twitter, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

版本与发布记录

@ambassify/twitter-text 共发布 2 个版本,首次发布于 2021/11/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2023/09/193 年前
3.1.02021/11/045 年前

关于 @ambassify/twitter-text 的常见问题

@ambassify/twitter-text 是什么?

Ambassify's for of the official twitter text linkification library

如何安装 @ambassify/twitter-text?

在项目目录运行 npm install @ambassify/twitter-text,或使用 pnpm add / yarn add / bun add。

@ambassify/twitter-text 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ambassify__twitter-text 是否存在。

@ambassify/twitter-text 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ambassify/twitter-text 可以商用吗?

@ambassify/twitter-text 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@ambassify/twitter-text 的下载量有多少?

根据 npm 官方下载统计,@ambassify/twitter-text 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,332 次,日均约 178 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ambassify/twitter-text 还在维护吗?

最新版本 4.0.0 发布于 2023/09/19(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。