def _replace_elements_with_text(
    parent: etree.Element, predicate: Callable[[etree.Element], str | None]
) -> None:
    """For each child element, if matched, replace it with the text returned from the predicate."""
    carry_text = ""
    for child in reversed(parent):  # Reversed for the ability to mutate during iteration.
        # Remove matching elements but carry any `tail` text to preceding elements.
        new_text = predicate(child)
        if new_text is not None:
            carry_text = new_text + (child.tail or "") + carry_text
            parent.remove(child)
        elif carry_text:
            child.tail = (child.tail or "") + carry_text
            carry_text = ""
    if carry_text:
        parent.text = (parent.text or "") + carry_text
For each child element, if matched, replace it with the text returned from the predicate.
mkdocs/utils/rendering.py