Create a custom mode

I tried with:

CodeMirror.defineMode('polar', function () {
  return {
    token: function (stream, state) {
      if (stream.match('### ')) {
        stream.skipToEnd()
        return 'header-3'
      }

      if (stream.match('## ')) {
        stream.skipToEnd()
        return 'header-2'
      }

      if (stream.match('# ')) {
        stream.skipToEnd()
        return 'header-1'
      }

      if (stream.match('#')) {
        if (stream.skipTo('#')) {
          stream.next()
          return 'tag'
        }

        if (!stream.eatWhile(/[\w/]/)) {
          return null
        }

        return 'tag'
      }

      if (stream.match('*')) {
        if (!stream.skipTo('*')) {
          return null
        }

        stream.next()
        return 'bold'
      }

      if (stream.match('/')) {
        if (!stream.skipTo('/')) {
          return null
        }

        stream.next()
        return 'italic'
      }

      if (stream.match('_')) {
        if (!stream.skipTo('_')) {
          return null
        }

        stream.next()
        return 'underline'
      }

      if (stream.match('-')) {
        if (!stream.skipTo('-')) {
          return null
        }

        stream.next()
        return 'strikethrough'
      }

      if (stream.match('::')) {
        if (!stream.skipTo('::')) {
          return null
        }

        stream.next()
        stream.next()
        return 'marker'
      }

      stream.skipToEnd()
      return null
    }
  }
})

But this way it only matches if we’re in the beginning of a line.