Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
fix: add support for tweet embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
gmsgowtham committed Sep 9, 2023
1 parent 43ce020 commit 7ea6aca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/components/Markdown/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { type CustomToken, MarkedTokenizer } from "react-native-marked";
import { getStackoverflowEmbedURL, getYoutubeEmbedURL } from "../../utils/url";
import {
getStackoverflowEmbedURL,
getTweetEmbedURL,
getYoutubeEmbedURL,
} from "../../utils/url";

class MDTokenizer extends MarkedTokenizer<CustomToken> {
paragraph(this: MarkedTokenizer<CustomToken>, src: string) {
Expand Down Expand Up @@ -110,6 +114,21 @@ class MDTokenizer extends MarkedTokenizer<CustomToken> {
return token;
}

const twitterMatch = src.match(/^[*]?{% (tweet)[*]? (.*?)[*]?%}[*]?/);
if (twitterMatch && twitterMatch.length > 2) {
const url = getTweetEmbedURL(twitterMatch[2]);
const token: CustomToken = {
identifier: "embed",
type: "custom",
raw: twitterMatch[0],
tokens: [],
args: {
text: url,
},
};
return token;
}

return super.paragraph(src);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export const VIDEO_UI_HIDE_TIMEOUT = 5000; // 5 seconds
export const NETWORK_TIMEOUT_MS = 10000;
export const YOUTUBE_HOST = "https://www.youtube.com";
export const STACKOVERFLOW_HOST = "https://stackoverflow.com";
export const TWITTER_URL = "https://www.twitter.com";
10 changes: 9 additions & 1 deletion src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Url from "url-parse";
import { STACKOVERFLOW_HOST, YOUTUBE_HOST } from "./const";
import { STACKOVERFLOW_HOST, TWITTER_URL, YOUTUBE_HOST } from "./const";

export const buildURLParams = (
params: Record<string, string | number | boolean | undefined | null>,
Expand Down Expand Up @@ -32,3 +32,11 @@ export const getStackoverflowEmbedURL = (str: string) => {
}
return `${STACKOVERFLOW_HOST}/questions/${str}`;
};

export const getTweetEmbedURL = (id: string) => {
const parsed = new Url(id, {});
if (parsed.host) {
return parsed.toString();
}
return `${TWITTER_URL}/i/web/status/${id}`;
};

0 comments on commit 7ea6aca

Please sign in to comment.