-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Custom extension to support new syntax #2125
Comments
On my initial skim through, your extension looks fine, but I'd have to dig deeper to see what's going on. What's the output from the Lexer if you do this?
|
The issue may be in your Second, I would recommend making your |
@haayhappen Were you able to resolve this issue? |
Sorry about my absence @calculuschild I wasn't able to resolve it and had to move on to use a different approach. However I highly appreciate your help and think this could also be helpful to others. |
I'm implementing a custom extension to do highlighting with the mark element and I'm seeing the same issue. The markdown will look like: const extension = {
name: 'mark',
level: 'inline',
start(src) {
return src.match(/==(?!\s)/)?.index;
},
tokenizer(src) {
const rule = /==(?!\s)([^\n]+)(?!\s)==/;
const match = rule.exec(src);
if (match) {
console.log(src, match) // This shows me multiple matches.
return {
type: 'mark',
raw: match[0],
inner: this.lexer.inlineTokens(match[1].trim())
};
}
},
renderer(token) {
return `<mark>${this.parser.parseInline(token.inner)}</mark>`;
}
}; Example logs:
So it looks like the start index of the source string is shifting, but feeding some of the same characters to the extension multiple times. |
@qubyte I would suggest changing
|
Nice. That works! I'd have never figured that out on my own. Thanks! 😅 |
It does make me a little confused about what the |
The There are many cases when the string not be aligned with the start of your token. By default, all user extensions are given priority over standard Markdown syntax, and the lexer will first run your tokenizer attempting to match if I don't know if I've explained it well but feel free to ask if you have questions. |
Thank you for the detailed explanation! You've also answered a couple of other questions I had before I'd formed them (mainly about priority). My little extension has other issues (the regex needs a bit of tweaking), but you've given me everything I need to finish building it. Thanks again! |
In case it's helpful to anyone else, the solution I ended up with eschewed a regular expression for the tokenizer because the regular expression was coming out too complex for me to reliably maintain. I still used one for the start function though: const extension = {
name: 'mark',
level: 'inline',
start(src) {
return src.match(/==(?!\s)/)?.index;
},
tokenizer(src) {
if (!src.startsWith('==')) {
return;
}
const nextIndex = src.indexOf('==', 2);
if (nextIndex !== -1) {
return {
type: 'mark',
raw: src.slice(0, nextIndex + 2),
inner: this.lexer.inlineTokens(src.slice(2, nextIndex))
};
}
},
renderer(token) {
return `<mark>${this.parser.parseInline(token.inner)}</mark>`;
}
}; |
Marked version:
Describe the bug
I'm trying to use the custom extension of a tokenizer in order to add support for a custom syntax: {{variable}}
In the end this should render as some custom html.
The tokenizer seems to loop over the text far to often and therefore the results are multiple parsed html blocks with the same context.
To Reproduce
Here is my code for the extension:
https://gist.github.com/haayhappen/57b7c77151165584e810f036d536c3aa
Input:
Marked Output:
Expected Output:
Expected behavior
I expect the custom syntax to be evaluated only once.
Did I misuse the extension? Any help highly appreciated!
Thanks
The text was updated successfully, but these errors were encountered: