Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azu/tweet-truncator
Truncate contents to 140 chars for Twitter/Tweets
https://github.com/azu/tweet-truncator
javascript tweet twitter
Last synced: 14 days ago
JSON representation
Truncate contents to 140 chars for Twitter/Tweets
- Host: GitHub
- URL: https://github.com/azu/tweet-truncator
- Owner: azu
- License: mit
- Created: 2015-12-24T11:02:46.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-08-22T07:13:27.000Z (about 1 year ago)
- Last Synced: 2024-10-09T17:01:09.696Z (28 days ago)
- Topics: javascript, tweet, twitter
- Language: TypeScript
- Homepage:
- Size: 99.6 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tweet-truncator [![Actions Status: test](https://github.com/azu/tweet-truncator/workflows/test/badge.svg)](https://github.com/azu/tweet-truncator/actions?query=workflow%3A"test")
Truncate contents to 280 chars for tweeting.
- [twitter-text-js](https://github.com/twitter/twitter-text/tree/master/js)
## Installation
npm install tweet-truncator
## Usage
### truncate(content, [options])
```js
import { truncate } from "tweet-truncator";
const contents = {
title: "tweet-truncator",
url: "https://github.com/azu/tweet-truncator",
desc: "Truncate contents to 140 chars.",
quote: "quote",
tags: ["#twitter", "JavaScript"]
};
const options = {
defaultPrefix: "See:",
template: '%desc% "%title%" %url% %tags%',
// maxLength: 280
};
const result = truncate(contents, options);
console.log(result);
/*
Truncate contents to 140 chars. "tweet-truncator" https://github.com/azu/tweet-truncator #twitter #JavaScript
*/
```See [example/](example/)
#### [TweetTruncator](src/tweet-truncator.js)
TweetTruncator is a core class of tweet-truncator.
```js
const truncator = new TweetTruncator({
defaultPrefix: "See:",
template: `%desc% "%title%" %url% %tags%`
});
const contents = {
title: "TITLE",
url: "https://github.com/twitter/twitter-text",
desc: "",
quote: "quote",
tags: []
};
const overLength = 10;// it means that remove 10 chars from contents.
var result = truncator.truncateStatus(contents, overLength);
```## Tests
npm test
## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D## License
MIT
Thanks to [Tombfix](https://github.com/tombfix/core "Tombfix").