Created
May 14, 2020 07:45
-
-
Save kmelve/a866d5bbf8eecde8171cb246a454aaab to your computer and use it in GitHub Desktop.
Mininal exampe of how to Twitter embeds with Santiy.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of how to output this in a React frontend | |
import React from 'react' | |
import BlockContent from '@sanity/block-content-to-react' | |
import { TwitterTweetEmbed } from 'react-twitter-embed' | |
const Tweet = ({url}) => { | |
const exp = /\/status\/(\d+)($|[?/])/ | |
const [, id] = exp.exec(url) || [] | |
if (id) { | |
return <TwitterTweetEmbed className="sliderBoxes" tweetId={id} options={{conversation: 'none', 'hide-thread': true}} /> | |
} | |
} | |
const serializers = { | |
types: { | |
tweet: ({node}) => <Tweet url={node.url} /> | |
} | |
} | |
const BlockContent = ({blocks}) => <BlockContent blocks={blocks} serializers={serializers} /> | |
export default BlockContent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Minimal rich text field with a custom tweet embed block | |
export default { | |
name: 'richText', | |
type: 'array', | |
title: 'Rich text', | |
of: [ | |
{ | |
type: 'block' | |
}, | |
{ | |
type: 'tweet' | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make a object type for the twitter data that holds the URL (and can be repurposed for any presentation) | |
import React from 'react' | |
import { TwitterTweetEmbed } from 'react-twitter-embed' | |
const Tweet = ({ value: {url} }) => { | |
const exp = /\/status\/(\d+)($|[?/])/ | |
const [, id] = exp.exec(url) || [] | |
if (id) { | |
return <TwitterTweetEmbed className="sliderBoxes" tweetId={id} options={{conversation: 'none', 'hide-thread': true}} /> | |
} | |
} | |
export default { | |
name: 'tweet', | |
type: 'object', | |
title: 'Twitter embed', | |
fields: [ | |
{ | |
name: 'url', | |
type: 'string', | |
title: 'Tweet URL' | |
} | |
], | |
preview: { | |
select: { 'url': url }, | |
component: Tweet | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment