npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-replace-image-src-from-data-attr

v1.0.8

Published

replace the "src" attribute with specific path of "img" in "data-local-src" attribute of HTML files (or any attribute you specify)

Downloads

16

Readme

gulp-replace-image-src-from-data-attr Build Status

Replace the "src" attribute of <img> tags with specific path from the "data-local-src" attribute in HTML files (or any attribute you specify).

It is very easy to replace the IMG "src" with an alternative "src" throughout your project HTML file.

Installation

Install package with NPM and add it to your development dependencies:

npm install --save-dev gulp-replace-image-src-from-data-attr

Usage

Example 1:

Replace images and copy the new file to a folder in the root called dist.

var rep = require('gulp-replace-image-src-from-data-attr');
const gulp = require('gulp');

function replace() {

    return gulp
        .src('**/*.html')
        .pipe(rep({
            keepOrigin : false
        }))
        .pipe(gulp.dest('dist/'));

}

exports.replace = replace;
exports.default = replace;

Example 2:

Using the gulp-rename package to create the file in the exising directory and adding -dist to the end of the filename:

var rep = require('gulp-replace-image-src-from-data-attr');
const gulp = require('gulp');
const rename = require('gulp-rename');

function replace() {

    return gulp
        .src('**/*.html')
        .pipe(rep({
            keepOrigin : false
        }))
        .pipe(rename(function(path){
            path.basename += '-dist';
        }))
        .pipe(gulp.dest('.'));
}

exports.replace = replace;
exports.default = replace;

Result

If the original HTML is like this:

<body>
  <div class="anIconClass">
    <img src="/public/iconA.png" data-local-src="images/icon1.png" />
  </div>
  <div class="anotherClass">
    <img src="../iconB.png" data-local-src="images/icon1-1.png" />
  </div>
  <div>
    <img src="http://a.cdn.com/iconC.png" data-local-src="images/icon1-2.png" />
  </div>
</body>

After running it will be like this:

<body>
  <div class="anIconClass">
    <img src="images/icon1.png" />
  </div>
  <div class="anotherClass">
    <img src="images/icon1-1.png" />
  </div>
  <div>
    <img src="http://a.cdn.com/icon1-2.png" />
  </div>
</body>

API

replace(options)

Options, Type: Object.

options.keepOrigin

Type:Boolean

Default:false

If the value is true, and the the "src" starts with "http:|ftp:|https:|//", then the new "src" is prepended to the origin "src".

options.sourceAttr

Type:String

Default:'data-local-src'

For added flexibility you can name any attribute here, whether it's a data- or alt or whatever you fancy. It's your code.

(Shout out to the maker of "gulp-replace-image-src" which was the basis for this tool)