Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 402
Venupriya Avatar asked Oct 29 '22 20:10

Venupriya


1 Answers

Maybe not the direct answer but related. Here is the solutions to keep attributes for image when initializing from full text html.

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 the whitelist for attributes with solution2.

like image 177
lingceng Avatar answered Nov 09 '22 08:11

lingceng