def md_heading(raw_text, level):
  """Returns markdown-formatted heading."""
  adjusted_level = min(max(level, 0), 6)
  return '%s%s%s' % (
      '#' * adjusted_level, ' ' if adjusted_level > 0 else '', raw_text)