Skip to content

Proper regex when nesting elements? #3416

Answered by ivanjaros
ivanjaros asked this question in Q&A
Discussion options

You must be logged in to vote

I think i figured it out. Not sure if this is the "right" way to do it but it is working:

const tabs = {
  name: 'tabs',
  level: 'block',
  start(src) { return src.match(/^:{3,}tabs\n/)?.index; },
  tokenizer(src, tokens) {
    let count = 0
    // because this is a block token, we will actually receive "\n" as first character.
    for (let i = 0; i < src.length; i++) {
      if (src.charAt(i) === ':') {
        count++
        continue;
      }
      if (count > 0) {
        break
      }
    }

    if (count === 0) {
      return
    }

    const pattern = `^:{${count}}tabs\\n([\\s\\S]*?)\\n:{${count}}`;
    const rule = new RegExp(pattern)
    const match = rule.exec(src);

    if (match

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ivanjaros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant