markdoc icon indicating copy to clipboard operation
markdoc copied to clipboard

Tag is ignored when double quote is missing in tag attribute

Open StarpTech opened this issue 3 years ago • 4 comments

What happened?

This was quite hard to debug. I had the following:

{% quote content="test /%}

No error was thrown. I'd expect that the parser throws an error.

To reproduce

See above.

Version

0.1.3

Additional context

No response

StarpTech avatar Jul 03 '22 17:07 StarpTech

(Originally posted this on https://github.com/markdoc/markdoc/pull/160, reproducing here for completeness)

The underlying problem here is with the way that Markdoc identifies the end of a tag construct in content. When it sees a {% in the text, it scans forward each character until it finds a matching %} that is not inside of a string. It does this in order to avoid prematurely identifying the tag end when it encounters tag-like syntax inside of a string attribute value. This means that if a tag has an unterminated string, the parser assumes that the actual closing is still inside of that string and it declines to match it as a tag.

What we should probably do is solve this in the markdown-it plugin and make the parser generate an error for the whole block any time it sees the tag start syntax without a matched end. I think this would be cleaner than trying to identify malformed tags during the validation process. I will tackle fixing this myself at some point in the next week or two.

For now, if you want to be able to catch these cases reliably during validation without a large surface area of potential false positives, I would suggest overriding the text schema node and doing the validation check there. Here's a quick-and-dirty example of something you can include in your Markdoc config to achieve this:

export const text: Schema = {
  attributes: {
    content: { type: String, required: true },
  },
  transform(node) {
    return node.attributes.content;
  },
  validate(node: Node, config: Config) {
    if (node.attributes?.content?.match?.(/{%[^%}]+%}/)) {
      return [{
        id: 'attribute-value-invalid',
        level: 'error',
        message:
          'The string attribute must have an opening and closing quote',
      }]

    return [];
  }

Thanks for raising this issue!

rpaul-stripe avatar Sep 20 '22 15:09 rpaul-stripe

What happened?

This was quite hard to debug. I had the following:

{% quote content="test /%}

No error was thrown. I'd expect that the parser throws an error.

To reproduce

See above.

Version

0.1.3

Additional context

No response

Test

urica12 avatar Nov 29 '22 03:11 urica12

What happened?

This was quite hard to debug. I had the following:

{% quote content="test /%}

No error was thrown. I'd expect that the parser throws an error.

To reproduce

See above.

Version

0.1.3

Additional context

No response

Test

Test

urica12 avatar Nov 29 '22 03:11 urica12

What happened?

This was quite hard to debug. I had the following:

{% quote content="test /%}

No error was thrown. I'd expect that the parser throws an error.

To reproduce

See above.

Version

0.1.3

Additional context

No response

Test

Test

{% quote content="test /%}

urica12 avatar Nov 29 '22 03:11 urica12