Fix attribute adding to avoid duplicates #157
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Right now, the plugin adds attributes to tokens in a way that causes duplicates if those attributes already exist. This pull request fixes the problem.
For example, suppose someone wants to use this plugin to add the
alt
attribute to an image:markdown-it
already adds thealt
attribute to Markdown image tokens. (For whatever reason, this attribute is empty.) Whenmarkdown-it-attrs
adds thealt
attribute because of the curly braces, it will duplicate the attribute. The attributes of the above token are:Due to the way that
markdown-it
provides attributes when usingtoken.attrGet
, only the first instance of any duplicated attribute will be accessible. In other words, it's as if the plugin has done nothing at all.This pull request makes a single line change—from
token.attrPush
totoken.attrSet
—to fix the issue of duplicate attributes. The latter avoids adding duplicates and instead updates attributes that already exist. See the docs.