add fromString method

This commit is contained in:
Philipp Kühn
2020-10-31 23:35:30 +01:00
parent 1422704f68
commit b570fcb560
2 changed files with 17 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
export default function fromString(value: any) {
if (value.match(/^\d*(\.\d+)?$/)) {
return Number(value)
}
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
return value
}

View File

@@ -1,5 +1,6 @@
import { ParseRule } from 'prosemirror-model'
import { ExtensionAttribute } from '../types'
import fromString from './fromString'
/**
* This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
@@ -29,7 +30,7 @@ export default function injectExtensionAttributesToParseRule(parseRule: ParseRul
const attributes = item.attribute.parseHTML
? item.attribute.parseHTML(node as HTMLElement) || {}
: {
[item.name]: (node as HTMLElement).getAttribute(item.name),
[item.name]: fromString((node as HTMLElement).getAttribute(item.name)),
}
const filteredAttributes = Object.fromEntries(Object.entries(attributes)