arve0 / markdown-it-attrs

Add classes, identifiers and attributes to your markdown with {} curly brackets, similar to pandoc's header attributes
MIT License
300 stars 58 forks source link

handle no token in exports.addAttrs #119

Closed sajozsattila closed 2 years ago

sajozsattila commented 3 years ago

If I use the bellow chain:

markdownit(defaults)
        .use(markdownitCriticmarkup)
        .use(implicitFigures, {
            figcaption: true,  // <figcaption>alternative text</figcaption>, default: false
            tabindex: true // <figure tabindex="1+n">..., default: false
        })        
        .use(markdownitS2Tex)                
        .use(markdownitFootnote)
        .use(markdownitMultimdTable, {
            rowspan: true,
            multiline:  true
        }) 
        .use(markdownitSub)
        .use(markdownitSup)
        .use(markdownitIns)
        .use(markdownItAttrs, {})

And the below table:

|               |          Grouping             ||         Grouping 2         ||  Not Grouped    |
| First Header  | Second Header | Third Header   | Forth Header | Fifth Header | Sixth Header    |
| ------------- | :-----------: | -------------: | :----------: | :----------: | --------------- |
| Tall Cell     |          *Long Cell*          ||         *Long Long Cell*                    |||
| ^^            |   **Bold**    | 1. first item  | *Italic*     | 3. third item | + first point  |\
| ^^            |               | 1. second item |              | 1. forth item | + second point |

| New section   |     More      |         Data   | ... - -- --- |
| And more      | With an escaped '\|'          || "Quotes in 'quotes'"         |
[Table example]
{#tbl:2}

I have managed to crash the markdown-it-attrs.js. The reason for this in line 687:

exports.addAttrs = function (attrs, token) {
    for (var j = 0, l = attrs.length; j < l; ++j) {
      var key = attrs[j][0];

      if (key === 'class') {
        token.attrJoin('class', attrs[j][1]);
      } else if (key === 'css-module') {
        token.attrJoin('css-module', attrs[j][1]);
      } else {
        token.attrPush(attrs[j]);
      }
    }

  return token;
};

There the 'token' is undefined in this case. A simple solution to make check for this condition:

exports.addAttrs = function (attrs, token) {
  if (token) {
    for (var j = 0, l = attrs.length; j < l; ++j) {
      var key = attrs[j][0];

      if (key === 'class') {
        token.attrJoin('class', attrs[j][1]);
      } else if (key === 'css-module') {
        token.attrJoin('css-module', attrs[j][1]);
      } else {
        token.attrPush(attrs[j]);
      }
    }
  }

  return token;
};
arve0 commented 3 years ago

Hi👋 Thanks for reporting.

arve0 commented 2 years ago

Hi again 🙂 I'll close this issue, with same message as in pull requests:

Do you have a test case, or a stack trace? I might want to fix the issue at the calling site, patterns.js that is.