p = 'frontend/src/components/RichText.jsx'
s = open(p).read()

s = s.replace("""const WIKI_LINK = /\\[\\[(?:(\\d+)(?:#([A-Za-z0-9_-]+))?\\|([^\\]]+)|([^\\]|]+?)(?:\\|([a-z0-9-]+))?)\\]\\]/g""",
"""const WIKI_LINK = /\\[\\[(?:(\\d+)(?:#([A-Za-z0-9_-]+))?\\|([^\\]]+)|([^\\]|]+?)(?:\\|([a-z0-9-]+))?)\\]\\]/g

// `[[deck:12|Cards on this]]` — a link to a deck of cards, written the same
// way a cross-reference to an article is. Placed by hand, usually in a
// question's explanation, and never automatically: a deck link on every
// question would be a row of buttons nobody presses.
const DECK_LINK = /\\[\\[deck:(\\d+)(?:\\|([^\\]]+))?\\]\\]/g

const expandDeckLinks = (text) => (text || '').replace(
  DECK_LINK,
  (_m, id, label) => `[${(label || 'Cards').trim()}](/flashcards/${id})`,
)""")

s = s.replace("""const expandWikiLinks = (text) => (text || '').replace(""",
"""const expandWikiLinks = (text) => expandDeckLinks(text).replace(""")

s = s.replace("""export const plainWikiText = (text) => (text || '').replace(
  WIKI_LINK,
  (_id, _section, idLabel, label) => (idLabel || label || '').trim(),
)""",
"""export const plainWikiText = (text) => (text || '')
  .replace(DECK_LINK, (_m, _id, label) => (label || 'Cards').trim())
  .replace(WIKI_LINK, (_m, _id, _section, idLabel, label) => (idLabel || label || '').trim())""")

s = s.replace("""export const plainWikiText = (text) => (text || '').replace(
  WIKI_LINK,
  (_m, _id, _section, idLabel, label) => (idLabel || label || '').trim(),
)""",
"""export const plainWikiText = (text) => (text || '')
  .replace(DECK_LINK, (_m, _id, label) => (label || 'Cards').trim())
  .replace(WIKI_LINK, (_m, _id, _section, idLabel, label) => (idLabel || label || '').trim())""")
open(p, 'w').write(s)
print('written')
