Skip to content

Commit cb124b7

Browse files
authored
Merge pull request #226 from Maddoxx88/patch-6
[add]: Twitter Username Validation
2 parents 008f2f8 + c8102c7 commit cb124b7

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/components/markdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const Markdown = props => {
101101
if (props.show) {
102102
return (
103103
<>
104-
{`<p align="left"> <img src="${link}" alt="${props.twitter}" /> </p>`}
104+
{`<p align="left"> <a href="${props.base}/${props.twitter}" target="blank"><img src="${link}" alt="${props.twitter}" /></a> </p>`}
105105
<br />
106106
<br />
107107
</>
@@ -253,6 +253,7 @@ const Markdown = props => {
253253
github={props.social.github}
254254
/>
255255
<TwitterBadge
256+
base="https://twitter.com"
256257
show={props.data.twitterBadge}
257258
twitter={props.social.twitter}
258259
/>

src/components/markdownPreview.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ const MarkdownPreview = props => {
244244
return (
245245
<div className="text-left my-2">
246246
{" "}
247-
<img className="h-4 sm:h-6" src={link} alt={props.twitter} />{" "}
247+
<a href="https://twitter.com/${props.twitter}" target="blank">
248+
<img className="h-4 sm:h-6" src={link} alt={props.twitter} />
249+
</a>{" "}
248250
</div>
249251
)
250252
}

src/pages/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import SEO from "../components/seo"
2727
import {
2828
isGitHubUsernameValid,
2929
isMediumUsernameValid,
30+
isTwitterUsernameValid
3031
} from "../utils/validation"
3132
import Layout from "../components/layout"
3233

@@ -239,7 +240,7 @@ const IndexPage = () => {
239240
generate()
240241
}
241242
} else if (data.twitterBadge) {
242-
if (social.twitter) {
243+
if (social.twitter && isTwitterUsernameValid(social.twitter)) {
243244
generate()
244245
}
245246
} else if (social.github) {
@@ -532,13 +533,20 @@ const IndexPage = () => {
532533
) : (
533534
""
534535
)}
535-
{(data.twitterBadge && !social.twitter) ? (
536+
{data.twitterBadge && !social.twitter ? (
536537
<div className="warning">
537538
* Please add twitter username to use these add-ons
538539
</div>
539540
) : (
540541
""
541542
)}
543+
{social.twitter && !isTwitterUsernameValid(social.twitter) ? (
544+
<div className="warning">
545+
* Twitter username is invalid, please add a valid username
546+
</div>
547+
) : (
548+
""
549+
)}
542550
</div>
543551
<div className="flex items-center justify-center w-full">
544552
<div

src/utils/validation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ const isMediumUsernameValid = username => {
99
return true
1010
}
1111

12-
export { isGitHubUsernameValid, isMediumUsernameValid }
12+
const isTwitterUsernameValid = username => {
13+
var pattern = /^[a-zA-Z0-9_]{1,15}$/
14+
return pattern.test(username)
15+
}
16+
17+
export { isGitHubUsernameValid, isMediumUsernameValid, isTwitterUsernameValid }

0 commit comments

Comments
 (0)