Skip to content

Adds wrapAttrs option#59

Open
plakak wants to merge 1 commit intopadolsey:masterfrom
plakak:wrap-attrs-option
Open

Adds wrapAttrs option#59
plakak wants to merge 1 commit intopadolsey:masterfrom
plakak:wrap-attrs-option

Conversation

@plakak
Copy link
Copy Markdown

@plakak plakak commented Apr 28, 2017

I could really use this functionality and I think it's a good idea to include it as a standard option for everyone to use.

P.S. Amazing work with this library. Lifesaver for me. Thanks!

@hftf
Copy link
Copy Markdown

hftf commented Oct 27, 2017

I suppose I would use this wrapAttrs option if I could pass in an object of functions:

findAndReplaceDOMText(..., {
    find: /.../,
    wrap: 'mark',
    wrapAttrs: {
        'class': 'match',
        'data-index': function(portion, match) { return match.index; }
    },
    ...
}

But then again, I'm using a replace function anyway:

function replace(portion, match) {
    var html = '<mark class="match" data-index="' + match.index + '">' + 
                   portion.text +
               '</mark>';
    return htmlToElement(html);
}
function htmlToElement(html) {
    var template = document.createElement('template');
    template.innerHTML = html;
    return template.content.firstChild;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants