How to set the math font to the current surrounding text font?

I have simple inline equations that I want to type

  • in normal text blocks (in the document’s regular serif font)
  • in section headings (in my case, using a bold sans font)
  • in figure captions (in my case, using a normal weight sans font)

Whenever I type foo $y = a x + b$ bar, the math gets typeset in New Computer Modern Math. I can change this with a show rule, but I don’t know how to check the surrounding text’s font:

#set page(width: auto, height: auto, margin: 1em)

#show math.equation: it => context {
  if text.font == "new computer modern math" {
    text(fill: blue, it)
  } else {
    text(fill: red, it)
  }
}

foo $y = a x + b$ bar

#set text(font: "Papyrus")
foo $y = a x + b$ bar

Hi there!
You can stack show rules and overwrite the “standard” show rule for math equations (here: green):

#show math.equation: set text(fill: green)

#show heading: it => {
  show math.equation: set text(fill: blue)
  it
}

#show figure.caption: it => {
  show math.equation: set text(fill: red)
  it
}

= foo $y = a x + b$ bar

foo $y = a x + b$ bar

#figure(
  rect(width: 50%, height: 2em),
  caption: [foo $y = a x + b$ bar]
)