Skip to content

Commit fd2cc3f

Browse files
authored
Better code creation (#5)
Better code creation
2 parents acc0b10 + e0abb8a commit fd2cc3f

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/bin/index.js",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}

src/convertHTMLtoPortableText.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const blockContentType = defaultSchema
1313
.fields.find(field => field.name === 'body').type
1414

1515
function 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

src/defaultSchema.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ module.exports = Schema.compile({
4545
]
4646
}
4747
]
48+
},
49+
{
50+
name: 'image',
51+
type: 'object',
52+
title: 'image',
53+
fields: [
54+
{
55+
title: 'src',
56+
name: 'src',
57+
type: 'string'
58+
},
59+
{
60+
title: 'alt',
61+
name: 'alt',
62+
type: 'string'
63+
}
64+
]
4865
}
4966
]
5067
}

0 commit comments

Comments
 (0)