Question: Two characters for parsing (move cursor backward?) #646
-
I apologize if this is covered somewhere, haven't seen it. I'm working on a custom extension. The pattern is: I'm using the footnote extension as reference for implementation, specifically the // FootnoteRefParser.php
public function getCharacters(): array
{
return ['['];
}
public function parse(InlineParserContext $inlineContext): bool
{
$container = $inlineContext->getContainer();
$cursor = $inlineContext->getCursor();
$nextChar = $cursor->peek();
if ($nextChar !== '^') {
return false;
}
...
}
// Should translate to something like
public function getCharacters(): array
{
return ['['];
}
public function parse(InlineParserContext $inlineContext): bool
{
$container = $inlineContext->getContainer();
$nextChar = $cursor->peek();
if ($nextChar !== '.') {
return false;
}
...
} This isn't working, the parser for my extension is not being called, apparently. Question
When I do the following, it kind of works, but because I don't know how (or if possible) to move the cursor back one character, I'm not able to complete the implementation (at least not with current understanding). public function getCharacters(): array
{
return ['.'];
}
public function parse(InlineParserContext $inlineContext): bool
{
$container = $inlineContext->getContainer();
$nextChar = $cursor->peek(-1);
if ($nextChar !== '[') {
return false;
}
$previousCursor = $cursor->saveState();
// This is where I'd like to move the cursor back one
$regex = '/^\[\..+?\]\(.+?\)/';
$abbr = $cursor->match($regex);
if (empty($abbr)) {
$cursor->restoreState($previousCursor);
return false;
}
...
} When I let this go and finish thing, I'm left with the starting square bracket |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This suggests that a different parser with a higher priority (or with the same priority) is running before your parser and basically returning For example, Try adding your parser to the environment with a higher priority so it is checked sooner.
The purpose of
Each string in the array must be a single character. #492 (comment) talks a little more about some of the limitations with this approach and new ideas that I'd like to try implementing in 2.0.
No, unfortunately not :-/ You can't advance the cursor backward, but you can |
Beta Was this translation helpful? Give feedback.
-
Thanks, you're awesome! Here's the project: https://github.com/8fold/commonmark-abbreviations That's where priority goes. I saw it mentioned in the docs but didn't quite understand where and whether it was higher or lower number. |
Beta Was this translation helpful? Give feedback.
This suggests that a different parser with a higher priority (or with the same priority) is running before your parser and basically returning
true
to say that "I handled this, nobody else needs to."For example,
FootnoteRefParser
is added with a priority of51
:commonmark/src/Extension/Footnote/FootnoteExtension.php
Line 41 in 09e9079
Try adding your parser to the environment with a higher priority so it is checked sooner.
The purpose of
getCharacters()
is to provide a performance op…