Skip to content

Commit

Permalink
feat: add CID cutter
Browse files Browse the repository at this point in the history
  • Loading branch information
BATMAH69 committed Aug 9, 2023
1 parent 2143eaa commit 712eedf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions modules/shared/utils/regexCID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const PATTERN_CID = `\\b(${CID_0_58_BTC}|${CID_1_16}|${CID_1_32}|${CID_1_58_BTC}

export const REGEX_CID = new RegExp(PATTERN_CID, 'g')
export const REGEX_CID_ONLY = new RegExp(`^${PATTERN_CID}$`)
export const REGEX_CID_CUTER = /(\w{8})\w+(\w{8}$)/

export const REGEX_LIDO_VOTE_CID = new RegExp(
`\\blidovoteipfs://(${CID_1_32})\\s*$`,
Expand Down
8 changes: 6 additions & 2 deletions modules/shared/utils/replaceCustomElementsInMD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ExternalLink } from '../ui/Common/ExternalLink'
import { AddressBadge } from '../ui/Common/AddressBadge'

import { REGEX_ETH_ADDRESS_ONLY } from 'modules/shared/utils/regexEthAddress'
import { REGEX_CID_ONLY } from 'modules/shared/utils/regexCID'
import { REGEX_CID_CUTER, REGEX_CID_ONLY } from 'modules/shared/utils/regexCID'
import { REGEX_URL_ONLY } from 'modules/shared/utils/regexURL'
import { getUrlFromCID } from 'modules/shared/utils/getUrlFromCID'

Expand All @@ -18,7 +18,11 @@ export const replaceAddressAndCIDInMD: CodeType = ({
const value = Array.isArray(children) ? `${children[0]}` : `${children}`

if (inline && value.match(REGEX_CID_ONLY)) {
return <ExternalLink href={getUrlFromCID(value)}>{value}</ExternalLink>
return (
<ExternalLink href={getUrlFromCID(value)}>
{value.replace(REGEX_CID_CUTER, '$1..$2')}
</ExternalLink>
)
}

if (inline && value.match(REGEX_ETH_ADDRESS_ONLY)) {
Expand Down
6 changes: 4 additions & 2 deletions modules/shared/utils/replaceLinksWithComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AddressBadge } from '../ui/Common/AddressBadge'

import { REGEX_ETH_ADDRESS } from 'modules/shared/utils/regexEthAddress'
import { REGEX_URL } from 'modules/shared/utils/regexURL'
import { REGEX_CID } from 'modules/shared/utils/regexCID'
import { REGEX_CID, REGEX_CID_CUTER } from 'modules/shared/utils/regexCID'
import { getUrlFromCID } from 'modules/shared/utils/getUrlFromCID'

import { replaceRegexWithJSX } from './replaceRegexWithJSX'
Expand All @@ -21,7 +21,9 @@ export const replaceJsxElements = (text: string) => {
{
regex: REGEX_CID,
replace: cid => (
<ExternalLink href={getUrlFromCID(cid)}>{cid}</ExternalLink>
<ExternalLink href={getUrlFromCID(cid)}>
{cid.replace(REGEX_CID_CUTER, '$1..$2')}
</ExternalLink>
),
},
])
Expand Down
12 changes: 9 additions & 3 deletions modules/votes/ui/VoteDescription/VoteDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ export function VoteDescription({ metadata, allowMD }: Props) {
const trimmedData = trimStart(data)

if (error || !trimmedData) {
const loadingInfo = `\n\nA detailed description will be uploaded to an IPFS soon. File hash:${cid} To read the description, please refresh the page in 15 minutes.`
const text = metadata.replace(REGEX_LIDO_VOTE_CID, '') + loadingInfo
return <DescriptionText>{replaceJsxElements(text)}</DescriptionText>
const text = metadata.replace(REGEX_LIDO_VOTE_CID, '')
return (
<DescriptionText>
{replaceJsxElements(text)}
{`\n\nA detailed description will be uploaded to an IPFS soon. File hash: `}
<b>{cid}</b>
{`. To read the description, please refresh the page in 15 minutes.`}
</DescriptionText>
)
}

if (trimmedData && allowMD) {
Expand Down

0 comments on commit 712eedf

Please sign in to comment.