LoginSignup
1
1

More than 3 years have passed since last update.

tiptap editor ペースト時にタグを削除

Posted at

tiptapエディタを使うときにペーストする。
その際ゴミタグまでペーストされてしまう。
そんなとき、ゴミタグは削除してペーストする方法。

editor: new Editor({

editorProps: {

    //ペースト時に br タグと p タグは許可。それ以外の画像とかは削除する
    transformPastedHTML(str){

    var arrowTag = ['br', 'p'];

    // // 配列形式の場合は'|'で結合
    if ((Array.isArray ?
    Array.isArray(arrowTag)
    : Object.prototype.toString.call(arrowTag) === '[object Array]')
    ) {
    arrowTag = arrowTag.join('|');
    }

    // arrowTag が空の場合は全てのHTMLタグを除去する
    arrowTag = arrowTag ? arrowTag : '';

    // パターンを動的に生成
    var pattern = new RegExp('(?!<\\/?(' + arrowTag + ')(>|\\s[^>]*>))<("[^"]*"|\\\'[^\\\']*\\\'|[^\\\'">])*>', 'gim');
    return str.replace(pattern, '');
},

ちなみに改行brタグを利用するときは


import HardBreak from '../assets/HardBreak'

も必要なので読み込んでおく。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1