lonekorean/wordpress-export-to-markdown

Something went wrong - parentNode of undefined

Closed this issue · 2 comments

I am trying to run this tool, both through npx and locally, and getting the error:
Something went wrong, execution halted early. TypeError: Cannot read property 'parentNode' of undefined at isHeadingRow (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown-plugin-gfm\lib\turndown-plugin-gfm.cjs.js:100:23) at Object.filter (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown-plugin-gfm\lib\turndown-plugin-gfm.cjs.js:77:41) at filterValue (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:416:16) at findRule (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:404:9) at Rules.forNode (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:389:17) at TurndownService.replacementForNode (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:898:25) at C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:863:40 at NodeList.reduce (<anonymous>) at TurndownService.process (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:856:17) at TurndownService.turndown (C:\Users\sunfire\AppData\Roaming\npm-cache\_npx\15916\node_modules\wordpress-export-to-markdown\node_modules\turndown\lib\turndown.cjs.js:768:26)

I am on version 14+ and from what I can tell, all of my modules are up to date.

I had someone else test and they got the same result.

Would appreciate any guidance on this.

I had the same problem and I seem to have a workaround. I believe the problem that there's a function in the turndown-plugin-gfm.cjs.js dependency that is missing some error-handling.

My quick-and-dirty, once-off, temporary workaround is to edit the function directly in the installed module. Note that this is not a proper fix. That would need to be made upstream in turndown-plugin-gfm.

After running npm install, open node_modules\turndown-plugin-gfm\lib\turndown-plugin-gfm.cjs.js and go to line 100.

Replace the function isHeadingRow with this:

function isHeadingRow (tr) {
  if (tr) {
    var parentNode = tr.parentNode;
    return (
      parentNode.nodeName === 'THEAD' ||
      (
        parentNode.firstChild === tr &&
        (parentNode.nodeName === 'TABLE' || isFirstTbody(parentNode)) &&
        every.call(tr.childNodes, function (n) { return n.nodeName === 'TH' })
      )
    )
  }
}

All I've done there is wrap the function's internals in if (tr) to only return a value if it's been passed a valid value for tr. My guess is that isHeadingRow is being called without being passed a valid node, causing the error. I'm sure there are better fixes than mine, but this worked for me.

For me, after making this change I could successfully export my WordPress XML file (containing over 600 posts) to markdown.

Yeah, this is an upstream issue. turndown has been updated since this issue, although turndown-plugin-gfm has not been.

@arthurattwell that's some good hacking there, love it 😆