Questions regarding extensions #2373
-
Let's say I want to match the following text and turn into a token: ```js --someFlag
// Javascript code
``` Easy enough, I've already got the required regex that does the job, so the extension looks like this: {
name: "code-with-flags",
level: "block",
start: (src) => src.indexOf("```"),
tokenizer: function (src, tokens) {
const full = src.match(/```(?<lang>[a-z]*) --(?<flagName>.+)\n(?<content>[\s\S]*?)\n```/);
if (!full || !full[0]) return;
return {
type: "code-with-flags",
raw: full[0],
data: { lang: full.groups.lang, flag: full.groups.flagName, content: full.groups.content }
}
},
renderer: function(token) {
return "abc";
}
} But... this doesn't work. The extensions above seems to be eating tokens before it, I did a bit of digging in the source code of this library, and found this line: src = src.substring(token.raw.length); ... so if I have the following markdown: # Hello World
This is some text.
```js --someFlag
// Javascript code
``` what I would actually get is: (or something similar) abc --someFlag
// Javascript code
``` How do I go around this problem? I read the documentation about extensions, and I still cannot figure it out. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You want the |
Beta Was this translation helpful? Give feedback.
You want the
src.match
to only match the beginning of the string so you have to put a^
at the beginning of the regex.