Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatter): decode tweet text HTML entities #133

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/node": "^20.9.0",
"cli-spinners": "^2.9.1",
"dotenv": "^16.3.1",
"html-entities": "^2.4.0",
"html-escaper": "^3.0.3",
"masto": "^6.4.2",
"mime": "^3.0.0",
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/tweet/__tests__/format-tweet-text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { Tweet } from "@the-convocation/twitter-scraper";
import { formatTweetText } from "../format-tweet-text.js";

describe("formatTweetText", () => {
it("should decode html entities from the text", () => {
const tweet: Tweet = {
text: "one elephant & one mammoth make two animals, right?< > & " ' © ® ™ ¢ £ € ¥ § ° ± × ÷",
urls: [],
} as unknown as Tweet;
const result = formatTweetText(tweet);
expect(result).toStrictEqual(
"one elephant & one mammoth make two animals, right?< > & \" ' © ® ™ ¢ £ € ¥ § ° ± × ÷",
);
});

describe("when the tweet has links", () => {
it("should remove the t.co links from the text", () => {
const tweet: Tweet = {
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/tweet/format-tweet-text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Tweet } from "@the-convocation/twitter-scraper";
import { decode } from "html-entities";

export const formatTweetText = (tweet: Tweet): string => {
let text = tweet.text ?? "";
Expand All @@ -11,6 +12,9 @@ export const formatTweetText = (tweet: Tweet): string => {
// Remove medias t.co links
text = text.replaceAll(/https:\/\/t\.co\/\w+/g, "");

// Replace HTML entities with their unicode equivalent
text = decode(text);

// Return formatted
return text.trim();
};