Skip to content

Commit

Permalink
fix(block-tools): enforce list support in schema
Browse files Browse the repository at this point in the history
HTML lists should not be deserialized to list blocks if there is no such support in schema
  • Loading branch information
skogsmaskin committed Oct 10, 2023
1 parent 4151f2e commit 882e4f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/@sanity/block-tools/src/HtmlDeserializer/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export function createRuleOptions(blockContentType: ArraySchemaType): BlockEnabl
const enabledBlockStyles = features.styles.map((item) => item.value || item.title)
const enabledSpanDecorators = features.decorators.map((item) => item.value || item.title)
const enabledBlockAnnotations = features.annotations.map((item) => item.value || item.title || '')
const enabledListTypes = features.lists.map((item) => item.value || item.title || '')
return {
enabledBlockStyles,
enabledSpanDecorators,
enabledBlockAnnotations,
enabledListTypes,
}
}

Expand Down
25 changes: 18 additions & 7 deletions packages/@sanity/block-tools/src/HtmlDeserializer/rules/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ import {
} from '../../constants'
import {BlockEnabledFeatures, DeserializerRule} from '../../types'

export function resolveListItem(listNodeTagName: string): string {
export function resolveListItem(
listNodeTagName: string,
enabledListTypes: string[],
): string | undefined {
let listStyle
switch (listNodeTagName) {
case 'ul':
listStyle = 'bullet'
if (enabledListTypes.includes('bullet')) {
listStyle = 'bullet'
}
break
case 'ol':
listStyle = 'number'
if (enabledListTypes.includes('number')) {
listStyle = 'number'
}
break
default:
listStyle = 'bullet'
listStyle = undefined
}
return listStyle
}
Expand Down Expand Up @@ -188,15 +195,19 @@ export default function createHTMLRules(
},
}, // Deal with list items
{
deserialize(el, next) {
deserialize(el, next, block) {
const tag = tagName(el)
const listItem = tag ? HTML_LIST_ITEM_TAGS[tag] : undefined
const parentTag = tagName(el.parentNode) || ''
const enabledListItem = resolveListItem(parentTag, options.enabledListTypes)
if (!listItem || !el.parentNode || !HTML_LIST_CONTAINER_TAGS[parentTag]) {
return undefined
}

listItem.listItem = resolveListItem(parentTag)
// If list item style is not supported, return a defaultBlockType
if (!enabledListItem) {
return block({_type: blockContentType.name, children: next(el.childNodes)})
}
listItem.listItem = enabledListItem
return {
...listItem,
children: next(el.childNodes),
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/block-tools/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@ export interface DeserializerRule {
export interface BlockEnabledFeatures {
enabledBlockStyles: string[]
enabledSpanDecorators: string[]
enabledListTypes: string[]
enabledBlockAnnotations: string[]
}

0 comments on commit 882e4f1

Please sign in to comment.