-
I have read that when one has a block tag that uses something like so for example: :::foo
normal element
:::
::::foo
:::foo
nested element
:::
::::
but i am having a bit trouble with matching the tags with regex in my extensions. As far as i know, the Later, in the tokenizer, I would have something like: tokenizer(src, tokens) {
const rule = /^:::foo\n([\s\S]*?)\n:::/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'foo',
raw: match[0],
tokens: []
};
this.lexer.blockTokens(match[1], token.tokens);
return token;
}
}, Normally I would end the rule with \n to signal end of the block, but then I would be unable to match the Maybe one thing to note is that in case of blockquote, the pattern is actually: > quote
> > nested
> > quote and it will look like this:
PS: I can add |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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) {
const token = {
type: 'tabs',
raw: match[0],
tokens: []
};
this.lexer.blockTokens(match[1], token.tokens);
token.tokens = token.tokens.filter(t => t.type === 'tab')
return token;
}
},
}; |
Beta Was this translation helpful? Give feedback.
I think i figured it out. Not sure if this is the "right" way to do it but it is working: