
Security News
Socket Integrates With Bun 1.3’s Security Scanner API
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
@lyra/block-content-to-react
Advanced tools
React component for transforming Lyra block content to React components
Render an array of block text from Lyra with React or React Native.
npm install --save @lyra/block-content-to-react
const React = require('react')
const ReactDOM = require('react-dom')
const BlockContent = require('@lyra/block-content-to-react')
const client = require('@lyra/client')({
dataset: '<some dataset>'
})
const serializers = {
types: {
code: props => (
<pre data-language={props.node.language}>
<code>{props.node.code}</code>
</pre>
)
}
}
client.fetch('*[_type == "article"][0]').then(article => {
ReactDOM.render(
<BlockContent blocks={article.body} serializers={serializers} />,
document.getElementById('root')
)
})
className
- When more than one block is given, a container node has to be created. Passing a className
will pass it on to the container. Note: see renderContainerOnSingleChild
.renderContainerOnSingleChild
- When a single block is given as input, the default behavior is to not render any container. If you always want to render the container, pass true
.serializers
- Specifies the React components to use for rendering content. Merged with default serializers.serializers.types
- Serializers for block types, see example aboveserializers.marks
- Serializers for marks - data that annotates a text child of a block. See example usage below.serializers.list
- React component to use when rendering a list nodeserializers.listItem
- React component to use when rendering a list item nodeserializers.hardBreak
- React component to use when transforming newline characters to a hard break (<br/>
by default, pass false
to render newline character)serializers.container
- Serializer for the container wrapping the blocksimageOptions
- When encountering image blocks, this defines which query parameters to apply in order to control size/crop mode etc.In addition, in order to render images without materializing the asset documents, you should also specify:
dataset
- Name of the Lyra dataset containing the document that is being rendered.const input = [{
_type: 'block',
children: [{
_key: 'a1ph4',
_type: 'span',
marks: ['s0m3k3y'],
text: 'Lyra'
}],
markDefs: [{
_key: 's0m3k3y',
_type: 'highlight',
color: '#E4FC5B'
}]
}]
const highlight = props => {
return (
<span style={{backgroundColor: props.mark.color}}>
{props.children}
</span>
)
}
<BlockContent
blocks={input}
serializers={{marks: {highlight}}}
/>
<BlockContent
blocks={input}
imageOptions={{w: 320, h: 240, fit: 'max'}}
dataset="mydataset"
/>
block
-typeconst BlockRenderer = props => {
const style = props.node.style || 'normal'
if (/^h\d/.test(style)) {
const level = style.replace(/[^\d]/g, '')
return <h2 className={`my-heading level-${level}`}>{props.children}</h2>
}
return style === 'blockquote'
? <blockquote className="my-block-quote">{props.children}</blockquote>
: <p className="my-paragraph">{props.children}</p>
}
<BlockContent
blocks={input}
serializers={{types: {block: BlockRenderer}}}
/>
React Native support is experimental, but should work. Built-in serializers render using React Native elements instead of HTML tags when React Native is detected, and serializers can be specified as you normally would.
Since there are fewer built-in elements in React Native, elements such as lists are implemented using plain (text) views, styled to look rougly like the HTML variant. You'll probably want to customize most of the serializers when using React Native, since styling is not handled in a cascading fashion.
MIT-licensed. See LICENSE.
FAQs
React component for transforming Lyra block content to React components
We found that @lyra/block-content-to-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Security News
Python 3.14 adds template strings, deferred annotations, and subinterpreters, plus free-threaded mode, an experimental JIT, and Sigstore verification.
Security News
Former RubyGems maintainers have launched The Gem Cooperative, a new community-run project aimed at rebuilding open governance in the Ruby ecosystem.