Skip to content

grammar for inline codes is not good #1052

Closed

Description

@icecola2018

Expectation

``foo

foo``

`foo``

``foo`

should be

<p>foo``</p>
<p>``foo</p>
<p>`foo``</p>
<p>``foo`</p>

Result

marked returns

<p><code></code>foo</p>
<p>foo<code></code></p>
<p><code>foo`</code></p>
<p><code></code>foo`</p>

What was attempted

I edited marked.js:

  1. line 471, replace code: /^(`+)(\s*)([\s\S]*?[^`]?)\2\1(?!`)/ with code: /^(`+(?!`))([\s\S]*?[^`])\1(?!`)/
  2. line 694, replace cap[3] with cap[2]

then get

<p>foo``</p>
<p>``foo</p>
<p>`foo``</p>
<p>`<code>foo</code></p>

However it doesn't work well on the last one.

Activity

  1. Feder1co5oave commented on Feb 15, 2018

    @Feder1co5oave
    Contributor

    Thanks for reporting, this was partially my mistake in #1013.

    Babelmark for reference.

    The fix you suggest brings us back before #1013 so it is not right.
    I have an easy partial fix for this which would produce the following output:

    <p>``foo</p>
    <p>foo``</p>
    <p>`foo``</p>
    <p><code>`foo</code></p>

    I'm adding this to the consolidating issue hoping that we'll conform with commonmark on codespans too.

  2. icecola2018 commented on Feb 15, 2018

    @icecola2018
    Author

    Reply to @Feder1co5oave

    I see the problem in #1030 .

    Actually my suggestion

    /^(`+(?!`))([\s\S]*?[^`])\1(?!`)/
    

    is different from the one before #1030

    /^(`+)([\s\S]*?[^`])\1(?!`)/
    

    I found that back references (such as \1) perform strangely when they refer to groups ending with + or *. In fact \1 has chance to match any quantities of `.

    For example, if (`+) matched `` and it was not closed, then \1 would match ` or ``` or ```` or ...
    That's why /^(`+)([\s\S]*?[^`])\1(?!`)/ works well on `` ` `` but wrong on `` ` ```.


    Then I added (?!`) after + to the first group, which seemed no sense. Actually it worked well, \1 will only match the same string that the group matches. You may test it to be sure.


    I'm not very good at English, so I will try my best to explain if you don't understand what I want to say.

  3. added
    L2 - annoying
    on Feb 15, 2018
  4. Feder1co5oave commented on Feb 15, 2018

    @Feder1co5oave
    Contributor

    Thanks, you've been very clear and detailed, I'll look into this!

  5. UziTech commented on Dec 5, 2018

    @UziTech
    Member

    this seems to be fixed in the latest version of marked demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    L2 - annoyingcategory: code blocks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions