Replies: 5 comments 5 replies
-
@ivanjaros How did you wind up solving this? |
Beta Was this translation helpful? Give feedback.
-
This works with inline tokens(like image above), because it follows the markdown token and they are separate tokens, but it will NOT work with block tokens because their regex won't match the opening line. So you would have to write your own block tokens with attributes in mind, extract them and then call the original renderers. edit: to alter the rendered html, you have to override the original renderer, or change the token to your own and essentially hijack it altogether or alter it to html token. edit2: in the end i ended up only using lexer because i do my own rendering in Vue. |
Beta Was this translation helpful? Give feedback.
-
Or if you want to do it the same way as markdown-it-attrs you will have to override the tokenizers for every token and add the attributes to the token. |
Beta Was this translation helpful? Give feedback.
-
Here's my complete solution:
Any feedback is most welcome. Once I've used this in production for a bit and ironed out any wrinkles that arise, I'll probably publish it as a Node package. |
Beta Was this translation helpful? Give feedback.
-
The Homebrewery has had an extension that basically does this for a few years now. One extension for block tokens and another for inline tokens. Supports appending HTML attributes, inline styles, or CSS classes. Looks something like this:
Probably needs a refactoring update now that Marked has released new hooks, but might be worth looking into how the extension is written there for inspiration. Look for the "mustacheInjectInline" and "mustacheInjectBlock" extensions in this file: https://github.com/naturalcrit/homebrewery/blob/master/shared%2Fnaturalcrit%2Fmarkdown.js |
Beta Was this translation helpful? Give feedback.
-
I'm trying to implement markdown attributes (analogous to
markdown-it-attrs
).I've already updated the tokens so that they have an additional
attributes
property. For example, this markdown:...includes a token like this:
Now I'd like to postprocess the HTML from each token after its rendered, adding the attributes to the HTML. The token above should output:
Because attributes can be added to any element, I'd like to use a generic parser/renderer that isn't restricted to a specific token type. Otherwise I'd have to overwrite all of the existing renderers.
Ideally I'd like a hook called
postprocessAllTokens
or something similar, but that functionality doesn't exist, and I can't find a workaround.I've already looked at #3408, but that solution is also missing the final step I'm looking for, of turning the tokens into HTML.
Beta Was this translation helpful? Give feedback.
All reactions