How to add image attributes to Quill editor? I want to add the attribute 'alt' and 'title'

When I upload an image, only its 'src' is saved. I want to add alternate text and title for the purpose of SEO. I tried to find the module in the Quill documentation, but could not find.

+4
source share
1 answer

Maybe not a direct answer, but a related one. The following are solutions for saving image attributes when initializing from the full html text.

Solution1:

class ImageBlot extends Image {
  static create(value) {
    if (typeof value == 'string') {
      return super.create(value);
    } else {
      return value;
    }
  }

  static value(domNode) {
    return domNode;
  }
}
Quill.register(ImageBlot);

Solution2:

class ImageBlot extends Image {
  static get ATTRIBUTES() {
    return [ 'alt', 'height', 'width', 'class', 'data-original', 'data-width', 'data-height', 'style-data' ]
  }

  static formats(domNode) {
    return this.ATTRIBUTES.reduce(function(formats, attribute) {
      if (domNode.hasAttribute(attribute)) {
        formats[attribute] = domNode.getAttribute(attribute);
      }
      return formats;
    }, {});
  }

  format(name, value) {
    if (this.constructor.ATTRIBUTES.indexOf(name) > -1) {
      if (value) {
        this.domNode.setAttribute(name, value);
      } else {
        this.domNode.removeAttribute(name);
      }
    } else {
      super.format(name, value);
    }
  }
}
Quill.register(ImageBlot);

You can specify a whitelist of attributes using solution2.

0
source

Source: https://habr.com/ru/post/1655080/


All Articles