1.0.9 • Published 8 years ago

gulp-json-replace v1.0.9

Weekly downloads
26
License
MIT
Repository
github
Last release
8 years ago

gulp-json-replace

Replace string from a json file.

Usage

Assume you wanna replace the title and the description/keywords of the html file: index.html, signin/signin.html, signup/signup.html

<html>
<head>
    <title>%%title</title>
    <meta charset="utf-8" />
    <meta name="description" content="%%description">
    <meta name="keywords" content="%%keywords">
</head>
<body></body>
</html>

And you had the config file that contains all of the pages meta infomation: config.json

{
    "index.html": {
        "title": "GULP JSON REPLACE",
        "description": "description for the main page",
        "keywords": "keywords for the main page"
    },
    "signin/signin.html": {
        "title": "Sign In",
        "description": "description for the page sign in",
        "keywords": "keywords for the page sign in"
    },
    "signup/signup.html": {
        "title": "Sign Up",
        "description": "description for the page sign up",
        "keywords": "keywords for the page sign up"
    }
}

Rusult: index.html

<!DOCTYPE html>
<html>
<head>
    <title>GULP JSON REPLACE</title>
    <meta charset="utf-8" />
    <meta name="description" content="description for the main page">
    <meta name="keywords" content="keywords for the main page">
</head>
<body></body>
</html>

signin/signin.html

<!DOCTYPE html>
<html>
<head>
    <title>Sign In</title>
    <meta charset="utf-8" />
    <meta name="description" content="description for the page sign in">
    <meta name="keywords" content="keywords for the page sign in">
</head>
<body></body>
</html>

signup/signup.html

<!DOCTYPE html>
<html>
<head>
    <title>Sign Up</title>
    <meta charset="utf-8" />
    <meta name="description" content="description for the page sign up">
    <meta name="keywords" content="keywords for the page sign up">
</head>
<body></body>
</html>

First, install gulp-json-replace as a development dependency:

npm install --save-dev gulp-json-replace

Then, add it to your gulpfile.js:

var jr = require('gulp-json-replace');

gulp.task('json-replace', function() {
    return gulp.src('www/**/*.html')
        .pipe(jr({
            src: './config.json',
            identify: '%%'
        }))
        .pipe(gulp.dest('www/'))
})

API

replace(options)

options

Type: Object

options.src

Type: String or Object Accept the path of json file, or an JavaScript Object Literals.

options.identify

Type: String Default: %% The string to identify the search field.

options.mode

Type: String Default: strict Specify the match mode, the value would be strict or loose, if set to loose, it will ignore the file extension. For example: "index.html" will match files of "index.hbs", "index.html", "index.xxx"...

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago