# Create a custom mode

**URL:** <https://discuss.codemirror.net/t/create-a-custom-mode/1334>\
**Category:** Uncategorized\
**Created:** [August 19, 2017, 3:25pm UTC](https://discuss.codemirror.net/t/create-a-custom-mode/1334 "2017-08-19T15:25:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hacdias](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/hacdias/32/386_2.png) [@hacdias](https://discuss.codemirror.net/u/hacdias)\
**Post date:** [August 19, 2017, 3:25pm UTC](https://discuss.codemirror.net/t/create-a-custom-mode/1334/1 "2017-08-19T15:25:01Z")

</div>

Hello 🙂

I’m trying to create a custom mode and I started by using Simple Mode addon. Although, I’ve already noticed some of its limitation and I’d like to know if you could help me.

```auto
CodeMirror.defineSimpleMode('simplemode', {
 // The start state contains the rules that are intially used
 start: [
   {regex: /* .*/, token: 'ul', sol: true},
   {regex: /### .*/, token: 'header-3', sol: true},
   {regex: /## .*/, token: 'header-2', sol: true},
   {regex: /# .*/, token: 'header-1', sol: true},
   {regex: /\[.*]\(.*\)/, token: 'link'},
   {regex: /(#.*#)/, token: 'tag'},
   {regex: /(#[\w/]+)/, token: 'tag'},
   {regex: /(#\w+)/, token: 'tag'},
   {regex: /::.*?(::)/, token: 'marker'},
   {regex: /\*.*?(\*)/, token: 'bold'},
   {regex: /\/.*?(\/)/, token: 'italic'},
   {regex: /_.*?(_)/, token: 'underline'},
   {regex: /-.*?-/, token: 'strikethrough'}
 ]
})

```

The first thing is that `{regex: /\[.*]\(.*\)/, token: 'link'}` is not being highlighted in the following case (and some others): `/test [link](https://go.com)`. I get `/test [link](https:/` highlighted as an italic. How do I make the link thing to be prioritized?

The second thing: I want to nest some of the tokens, like:

```auto
*bold /this italic but still bold/*

```

Which is not working now.

Another thing is with the first rule: `{regex: /* .*/, token: 'ul', sol: true},`. If it matches, nothing else in that sentence is going to be matched…

How should I create a Mode that accomplishes this?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![hacdias](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/hacdias/32/386_2.png) [@hacdias](https://discuss.codemirror.net/u/hacdias)\
**Post date:** [August 19, 2017, 3:28pm UTC](https://discuss.codemirror.net/t/create-a-custom-mode/1334/2 "2017-08-19T15:28:48Z")

</div>

I tried with:

```auto
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.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [August 21, 2017, 7:57am UTC](https://discuss.codemirror.net/t/create-a-custom-mode/1334/3 "2017-08-21T07:57:44Z")

</div>

> [@hacdias](#):
>
> I want to nest some of the tokens, like:

For a markdown-ish language with nesting markup, I’m not sure the simple mode framework will be very useful. You might have more luck writing a mode directly using the mode interface, or wrapping the Markdown mode somehow.

---

<div class="post-metadata">

**Author:** ![hacdias](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/hacdias/32/386_2.png) [@hacdias](https://discuss.codemirror.net/u/hacdias)\
**Post date:** [August 21, 2017, 12:10pm UTC](https://discuss.codemirror.net/t/create-a-custom-mode/1334/4 "2017-08-21T12:10:42Z")

</div>

Thanks 🙂 Already found out how to do this!
