All replace methods take Buffer
or String
.
An object with all regular expressions used in rcs-core
Replaces attributes with
id
orclass
. Everything within a<script>
tag, will be processed via replace.js, and everything inside a<style>
tag with replace.css.
rcs.replace.html(code[, options])
Parameters:
- code
<String>
- options
<Object>
Options:
- espreeOptions: same as replace.js options
- triggerClassAttributes
<Array>
: Array of string or regular expressions. Renames all classes with the matching string or regex. E.g.[/data-*/ , 'custom-attr']
matches all data attributes and 'custom-attr' - triggerIdAttributes
<Array>
: Same as triggerClassAttributes just for IDs
Example:
const rcs = require('rcs-core');
// first set the id to replace
rcs.selectorLibrary.set('.my-class');
const replacedHtml = rcs.replace.html('<div class="my-class"></div>');
// output:
// '<div class="a"></div>'
Same as rcs.replace.html just with pug templates
rcs.replace.pug(code[, options])
Parameters:
- code
<String>
- same options as rcs.replace.html
<Object>
Example:
const rcs = require('rcs-core');
// first set the id to replace
rcs.selectorLibrary.set('.my-class');
const replacedHtml = rcs.replace.pug(`
.my-class(attr='my attribute')
.another-class
`);
// output in pug:
// `
// .a(attr!='my attribute')
// .another-class
// `
Replaces all strings which matches the filled selectors
rcs.replace.any(code)
Parameters:
- code
<String>
Example:
const rcs = require('rcs-core');
// first set the id to replace
rcs.selectorLibrary.set('#my-id');
const replacedCode = rcs.replace.any('document.getElementById("my-id")');
// output:
// document.getElementById("t")
Stores all selectors in
rcs.selectorLibrary
and replaces them
rcs.replace.css(code)
Parameters:
- code
<String>
Example:
const rcs = require('rcs-core');
// first set the id to replace
rcs.selectorLibrary.set('#my-id');
const replacedBuffer = rcs.replace.css('#my-id: {}');
// output:
// '#a: {}'
Replaces all selectors in strings of a JavaScript/JSX file
rcs.replace.js(code)
Parameters:
- code
<String>
- espreeOptions
<Object>
Espree default options:
{
ecmaVersion: 9,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
range: true, // cannot be changed
loc: true, // cannot be changed
comment: true, // cannot be changed
attachComment: true, // cannot be changed
tokens: true, // cannot be changed
}
Example:
const rcs = require('rcs-core');
const replacedBuffer = rcs.replace.js('#my-id: {}');
Replaces a given string with the stored value in
rcs.selectorLibrary
rcs.replace.string(selector)
Parameters:
- selector
<String>
: Any selector which is already set inrcs.selectorLibrary
Example:
const rcs = require('rcs-core');
// first set the id to replace
rcs.selectorLibrary.set('#my-id');
const replacedStringDoubleQuote = rcs.replace.string('"my-id"'); // returns "a"
const replacedStringSingleQuote = rcs.replace.string("'my-id'"); // returns 'a'