@@ -415,8 +415,9 @@ const attrName = (d: Data) => {
415415}
416416
417417const attrValue = ( d : Data ) => {
418- const attrValue = d . input . slice ( d . start , d . index )
419- d . tag ! . attrs ! [ d . attrName ] = decodeHTML ( attrValue )
418+ const value = decodeHTML ( d . input . slice ( d . start , d . index ) )
419+ // 如果是数字,则转为数字类型
420+ d . tag ! . attrs ! [ d . attrName ] = isValidNumber ( value ) ? Number ( value ) : value
420421}
421422
422423const elementStart = ( d : Data , gt ?: boolean ) => {
@@ -536,6 +537,18 @@ const isMarksEqual = (marks1: Mark[] = [], marks2: Mark[] = []) => {
536537 } )
537538}
538539
540+ /**
541+ * 判断字符串是否为有效数字
542+ * @eg isValidNumber('338.74') // true
543+ * @eg isValidNumber('-338.74') // true
544+ * @eg isValidNumber('338') // true
545+ * @eg isValidNumber('338.') // false
546+ * @eg isValidNumber('.74') // false
547+ */
548+ const isValidNumber = ( str : string ) : boolean => {
549+ return / ^ - ? \d + ( \. \d + ) ? $ / . test ( str . trim ( ) )
550+ }
551+
539552// setTimeout(() => {
540553// const d = parseHTML(`<p>show note</p><audio src="" audio-uuid="BEZ2dKG2GU2LdFpTxxssn" audio-duration="814.162313" show-note="<alert>\n\n01:23 啦啦啦\n01:23.111 啦啦啦"><p>啦啦啦</p></audio><p></p>`, {
541554// alias: {
@@ -554,3 +567,26 @@ const isMarksEqual = (marks1: Mark[] = [], marks2: Mark[] = []) => {
554567
555568// console.log(JSON.stringify(d.doc))
556569// })
570+
571+
572+ setTimeout ( ( ) => {
573+ const d = parseHTML ( `<audio><p>墨问西东<br>第二行</p></audio><p></p>` , {
574+ selfClose : [ 'image' , 'headBreak' ] ,
575+ alias : {
576+ p : 'paragraph' ,
577+ br : 'headBreak' ,
578+ img : 'image' ,
579+ 'audio > p' : 'audioText'
580+ } ,
581+ markRule : [ {
582+ type : 'strong' ,
583+ mark : { type : 'bold' }
584+ } , {
585+ type : 'span' ,
586+ attrs : { type : 'highlight' } ,
587+ mark : { type : 'highlight' }
588+ } ]
589+ } )
590+
591+ console . log ( d . doc )
592+ } )
0 commit comments