Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created May 14, 2020 07:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmelve/a866d5bbf8eecde8171cb246a454aaab to your computer and use it in GitHub Desktop.
Save kmelve/a866d5bbf8eecde8171cb246a454aaab to your computer and use it in GitHub Desktop.
Mininal exampe of how to Twitter embeds with Santiy.io
// 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
// Minimal rich text field with a custom tweet embed block
export default {
name: 'richText',
type: 'array',
title: 'Rich text',
of: [
{
type: 'block'
},
{
type: 'tweet'
}
]
}
// 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