@@ -13,7 +13,7 @@ const blockContentType = defaultSchema
1313 . fields . find ( field => field . name === 'body' ) . type
1414
1515function convertHTMLtoPortableText ( HTMLDoc ) {
16- if ( ! ( HTMLpattern . test ( HTMLDoc ) ) ) {
16+ if ( ! HTMLpattern . test ( HTMLDoc ) ) {
1717 return [ ]
1818 }
1919 const rules = [
@@ -23,26 +23,56 @@ function convertHTMLtoPortableText (HTMLDoc) {
2323 if ( el . tagName . toLowerCase ( ) !== 'pre' ) {
2424 return undefined
2525 }
26- const code = el . children [ 0 ]
27- const childNodes =
28- code && code . tagName . toLowerCase ( ) === 'code'
29- ? code . childNodes
26+ const codeElement = el . children [ 0 ]
27+ const codeElementNodes =
28+ codeElement && codeElement . tagName . toLowerCase ( ) === 'code'
29+ ? codeElement . childNodes
3030 : el . childNodes
31- let text = ''
32- childNodes . forEach ( node => {
33- text += node . textContent
31+ let code = ''
32+ codeElementNodes . forEach ( node => {
33+ code += node . textContent
3434 } )
35- /**
36- * use `block()` to add it to the
37- * root array, instead of as
38- * children of a block
39- * */
4035
36+ let language = 'text' ;
37+ if ( codeElement . className ) {
38+ language = codeElement . className . split ( "-" ) [ 1 ] ;
39+ }
4140 return block ( {
4241 _type : 'code' ,
43- text : text
42+ code : code ,
43+ language : language
4444 } )
4545 }
46+ } ,
47+ {
48+ deserialize ( el , next , block ) {
49+ if ( el . tagName === 'IMG' ) {
50+ return {
51+ _type : 'img' ,
52+ asset : {
53+ src : `${ el . getAttribute ( 'src' ) . replace ( / ^ \/ \/ / , 'https://' ) } ` ,
54+ alt : `${ el . getAttribute ( 'alt' ) } `
55+ }
56+ }
57+ }
58+
59+ if (
60+ el . tagName . toLowerCase ( ) === 'p' &&
61+ el . childNodes . length === 1 &&
62+ el . childNodes . tagName &&
63+ el . childNodes [ 0 ] . tagName . toLowerCase ( ) === 'img'
64+ ) {
65+ return {
66+ _type : 'img' ,
67+ asset : {
68+ src : `${ el . getAttribute ( 'src' ) . replace ( / ^ \/ \/ / , 'https://' ) } ` ,
69+ alt : `${ el . getAttribute ( 'alt' ) } `
70+ }
71+ }
72+ }
73+ // Only convert block-level images, for now
74+ return undefined
75+ }
4676 }
4777 ]
4878 /**
@@ -55,5 +85,4 @@ function convertHTMLtoPortableText (HTMLDoc) {
5585 parseHtml : html => new JSDOM ( html ) . window . document
5686 } )
5787}
58-
59- module . exports = convertHTMLtoPortableText
88+ module . exports = convertHTMLtoPortableText
0 commit comments