Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Created August 22, 2019 21:12
Show Gist options
  • Save dphiffer/8dfa555d342e56f3cf8a09f351aa0bfb to your computer and use it in GitHub Desktop.
Save dphiffer/8dfa555d342e56f3cf8a09f351aa0bfb to your computer and use it in GitHub Desktop.
Word → WordPress copy/paste filters
// re: https://github.com/WordPress/gutenberg/issues/16936
//
// n.b., these are pretty heavy-handed, they just strip out the comments & footnotes wholesale,
// and don't attempt to convert the content. Given a chance, I would try to ingest the footnotes
// in a more sophisticated way.
var ms_word_comments = function(node, doc) {
// Only filter on DOM elements
if (! node.getAttribute) {
return;
}
// Filters are all based on 'style' and 'class' attributes
var style = node.getAttribute('style');
var className = node.getAttribute('class');
// Convert "<a style='mso-comment-reference...'>text</a>" => "text"
if (style && style.indexOf('mso-comment-reference') !== -1) {
node.outerHTML = node.innerHTML;
return;
}
// Remove "<span class=MsoCommentReference>...</span>"
if (className && className.indexOf('MsoCommentReference') !== -1) {
node.remove();
return;
}
// Remove "<div style='mso-element:comment-list'>...</div>"
if (style && style.indexOf('mso-element:comment-list') !== -1) {
node.remove();
return;
}
// Remove "<a class='msocomanchor'>...</a>"
if (className && className.indexOf('msocomanchor') !== -1) {
node.remove();
return;
}
// Remove "<span style='mso-special-character:comment'>...</span>"
if (style && style.indexOf('mso-special-character:comment') !== -1) {
node.remove();
return;
}
};
var ms_word_footnotes = function(node, doc) {
// Only filter on DOM elements
if (! node.getAttribute) {
return;
}
// Filters are all based on 'style' attributes
var style = node.getAttribute('style');
// Remove "<a style='mso-footnote-id...'>...</a>"
// Note: for now we are just stripping out footnotes
if (style && style.indexOf('mso-footnote-id') !== -1) {
node.remove();
return;
}
// Remove "<div style='mso-element:footnote-list'>...</div>"
// Note: for now we are just stripping out footnotes
if (style && style.indexOf('mso-element:footnote-list') !== -1) {
node.remove();
return;
}
};
// Here is where these would plug in:
// https://github.com/WordPress/WordPress/blob/5.2.2/wp-includes/js/dist/blocks.js#L11063
var filters = [google_docs_uid_remover, ms_list_converter, ms_word_comments, ms_word_footnotes, head_remover, list_reducer, image_corrector, phrasing_content_reducer, special_comment_converter, comment_remover, figure_content_reducer, blockquote_normaliser];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment