add fromString method
This commit is contained in:
15
packages/core/src/utils/fromString.ts
Normal file
15
packages/core/src/utils/fromString.ts
Normal 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
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { ParseRule } from 'prosemirror-model'
|
import { ParseRule } from 'prosemirror-model'
|
||||||
import { ExtensionAttribute } from '../types'
|
import { ExtensionAttribute } from '../types'
|
||||||
|
import fromString from './fromString'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function merges extension attributes into parserule attributes (`attrs` or `getAttrs`).
|
* 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
|
const attributes = item.attribute.parseHTML
|
||||||
? item.attribute.parseHTML(node as HTMLElement) || {}
|
? 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)
|
const filteredAttributes = Object.fromEntries(Object.entries(attributes)
|
||||||
|
|||||||
Reference in New Issue
Block a user