Skip to content

Commit 312a908

Browse files
committed
Merge branch 'master' of github.com:sanity-io/markdown-to-sanity
2 parents b671fc7 + fd2cc3f commit 312a908

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 = [
@@ -47,26 +47,56 @@ function convertHTMLtoPortableText (HTMLDoc) {
4747
if (el.tagName.toLowerCase() !== 'pre') {
4848
return undefined
4949
}
50-
const code = el.children[0]
51-
const childNodes =
52-
code && code.tagName.toLowerCase() === 'code'
53-
? code.childNodes
50+
const codeElement = el.children[0]
51+
const codeElementNodes =
52+
codeElement && codeElement.tagName.toLowerCase() === 'code'
53+
? codeElement.childNodes
5454
: el.childNodes
55-
let text = ''
56-
childNodes.forEach(node => {
57-
text += node.textContent
55+
let code = ''
56+
codeElementNodes.forEach(node => {
57+
code += node.textContent
5858
})
59-
/**
60-
* use `block()` to add it to the
61-
* root array, instead of as
62-
* children of a block
63-
* */
6459

60+
let language = 'text';
61+
if(codeElement.className){
62+
language = codeElement.className.split("-")[1];
63+
}
6564
return block({
6665
_type: 'code',
67-
text: text
66+
code: code,
67+
language: language
6868
})
6969
}
70+
},
71+
{
72+
deserialize (el, next, block) {
73+
if (el.tagName === 'IMG') {
74+
return {
75+
_type: 'img',
76+
asset: {
77+
src: `${el.getAttribute('src').replace(/^\/\//, 'https://')}`,
78+
alt: `${el.getAttribute('alt')}`
79+
}
80+
}
81+
}
82+
83+
if (
84+
el.tagName.toLowerCase() === 'p' &&
85+
el.childNodes.length === 1 &&
86+
el.childNodes.tagName &&
87+
el.childNodes[0].tagName.toLowerCase() === 'img'
88+
) {
89+
return {
90+
_type: 'img',
91+
asset: {
92+
src: `${el.getAttribute('src').replace(/^\/\//, 'https://')}`,
93+
alt: `${el.getAttribute('alt')}`
94+
}
95+
}
96+
}
97+
// Only convert block-level images, for now
98+
return undefined
99+
}
70100
}
71101
]
72102
/**
@@ -79,5 +109,4 @@ function convertHTMLtoPortableText (HTMLDoc) {
79109
parseHtml: html => new JSDOM(html).window.document
80110
})
81111
}
82-
83-
module.exports = convertHTMLtoPortableText
112+
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)