Skip to content

Commit 8b8e873

Browse files
domclealdemshy
andauthored
fix(markdown): convert inline CSS from Google Docs to Markdown (#7351)
Extends the HTML to Markdown conversion to better support bold and italic formatting from Google Docs, which generates inline styles on a `span` element instead of strong/b/em/i type elements. Co-authored-by: Anze Demsar <[email protected]>
1 parent 47a2f70 commit 8b8e873

File tree

1 file changed

+19
-0
lines changed
  • packages/decap-cms-widget-markdown/src/MarkdownControl/plugins/html

1 file changed

+19
-0
lines changed

packages/decap-cms-widget-markdown/src/MarkdownControl/plugins/html/withHtml.js

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const TEXT_TAGS = {
3030
U: () => ({ underline: true }),
3131
};
3232

33+
const INLINE_STYLES = {
34+
'font-style': value => (value === 'italic' ? { italic: true } : {}),
35+
'font-weight': value => (value === 'bold' || parseInt(value, 10) >= 600 ? { bold: true } : {}),
36+
};
37+
3338
function deserialize(el) {
3439
if (el.nodeType === 3) {
3540
return el.textContent;
@@ -65,6 +70,20 @@ function deserialize(el) {
6570
return children.map(child => jsx('text', attrs, child));
6671
}
6772

73+
// Convert inline CSS on span elements generated by Google Docs
74+
if (nodeName === 'SPAN') {
75+
const attrs = {};
76+
for (let i = 0; i < el.style.length; i++) {
77+
const propertyName = el.style[i];
78+
if (INLINE_STYLES[propertyName]) {
79+
const propertyValue = el.style.getPropertyValue(propertyName);
80+
const propertyStyle = INLINE_STYLES[propertyName](propertyValue);
81+
Object.assign(attrs, propertyStyle);
82+
}
83+
}
84+
return children.map(child => jsx('text', attrs, child));
85+
}
86+
6887
return children;
6988
}
7089

0 commit comments

Comments
 (0)