def METHOD_NAME(self, text: str, level: int, raw: None = None) -> str:
    custom_id_match = re.match(r'^(.*?)\s+{([a-z0-9\-_]+)}\s*$', text)
    if custom_id_match:
        text = custom_id_match.group(1)
        id = custom_id_match.group(2)
        if id in self.toc_ids:
            raise ValueError(f'Duplicate header ID in Markdown: "{id}"')
    else:
        id = 'h{level}_{title}'.format(
            level=level,
            title=re.sub(r'[^a-z0-9\-_ ]', '', strip_tags(text).lower()).strip().replace(' ', '-'),
        )
        # dumb collision avoidance
        while id in self.toc_ids:
            id += '_'
    self.toc.append((level, text, id))
    self.toc_ids.add(id)
    return '<h{level} id="{id}">{text} <a class="anchor" href="#{id}"><span></span></a></h{level}>\n'.format(
        level=level,
        id=id,
        text=text,
    )