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

feat: Enable links in bio #10337

Merged
merged 20 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion components/profile/ProfileDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
class="max-w-lg whitespace-break-spaces text-sm">
<Markdown
:source="userProfile.description"
html
data-testid="profile-description" />
</div>
<!-- Followers -->
Expand Down Expand Up @@ -381,7 +382,6 @@ import {
} from '@/services/profile'
import { removeHttpFromUrl } from '@/utils/url'
import { ButtonConfig, ProfileTab } from './types'

import profileTabsCount from '@/queries/subsquid/general/profileTabsCount.query'
import { openProfileCreateModal } from '@/components/profile/create/openProfileModal'

Expand Down
3 changes: 2 additions & 1 deletion components/profile/create/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { rateLimitedPinFileToIPFS } from '@/services/nftStorage'
import { appClient, createChannel } from '@/services/farcaster'
import { StatusAPIResponse } from '@farcaster/auth-client'
import { useDocumentVisibility } from '@vueuse/core'
import { getBioWithLinks } from '../utils'

const props = defineProps<{
modelValue: boolean
Expand Down Expand Up @@ -107,7 +108,7 @@ const processProfile = async (profileData: ProfileFormData) => {
const profileBody: CreateProfileRequest | UpdateProfileRequest = {
address: profileData.address,
name: profileData.name,
description: profileData.description,
description: getBioWithLinks(profileData.description),
image: imageUrl,
banner: bannerUrl,
socials: constructSocials(profileData),
Expand Down
37 changes: 17 additions & 20 deletions components/profile/create/stages/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,29 @@ const socialLinks = [
watchEffect(async () => {
const profile = userProfile.value
const farcasterProfile = props.farcasterUserData
const useFarcasterData = props.useFarcaster && farcasterProfile
const getProfileSocial = (platform: string) =>
profile?.socials.find((s) => s.platform === platform)

// Use Farcaster data if useFarcaster is true and data is available, otherwise fallback to profile data
form.name =
props.useFarcaster && farcasterProfile
? farcasterProfile.displayName ?? ''
: profile?.name ?? ''
form.description =
props.useFarcaster && farcasterProfile
? farcasterProfile.bio ?? ''
: profile?.description ?? ''
form.imagePreview =
props.useFarcaster && farcasterProfile
? farcasterProfile.pfpUrl
: profile?.image
form.name = useFarcasterData
? farcasterProfile.displayName ?? ''
: profile?.name ?? ''
form.description = useFarcasterData
? farcasterProfile.bio ?? ''
: profile?.description ?? ''
form.imagePreview = useFarcasterData
? farcasterProfile.pfpUrl
: profile?.image
form.bannerPreview = profile?.banner ?? undefined // Banner preview assumed to always come from the profile

// Conditional for Farcaster handle based on the useFarcaster prop
form.farcasterHandle =
props.useFarcaster && farcasterProfile
? farcasterProfile.username
: profile?.socials.find((s) => s.platform === 'Farcaster')?.handle
form.farcasterHandle = useFarcasterData
? farcasterProfile.username
: getProfileSocial('Farcaster')?.handle

// Social handles are fetched from profile regardless of the Farcaster usage
form.twitterHandle = profile?.socials.find(
(s) => s.platform === 'Twitter',
)?.handle
form.website = profile?.socials.find((s) => s.platform === 'Website')?.handle
form.twitterHandle = getProfileSocial('Twitter')?.handle
form.website = getProfileSocial('Website')?.handle
})
</script>
31 changes: 31 additions & 0 deletions components/profile/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type LinkableBlock = {
regex: RegExp
template: (match: string) => string
}

const createLink = (content: string, url: string) =>
`<a href="${url}" target="_blank" rel="nofollow noopener noreferrer">${content}</a>`

const LINKABLE_BLOCKS: LinkableBlock[] = [
{
regex:
/(?<!\[[^\]]*)(?<!\([^\)]*)(?<!<a[^>]*>)\/\w+(?![^<>]*<\/a>)(?![^<>]*>)(?![^\[]*\])(?![^\(]*\))/,
template: (match: string) => `https://warpcast.com/~/channel${match}`,
},
{
regex:
/(?<!\[[^\]]*)(?<!\([^\)]*)(?<!<a[^>]*>)@(\w{1,15})\b(?!<\/a>)(?![^\[]*\])(?![^\(]*\))/,
template: (match: string) =>
`https://warpcast.com/${match.slice(1, match.length)}`,
},
]

export const getBioWithLinks = (description: string) => {
return LINKABLE_BLOCKS.reduce(
(reducer, { regex, template }) =>
reducer.replace(new RegExp(regex, 'g'), (match: string) =>
createLink(match, template(match)),
),
description,
)
}
13 changes: 9 additions & 4 deletions components/shared/Markdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
import MarkdownIt from 'markdown-it'
import Prism from 'prismjs'

const props = withDefaults(
defineProps<{
source: string
html?: boolean
}>(),
{ html: false },
)

const markdown = new MarkdownIt({
breaks: true,
html: props.html,
highlight: (code: string, lang: string) => {
if (lang && Prism.languages[lang]) {
return `<pre class="language-${lang}"><code>${Prism.highlight(
Expand All @@ -23,9 +32,5 @@ const markdown = new MarkdownIt({
},
})

defineProps<{
source: string
}>()

// useRedirectModal(markdown)
</script>
Loading