_generate_html_toc adds libmarkdown.MKD_TOC to .flags, so differing from the other ones that call _get_compiled_doc. I am not sure what the effect of changing .flags is.
Since the memoized value is invalidated want to
if self.flags &libmarkdown.MKD_TOC ==0: delattr(self, '_doc')