frostming/marko

MarkdownRenderer won't render Link elements

Closed this issue · 1 comments

I'm trying to turn some selected Link elements back into text. But MarkdownRenderer gives an error.
Here's an example:

import marko
import marko.md_renderer
doc = marko.parse("[link](http://bouncingdvdlogo.com/)")
link = doc.children[0].children[0]
renderer = marko.md_renderer.MarkdownRenderer()
renderer.render(link)

and the output:

    140 link_title = (
    141     '"{}"'.format(element.title.replace('"', '\\"')) if element.title else None
    142 )
    143 assert self.root_node
    144 label = next(
    145     (
    146         k
--> 147         for k, v in self.root_node.link_ref_defs.items()
    148         if v == (element.dest, link_title)
    149     ),
    150     None,
    151 )
    152 if label is not None:
    153     if label == link_text:

AttributeError: 'Link' object has no attribute 'link_ref_defs'

Seems like these lines might have something to do with it:

marko/marko/renderer.py

Lines 62 to 63 in 625f8a4

if not self.root_node: # pragma: no cover
self.root_node = element # type: ignore

They set the root_node to any Element but it seems like it should only be set to a Document.

Here's a workaround which is to set root_node manually:

import marko
import marko.md_renderer
doc = marko.parse("[link](http://bouncingdvdlogo.com/)")
link = doc.children[0].children[0]
renderer = marko.md_renderer.MarkdownRenderer()
renderer.root_node=doc
renderer.render(link)

Maybe we could throw an error if render_link is called and the root_node isn't set? Then drop these lines so root_node stays None then functions aren't tempted to access it. I tried dropping those lines, the tests still pass.