Skip to content
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

How to implement an inline parser extension #67

Closed
IlyaGulya opened this issue Sep 25, 2018 · 2 comments
Closed

How to implement an inline parser extension #67

IlyaGulya opened this issue Sep 25, 2018 · 2 comments

Comments

@IlyaGulya
Copy link

IlyaGulya commented Sep 25, 2018

Hello!
I'm trying to implement parsing of Special GitLab References
Specifically those ones, where only starting delimiter exists: ~bug ~"Multiple word fea"
How can i do this?
I've already tried DelimiterProcessor but it seems not to support such cases.
Also, i've tried to implement custom BlockParser but it looks like it's consuming the whole line and does not give default parsers ability to do their work.
Seems that it is because of this code inside DocumentParser:

while (tryBlockStarts) {
...
tryBlockStarts = newBlockParser.isContainer();

Does it mean that i can not use BlockParser for this purpose?
Is there any way to implement InlineParser extension?

As I can see, currently library gives me two ways:

  1. Implement own InlineParser. This way seems like an overengineering and too hard for such small feature.
  2. Do not extend parsing at all and iterate over AST to find Text nodes and replace necessary parts with custom nodes. This way seems a bit hacky.

Same question in commonmark-java repo

@noties
Copy link
Owner

noties commented Sep 27, 2018

Hello @IlyaGulya !

Unfortunately I do not see a native way to implement what you have in mind due to somewhat limited nature of InlineParser in commonmark-java. There is an issue that have this concern addressed: #113. But it's still open and I'm not sure if someone is working on it.

But this functionality certainly can be achieved. In sample-custom-extension I have demonstrated how this could be done. In short:

  • pre-proccess raw markdown
    • find desired components and wrap then with delimiters (from start and at the end)
  • register custom DelimiterProcessor that will handle you components
  • override visitor to handle CustomNode and apply a span

I know it might be not optimal solution but so far I know no other ways. But let's see how issue in commonmark-java will change, maybe it will be super trivial soon.

@IlyaGulya
Copy link
Author

@noties
Thank you for the answer. Then I will use the approach you've just suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants