Skip to content

Commit

Permalink
fix(markdown): convert inline CSS from Google Docs to Markdown
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
domcleal committed Dec 18, 2024
1 parent 022dbe5 commit c7cebde
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const TEXT_TAGS = {
U: () => ({ underline: true }),
};

const INLINE_STYLES = {
'font-style': value => (value === 'italic' ? { italic: true } : {}),
'font-weight': value => (value === 'bold' || parseInt(value, 10) >= 600 ? { bold: true } : {}),
};

function deserialize(el) {
if (el.nodeType === 3) {
return el.textContent;
Expand Down Expand Up @@ -65,6 +70,20 @@ function deserialize(el) {
return children.map(child => jsx('text', attrs, child));
}

// Convert inline CSS on span elements generated by Google Docs
if (nodeName === 'SPAN') {
const attrs = {};
for (let i = 0; i < el.style.length; i++) {
const propertyName = el.style[i];
if (INLINE_STYLES[propertyName]) {
const propertyValue = el.style.getPropertyValue(propertyName);
const propertyStyle = INLINE_STYLES[propertyName](propertyValue);
Object.assign(attrs, propertyStyle);
}
}
return children.map(child => jsx('text', attrs, child));
}

return children;
}

Expand Down

0 comments on commit c7cebde

Please sign in to comment.