Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Allow more complex mark handlers to be defined outside of span handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Sep 19, 2017
1 parent 58845ec commit 412a074
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@sanity/block-content-to-react",
"description":
"React component for transforming Sanity block content to React components",
"version": "0.2.3-next",
"version": "0.2.4",
"main": "lib/index.js",
"umd": "umd/index.min.js",
"scripts": {
Expand All @@ -24,7 +24,7 @@
"author": "Sanity <hello@sanity.io>",
"license": "MIT",
"dependencies": {
"@sanity/block-content-to-tree": "0.2.0-next",
"@sanity/block-content-to-tree": "^0.3.0",
"in-publish": "^2.0.0",
"object-assign": "^4.1.1"
},
Expand Down
45 changes: 28 additions & 17 deletions src/typeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,38 @@ function getListItems(items, listHandlers, typeHandlers) {
}

function mapMark(mark, marksMapping) {
if (marksMapping && typeof marksMapping[mark] !== 'undefined') {
return marksMapping[mark]
const markName = mark._type || mark
if (marksMapping && typeof marksMapping[markName] !== 'undefined') {
return marksMapping[markName]
}

return mark
return markName
}

module.exports = function(blockTypeHandlers = {}, customBlockHandler) {
const defaultHandlers = {
normal: node => h('p', {key: node.nodeKey}, node.children)
const defaultMarkHandlers = {
link: props => {
return h('a', {key: props.nodeKey, href: props.href}, props.children)
}
}

const defaultHandlers = {
normal: node => h('p', {key: node.nodeKey}, node.children)
}

module.exports = function(blockTypeHandlers = {}, customBlockHandler) {
const blockHandlers = objectAssign(
{},
defaultHandlers,
blockTypeHandlers.textBlock || {},
blockTypeHandlers.customBlock || {}
)

blockHandlers.marks = objectAssign(
{},
defaultMarkHandlers,
blockTypeHandlers.marks
)

const listHandlers = objectAssign(
{
number: node => h('ol', {key: node.nodeKey}, node.children),
Expand Down Expand Up @@ -109,10 +124,13 @@ module.exports = function(blockTypeHandlers = {}, customBlockHandler) {
},

span: node => {
let wrap = input => input
if (typeof node.mark === 'string') {
const mark = mapMark(node.mark, blockTypeHandlers.marks)
wrap = mark ? input => h(mark, {key: node.nodeKey}, input) : wrap
let wrap = children => children
if (node.mark) {
const mark = mapMark(node.mark, blockHandlers.marks)
wrap = mark
? children =>
h(mark, objectAssign({key: node.nodeKey}, node.mark), children)
: wrap
}

node.children = getChildren(node.children, typeHandlers, node)
Expand All @@ -121,13 +139,6 @@ module.exports = function(blockTypeHandlers = {}, customBlockHandler) {
return wrap(blockTypeHandlers.span(node))
}

if (node.mark && node.mark._type === 'link') {
// Deal with the default block editor setup 'link' attribute
return wrap(
h('a', {href: node.mark.href, key: node.nodeKey}, node.children)
)
}

return wrap(node.children)
}
}
Expand Down
24 changes: 5 additions & 19 deletions test/BlockContentToReact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const h = React.createElement
const render = props => ReactDOM.renderToStaticMarkup(h(BlockContent, props))

const blockTypeHandlers = {
marks: {em: null},
marks: {
em: null,
author: props => h('div', null, props.name),
link: props => h('a', {className: 'foo', href: props.href}, props.children)
},
listBlock: {
number: node =>
h('ol', {key: node.nodeKey, className: 'foo'}, node.children),
Expand All @@ -28,23 +32,6 @@ const blockTypeHandlers = {
{key: node.nodeKey, className: 'big-heading', id: node.extra},
node.children
)
},
span: node => {
if (node.mark && node.mark._type === 'author') {
return h('div', null, node.mark.name)
}

if (node.mark && node.mark._type === 'link') {
return h(
'a',
{key: node.nodeKey, className: 'foo', href: node.mark.href},
node.children
)
}

return node.children && node.children.length === 1
? node.children[0]
: h('span', null, node.children)
}
}

Expand All @@ -59,7 +46,6 @@ const renderCustom = props => {
return h(
'div',
{className: 'grid', key: node.nodeKey},
node.content,
defaultBlock(node)
)
}
Expand Down

0 comments on commit 412a074

Please sign in to comment.