You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to extend CommonMark, just for simple things like auto-linking ticket numbers and user names. I found the Twitter handle example, and it's conceptually very similar to what i need to do — just look for a special character, make sure the following characters meet some very simple requirements, and then replace the text by a hyperlink.
Unfortunately the example is very naïve. For one thing, the parser will try to link handles that occur inside of hyperlinks:
This is [a @twitter handle](/test) inside a hyperlink.
becomes
<p>This is <ahref="/test">a <ahref="https://twitter.com/twitter">@twitter</a> handle</a> inside a hyperlink.</p>
Additionally, because the parser requires the @ character to be preceded by a space, Twitter handles won't be linked inside of another in-line element if it's the first character (e.g., *@twitter* won't work, but *foo @twitter bar* will).
Because i based my own parsers on this example, they have all of the same limitations. :/
When i first saw the getContainer() method in the docs my immediate thought was that maybe you could use it to prevent issues like this, like your TwitterHandleParser could call getContainer() to see whether it's inside of a link or emphasis or whatever and act accordingly. But i guess it's not like that — with the way parsers work it doesn't seem there's any way to know what the immediate container of a character is, just the nearest block container. Is that right?
I was looking through the code trying to figure out a solution and i noticed that emphasis is handled differently from most other in-line elements, in that it uses DelimiterStack. I haven't so far been able to figure out exactly what it does for you though. Can it provide more robust behaviour in situations like these? If so, can you expand on how it works? If not, do you know how else one might solve the problem?
questionGeneral questions about the project or usage
1 participant
Converted from issue
This discussion was converted from issue #257 on June 12, 2021 14:08.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello!
I'm trying to extend CommonMark, just for simple things like auto-linking ticket numbers and user names. I found the Twitter handle example, and it's conceptually very similar to what i need to do — just look for a special character, make sure the following characters meet some very simple requirements, and then replace the text by a hyperlink.
Unfortunately the example is very naïve. For one thing, the parser will try to link handles that occur inside of hyperlinks:
becomes
Additionally, because the parser requires the
@
character to be preceded by a space, Twitter handles won't be linked inside of another in-line element if it's the first character (e.g.,*@twitter*
won't work, but*foo @twitter bar*
will).Because i based my own parsers on this example, they have all of the same limitations. :/
When i first saw the
getContainer()
method in the docs my immediate thought was that maybe you could use it to prevent issues like this, like yourTwitterHandleParser
could callgetContainer()
to see whether it's inside of a link or emphasis or whatever and act accordingly. But i guess it's not like that — with the way parsers work it doesn't seem there's any way to know what the immediate container of a character is, just the nearest block container. Is that right?I was looking through the code trying to figure out a solution and i noticed that emphasis is handled differently from most other in-line elements, in that it uses
DelimiterStack
. I haven't so far been able to figure out exactly what it does for you though. Can it provide more robust behaviour in situations like these? If so, can you expand on how it works? If not, do you know how else one might solve the problem?Thank you!
Beta Was this translation helpful? Give feedback.
All reactions