| 1 |
import collapseWhitespace from './collapse-whitespace' |
| 2 |
import HTMLParser from './html-parser' |
| 3 |
import { isBlock, isVoid } from './utilities' |
| 4 |
|
| 5 |
export default function RootNode (input, options) { |
| 6 |
let root |
| 7 |
if (typeof input === 'string') { |
| 8 |
const doc = htmlParser().parseFromString( |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
'<x-turndown id="turndown-root">' + input + '</x-turndown>', |
| 13 |
'text/html' |
| 14 |
) |
| 15 |
root = doc.getElementById('turndown-root') |
| 16 |
} else { |
| 17 |
root = input.cloneNode(true) |
| 18 |
} |
| 19 |
collapseWhitespace({ |
| 20 |
element: root, |
| 21 |
isBlock, |
| 22 |
isVoid, |
| 23 |
isPre: options.preformattedCode ? isPreOrCode : null |
| 24 |
}) |
| 25 |
|
| 26 |
return root |
| 27 |
} |
| 28 |
|
| 29 |
let _htmlParser |
| 30 |
function htmlParser () { |
| 31 |
_htmlParser = _htmlParser || new HTMLParser() |
| 32 |
return _htmlParser |
| 33 |
} |
| 34 |
|
| 35 |
function isPreOrCode (node) { |
| 36 |
return node.nodeName === 'PRE' || node.nodeName === 'CODE' |
| 37 |
} |