Skip to content

How to implement attributes? #3408

Closed Answered by ivanjaros
ivanjaros asked this question in Q&A
Aug 12, 2024 · 4 comments · 3 replies
Discussion options

You must be logged in to vote

Ok, I have figured it out. This is simple extension that just looks for attributes pattern following certain tokens. In this case it hooks into image so it won't trigger if preceding token is text or table or code.

import parseAttrs from 'attributes-parser'

const attributes = {
  name: 'attributes',
  level: 'inline',
  start(src) { return src.match(/^\{.*\}/)?.index; },
  tokenizer(src, tokens) {
    const match = src.match(/^\{(.*)\}/);
    if (match && tokens.length > 0) {
      let last = tokens[tokens.length-1]
      if (['image'].includes(last.type)) {
        last.attributes = parseAttrs(match[1])
        return {
          type: 'attributes',
          raw: match[0],
        };
 …

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@ivanjaros
Comment options

@calculuschild
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ivanjaros
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by ivanjaros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants