简体   繁体   English

从 Word 复制到 Wordpress TinyMCE 时替换 HTML 标签

[英]Replace HTML tags when copying from Word to Wordpress TinyMCE

I'm trying to clean as much as I can the HTML code when copying from Word/LibreOffice to Wordpress TinyMCE editor.从 Word/LibreOffice 复制到 Wordpress TinyMCE 编辑器时,我正在尝试尽可能多地清理 HTML 代码。 I'm using this code (in functions.php), slightly adapted from here :我正在使用这段代码(在functions.php中),稍微改编自这里

add_filter('tiny_mce_before_init','configure_tinymce');

/**
 * Customize TinyMCE's configuration
 *
 * @param   array
 * @return  array
 */
function configure_tinymce($in) {
  $in['paste_preprocess'] = "function(plugin, args){
    // Strip all HTML tags except those we have whitelisted
    var whitelist = 'p,strong,em,i,b,h2,h3,h4,h5,h6,ul,li,ol';
    var stripped = jQuery('<div>' + args.content + '</div>');
    var els = stripped.find('*').not(whitelist);
    for (var i = els.length - 1; i >= 0; i--) {
      var e = els[i];
      jQuery(e).replaceWith(e.innerHTML);
    }
    // Strip all class and id attributes
    stripped.find('*').removeAttr('id').removeAttr('class').removeAttr('align');
    // Return the clean HTML
    args.content = stripped.html();
  }";
  return $in;
}

It works great, but I would also like this function to replace <b></b> and <i></i> tags by <strong></strong> and <em></em> tags.它工作得很好,但我也希望这个 function 用<strong></strong><em></em>标签替换<b></b><i></i>标签。 I've tried to play with str.replace, but since I'm new to Javascript I can't figure out a nice way to replace tags like this.我尝试过使用 str.replace,但由于我是 Javascript 的新手,所以我想不出一个好方法来替换这样的标签。

You can configure TinyMCE to convert <b> to <strong> and <i> to <em> in the TinyMCE configuration so you don't need to write code to do this.您可以在 TinyMCE 配置中配置 TinyMCE 以将<b>转换为<strong>并将<i>转换为<em> ,因此您无需编写代码来执行此操作。 The setting is:设置是:

extended_valid_elements : "strong/b,em/i"

Here is an example in TinyMCE: http://fiddle.tinymce.com/pjhaab这是 TinyMCE 中的一个示例: http://fiddle.tinymce.com/pjhaab

WordPress has hooks for you to include configuration options before TinyMCE is invoked so you can add this instead of trying to pre or post process the content. WordPress 在调用 TinyMCE 之前有钩子供您包含配置选项,因此您可以添加它而不是尝试对内容进行预处理或后处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM