-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A variant of cleanTags which retains child elements #1141
Comments
@paradite apologies no one responded to you sooner, I think this feature makes a lot of sense and you should definitely open a PR! Some other ways to think about implementing it: option 1
// options
cleanTags: ['meta'],
unwrapTags: ['sub', 'sup']
cleanupTags: function (el, tags) {
tags.forEach(function (tag) {
if (el.nodeName.toLowerCase() === tag) {
el.parentNode.removeChild(el);
}
});
},
unwrapTags: function(el, tags) {
tags.forEach(function (tag) {
if (el.nodeName.toLowerCase() === tag) {
MediumEditor.util.unwrap(el, document);
}
});
}, option 2
cleanTags: ['meta'],
unwrapTags: ['sub', 'sup'],
cleanupTags: function (el, tags, unwrap, doc) {
if (tags.indexOf(el.nodeName.toLowerCase()) !== -1) {
if (unwrap && unwrap.indexOf(el.nodeName.toLowerCase() !== -1) {
MediumEditor.util.unwrap(el, doc);
} else {
el.parentNode.removeChild(el);
}
}
} option 3have cleanTags: ['meta', { tag: 'sub', unwrap: true }, { tag: 'sup', unwrap: true }]
cleanupTags: function (el, tags, doc) {
tags.forEach(function (tag) {
var tagName = tag.tag || tag;
var unwrap = tag.unwrap;
if (el.nodeName.toLowerCase() === tagName) {
if (tag.unwrap) {
MediumEditor.util.unwrap(el, doc);
} else {
el.parentNode.removeChild(el);
}
}
});
} Regardless of what you want to do, keep in mind that functions on Also, if you go with option 1 or option 2, take the opportunity to make the function more efficient and do Thanks and happy coding! |
Thanks for the reply. I will be working a PR in the next few days. |
Description
Currently medium-editor supports
cleanTags
, in which the elements with the tags will be removed, along with their children.I am proposing a variant of it,
unwrapTags
(or any other better name), which would remove the element with the tags but retain the child elements.So for example, I have
cleanTags: ['meta', 'sup']
, for the following html:cleanTags now gives:
unwrapTags would give:
medium-editor
already has a util functionunwrap
which can do this internally.Here is my implementation of
unwrapTags
along with the original cleanupTags:Let me know if you find this feature useful enough to be included in medium-editor.
The text was updated successfully, but these errors were encountered: