|
|
@ -7,6 +7,12 @@ function isHeading(type) { |
|
|
|
return /h[1-6]/i.test(type); |
|
|
|
} |
|
|
|
|
|
|
|
function mdLangToHljsLang(lang) { |
|
|
|
return lang.toLowerCase() === 'jsx' ? |
|
|
|
'javascript' : |
|
|
|
lang; |
|
|
|
} |
|
|
|
|
|
|
|
export function objectToComponent(object, index) { |
|
|
|
if (object === null) return; |
|
|
|
|
|
|
@ -26,13 +32,26 @@ export function objectToComponent(object, index) { |
|
|
|
|
|
|
|
if (object.type === 'html') { |
|
|
|
return React.createElement('div', { |
|
|
|
className: 'markdown', |
|
|
|
key: index, |
|
|
|
dangerouslySetInnerHTML: { __html: children } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (isHeading(object.type)) { |
|
|
|
return React.createElement(object.type, { |
|
|
|
key: index, |
|
|
|
}, [ |
|
|
|
object.children, |
|
|
|
<a className="anchor" key="anchor">#</a>, |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
if (object.type === 'code') { |
|
|
|
const highlightedCode = hljs.highlight('javascript', children).value; |
|
|
|
const highlightedCode = hljs.highlight( |
|
|
|
mdLangToHljsLang(object.props.lang), |
|
|
|
children |
|
|
|
).value; |
|
|
|
return ( |
|
|
|
<div className="highlight" key={index}> |
|
|
|
<pre> |
|
|
@ -43,21 +62,13 @@ export function objectToComponent(object, index) { |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
if (isHeading(object.type)) { |
|
|
|
return React.createElement(object.type, { |
|
|
|
key: index, |
|
|
|
}, [ |
|
|
|
object.children, |
|
|
|
<a className="anchor" key="anchor">#</a>, |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof children === 'string') { |
|
|
|
return React.createElement(object.type, { |
|
|
|
key: index, |
|
|
|
dangerouslySetInnerHTML: { __html: children } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return React.createElement( |
|
|
|
object.type, { key: index }, |
|
|
|
children && children.map(objectToComponent) // `hr` has no children
|
|
|
|