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

External links target blank example #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This can be used for several things in a build process. Some examples:
* [Remove whitespace](https://github.com/trygve-lie/gulp-dom/tree/master/examples/remove-whitespace) in the document in a safe way.
* [Replace script / css references](https://github.com/trygve-lie/gulp-dom/tree/master/examples/replace-script-tags) with a new reference (to ex a minified version).
* [Web scraping.](https://github.com/trygve-lie/gulp-dom/tree/master/examples/web-scrape) Take a document from a URL and transform it or extract parts of it during build.
* [Make external links target _blank](https://github.com/trygve-lie/gulp-dom/tree/master/examples/external-links-target-blank)



Expand Down
24 changes: 24 additions & 0 deletions examples/external-links-target-blank/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* jshint node: true, strict: true */

"use strict";

var gulp = require('gulp'),
dom = require('../../');

// Gulp task which will make external links target _blank

gulp.task('external-links-target-blank', function() {
return gulp.src('src/example.html')
.pipe(dom(function() {
var links = this.querySelectorAll('a');
for (var i = 0; i < links.length; i++) {
if (links[i].getAttribute('href').match(/^https?:\/\//)) {
links[i].setAttribute('target', '_blank');
}
}
return this;
}))
.pipe(gulp.dest('./build'));
});

gulp.task('default', ['external-links-target-blank']);
13 changes: 13 additions & 0 deletions examples/external-links-target-blank/src/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Example of making external links target _blank</title>
</head>
<body>
<a href="http://google.com">Google</a>
<a href="https://msn.com">Msn</a>
<a href="internal">Internal Link 1</a>
<a href="/internal">Internal Link 2</a>
</body>
</html>