def process_wikilinks(text):
  """Returns clean text and extracted wikilinks from wikitext."""
  clean_text = text
  links = []
  match = RE_MENTION.search(clean_text)
  while match:
    link = match.group("link")
    surface = match.group("surface")
    replacement = surface if surface else link
    start = match.start()
    end = match.start() + len(replacement)
    links.append({"id": clean_wikilink(link), "start": start, "end": end})
    try:
      clean_text = "".join(
          [clean_text[:match.start()], replacement, clean_text[match.end():]])
    except IndexError:
      raise Exception(f"text: {clean_text}, match: {match}")
    match = RE_MENTION.search(clean_text)
  return clean_text, links
Returns clean text and extracted wikilinks from wikitext.
language/fruit/wiki_utils.py