parseInlineKatex = (state, silent) => {
    const start = state.pos;
    const max = state.posMax;
    let pos = start;

    // Unexpected starting character
    if (state.src.charAt(pos) !== delimiter) { return false; }

    ++pos;
    while (pos < max && state.src.charAt(pos) === delimiter) { ++pos; }

    // Capture the length of the starting delimiter -- closing one must match in size
    const marker = state.src.slice(start, pos);
    if (marker.length > 2) { return false; }

    const spanStart = pos;
    let escapedDepth = 0;
    while (pos < max) {
      const char = state.src.charAt(pos);
      if (char === '{' && (pos == 0 || state.src.charAt(pos - 1) != backslash)) {
        escapedDepth += 1;
      } else if (char === '}' && (pos == 0 || state.src.charAt(pos - 1) != backslash)) {
        escapedDepth -= 1;
        if (escapedDepth < 0) { return false; }
      } else if (char === delimiter && escapedDepth === 0) {
        const matchStart = pos;
        let matchEnd = pos + 1;
        while (matchEnd < max && state.src.charAt(matchEnd) === delimiter) { ++matchEnd; }

        if (matchEnd - matchStart === marker.length) {
          if (!silent) {
            const content = state.src.slice(spanStart, matchStart)
                .replace(/[ \n]+/g, ' ')
                .trim();
            state.push({type: 'katex', content: content, block: marker.length > 1, level: state.level});
          }
          state.pos = matchEnd;
          return true;
        }
      }
      pos += 1;
    }

    if (!silent) { state.pending += marker; }
    state.pos += marker.length;

    return true;
  }
Look for '$' or '$$' spans in Markdown text. Based off of the 'fenced' parser in remarkable.