function escapeLeadingChars(text) {
  return text.replace(/^\s*([-#*>=|]| {4,}|`{3,})/, '$`\\$1');
}
escapeLeadingChars

Handles escaping for characters that must be positioned at the beginning of
the string, such as headers and list items.

Escapes '#', '*', '-', '>', '=', '|', and sequences of 3+ backticks or 4+
spaces when found at the beginning of a string, preceded by zero or more
whitespace characters.