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

fix: tweet embed #221

Merged
merged 2 commits into from
Sep 9, 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
4 changes: 2 additions & 2 deletions src/components/Appbar/HomeAppbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from "@react-navigation/native";
import { Fragment, FunctionComponent, memo, useState } from "react";
import { Appbar, Avatar, Menu, Tooltip, useTheme } from "react-native-paper";
import { Appbar, Avatar, Menu, Tooltip } from "react-native-paper";
import { StackParamList } from "../../router/types";

type props = {
Expand Down Expand Up @@ -45,7 +45,7 @@ const HomeAppbar: FunctionComponent<props> = ({
/>
)}
/>
<Appbar.Content title="" />
<Appbar.Content title="UDev" />
{!isVideoListScreen ? (
<Fragment>
<Tooltip title="Search posts">
Expand Down
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}`;
};
Loading