Skip to content
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

Add targetBlank option #1371

Closed
wants to merge 1 commit into from
Closed

Add targetBlank option #1371

wants to merge 1 commit into from

Conversation

Tarocch1
Copy link

@Tarocch1 Tarocch1 commented Oct 30, 2018

Description

Add targetBlank option. If true, open all links in new windows. Default is false.

marked.setOptions({
  targetBlank: true
});
marked('[test](/url/)');

Result

<p><a href="/url/" target="_blank">test</a></p>

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

  • Draft GitHub release notes have been updated.
  • CI is green (no forced merge required).
  • Merge PR

@styfle
Copy link
Member

styfle commented Oct 30, 2018

Hi @Tarocch1 thanks for the PR

We are actually trying to avoid adding new options to marked if it can be achieved with a sanitizer.

This PR overlooks the fact that the user may want to add a rel attribute or any number of sanitization techniques.

This feature can be achieved using sanitize-html like so:

       const sanitize = require('sanitize-html');
       const cleanHtml = sanitize(dirtyHtml, {
            allowedTags: [ 'a', 'b', 'p', 'i',  'em' ],
            allowedAttributes: {
                'a': ['href', 'target', 'rel'],
            },
            transformTags: {
                'a': (tagName, attribs) => {
                    if (attribs) {
                        attribs.target = '_blank';
                        attribs.rel = 'noopener noreferrer';
                    }
                    return { tagName, attribs };
                }
            },
        });

We actually have a proposal to remove sanitization in #1232

@UziTech
Copy link
Member

UziTech commented Oct 30, 2018

This has already been proposed many times. As @styfle mentioned we are trying to avoid adding new options to prevent feature creep.

You can extend marked's renderer to add attributes to the a tag:

var renderer = new marked.Renderer();
renderer.link = function(href, title, text) {
    var link = marked.Renderer.prototype.link.apply(this, arguments);
    return link.replace("<a","<a target='_blank'");
};

marked.setOptions({
    renderer: renderer
});

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.

3 participants