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

Wrong parsing of emphasis and strong emphasis formatting #82

Open
susnux opened this issue Jul 11, 2022 · 2 comments
Open

Wrong parsing of emphasis and strong emphasis formatting #82

susnux opened this issue Jul 11, 2022 · 2 comments

Comments

@susnux
Copy link
Contributor

susnux commented Jul 11, 2022

From CommonMark example 431:

**foo *bar **baz**
bim* bop**

should be parsed as:

<p><strong>foo <em>bar <strong>baz</strong>
bim</em> bop</strong></p>

Using just markdown-it this works as expected but the defaultMarkdownParser creates this incorrect representation:

<p><strong>foo </strong><em><strong>bar baz</strong>
bim</em> bop</p>

Invalid parsing of emphasis and strong emphasis

@marijnh
Copy link
Member

marijnh commented Jul 11, 2022

ProseMirror does not allow marks of the same type to be nested like this. I guess the markdown parser could be a bit more graceful about this (just ignoring the inner opening and closing strong tokens).

@susnux
Copy link
Contributor Author

susnux commented Jul 12, 2022

ProseMirror does not allow marks of the same type to be nested like this.

Well one could workaround this as ProseMirror supports multiple marks of the same type if they have different attributes. Meaning you have to set excludes: '' for the marks em and strong and add a nesting attribute to them.

e.g. something like this:

function nesting(...types) {
  const fn = (node) => {
    let el = (node as HTMLElement).parentElement
    let nesting = 0
    while(el !== null) {
      if ([...arguments].includes(el.tagName)) nesting++
      el = el.parentElement
    }
    return {nesting: nesting}
  }
  return fn
}

// ... inside the schema:
marks: {
    em: {
      attrs: {
        nesting: {
          default: null
        }
      },
      parseDOM: [
        {tag: "i", getAttrs: nesting("EM", "I")},
        {tag: "em", getAttrs: nesting("EM", "I")},
      ],
      toDOM() { return ["em"] },
      excludes: ''
    },

    strong: {
      excludes: '',
      attrs: {
        nesting: {
          default: null
        }
      },
      parseDOM: [
        {tag: "b", getAttrs: nesting("STRONG", "B")},
        {tag: "strong", getAttrs: nesting("STRONG", "B")},
      ],
      toDOM() { return ["strong"] }
    },
}

susnux added a commit to nextcloud/text that referenced this issue Oct 28, 2022
As two different markdown strings can be semantically the same,
we can test if the resulting rendered HTML are syntactically equal.

Currently about hundred test cases fail because of a known limitation of
`prosemirror-markdown` not able to serialize nested marks correctly, see
ProseMirror/prosemirror-markdown#82

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
susnux added a commit to nextcloud/text that referenced this issue Nov 17, 2022
As two different markdown strings can be semantically the same,
we can test if the resulting rendered HTML are syntactically equal.

Currently about hundred test cases fail because of a known limitation of
`prosemirror-markdown` not able to serialize nested marks correctly, see
ProseMirror/prosemirror-markdown#82

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
juliushaertl pushed a commit to nextcloud/text that referenced this issue Nov 30, 2023
As two different markdown strings can be semantically the same,
we can test if the resulting rendered HTML are syntactically equal.

Currently about hundred test cases fail because of a known limitation of
`prosemirror-markdown` not able to serialize nested marks correctly, see
ProseMirror/prosemirror-markdown#82

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
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