Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
@sanity/block-content-to-html
Advanced tools
Changelog
2.0.0 - 2021-05-17
div
with a message noting that a serializer is missing. A message will also be logged to the console. To use the old behavior of throwing un known types, pass ignoreUnknownTypes: false
as a property.markFallback
serializer has been renamed to unknownMark
to align with the new unknownType
serializer for blocks.Readme
Render an array of block text from Sanity to HTML.
npm install --save @sanity/block-content-to-html
const blocksToHtml = require('@sanity/block-content-to-html')
const client = require('@sanity/client')({
projectId: '<your project id>',
dataset: '<some dataset>',
apiVersion: '2021-03-25',
useCdn: true
})
// `h` is a way to build HTML known as hyperscript
// See https://github.com/hyperhype/hyperscript for more info
const h = blocksToHtml.h
const serializers = {
types: {
code: props => (
h('pre', {className: props.node.language},
h('code', props.node.code)
)
)
}
}
client.fetch('*[_type == "article"][0]').then(article => {
const el = blocksToHtml({
blocks: article.body,
serializers: serializers
})
document.getElementById('root').appendChild(el)
})
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 that if only a single block is given as input, the container node will be skipped.serializers
- Specifies the functions 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
- Function to use when rendering a list nodeserializers.listItem
- Function to use when rendering a list item nodeserializers.hardBreak
- Function to use when transforming newline characters to a hard break (<br/>
by default, pass false
to render newline character)serializers.unknownType
- Override the default serializer for blocks of unknown type, if ignoreUnknownTypes
is set to false
(the default).serializers.unknownMark
- Override the default serializer for marks of unknown type. Defaults to a span without any styling.imageOptions
- When encountering image blocks, this defines which query parameters to apply in order to control size/crop mode etc.ignoreUnknownTypes
- By default (or when setting this property explicitly to true
) it will output a hidden <div>
with a warning. By setting this property to false
, the renderer will throw an error when encountering unknown block types. The behavior of the unknown type rendering can be customized by specifying a serializer with serializers.unknownType
.In addition, in order to render images without materializing the asset documents, you should also specify:
projectId
- The ID of your Sanity project.dataset
- Name of the Sanity dataset containing the document that is being rendered.const input = [{
_type: 'block',
children: [{
_key: 'a1ph4',
_type: 'span',
marks: ['s0m3k3y'],
text: 'Sanity'
}],
markDefs: [{
_key: 's0m3k3y',
_type: 'highlight',
color: '#E4FC5B'
}]
}]
const highlight = props => (
h('span', {style: {backgroundColor: props.mark.color}}, props.children)
)
const content = blocksToHtml({
blocks: input,
serializers: {marks: {highlight}}
})
blocksToHtml({
blocks: input,
imageOptions: {w: 320, h: 240, fit: 'max'},
projectId: 'myprojectid',
dataset: 'mydataset',
})
block
-typeconst BlockRenderer = props => {
const style = props.node.style || 'normal'
if (/^h\d/.test(style)) {
const level = style.replace(/[^\d]/g, '')
return h('h2', {className: `my-heading level-${level}`}, props.children)
}
return style === 'blockquote'
? h('blockquote', {className: 'my-block-quote'}, props.children)
: h('p', {className: 'my-paragraph'}, props.children)
}
blocksToHtml({
blocks: input,
serializers: {types: {block: BlockRenderer}}
})
MIT-licensed. See LICENSE.
FAQs
Function for transforming Sanity block content to HTML
The npm package @sanity/block-content-to-html receives a total of 5,813 weekly downloads. As such, @sanity/block-content-to-html popularity was classified as popular.
We found that @sanity/block-content-to-html demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 24 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
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).