Uttori plugin for replacing text in a rendering pipeline with Regular Expressions.
npm install --save @uttori/plugin-renderer-replacer{
// Registration Events
events: {
renderContent: [],
renderCollection: [],
validateConfig: [],
},
// Replace Rules
rules: [
{
test: 'shamrock',
output: 'βοΈ',
},
{
test: /cow[-\s]?boy/gm,
output: 'π€ ',
},
]
}Uttori Replacer Renderer
Kind: global class
- ReplacerRenderer
- .configKey β
string - .defaultConfig() β
object - .validateConfig(config, _context)
- .register(context)
- .renderContent(content, context) β
string - .renderCollection(collection, context) β
Array.<object> - .render(content, config) β
string
- .configKey β
The configuration key for plugin to look for in the provided configuration.
Kind: static property of ReplacerRenderer
Returns: string - The configuration key.
Example (ReplacerRenderer.configKey)
const config = { ...ReplacerRenderer.defaultConfig(), ...context.config[ReplacerRenderer.configKey] };The default configuration.
Kind: static method of ReplacerRenderer
Returns: object - The configuration.
Example (ReplacerRenderer.defaultConfig())
const config = { ...ReplacerRenderer.defaultConfig(), ...context.config[ReplacerRenderer.configKey] };Validates the provided configuration for required entries.
Kind: static method of ReplacerRenderer
| Param | Type | Description |
|---|---|---|
| config | object |
A configuration object. |
| config.configKey | object |
A configuration object specifically for this plugin. |
| _context | object |
Unused |
Example (ReplacerRenderer.validateConfig(config, _context))
ReplacerRenderer.validateConfig({ ... });Register the plugin with a provided set of events on a provided Hook system.
Kind: static method of ReplacerRenderer
| Param | Type | Description |
|---|---|---|
| context | object |
A Uttori-like context. |
| context.hooks | object |
An event system / hook system to use. |
| context.hooks.on | function |
An event registration function. |
| context.config | object |
A provided configuration to use. |
| context.config.events | object |
An object whose keys correspong to methods, and contents are events to listen for. |
Example (ReplacerRenderer.register(context))
const context = {
hooks: {
on: (event, callback) => { ... },
},
config: {
[ReplacerRenderer.configKey]: {
...,
events: {
renderContent: ['render-content', 'render-meta-description'],
renderCollection: ['render-search-results'],
validateConfig: ['validate-config'],
},
},
},
};
ReplacerRenderer.register(context);Replace content in a provided string with a provided context.
Kind: static method of ReplacerRenderer
Returns: string - The rendered content.
| Param | Type | Description |
|---|---|---|
| content | string |
Content to be converted to HTML. |
| context | object |
A Uttori-like context. |
| context.config | object |
A provided configuration to use. |
Example (ReplacerRenderer.renderContent(content, context))
const context = {
config: {
[ReplacerRenderer.configKey]: {
...,
},
},
};
ReplacerRenderer.renderContent(content, context);Replace content in a collection of Uttori documents with a provided context.
Kind: static method of ReplacerRenderer
Returns: Array.<object> - } The rendered documents.
| Param | Type | Description |
|---|---|---|
| collection | Array.<object> |
A collection of Uttori documents. |
| context | object |
A Uttori-like context. |
| context.config | object |
A provided configuration to use. |
Example (ReplacerRenderer.renderCollection(collection, context))
const context = {
config: {
[ReplacerRenderer.configKey]: {
...,
},
},
};
ReplacerRenderer.renderCollection(collection, context);Replace content in a provided string with a provided set of rules.
Kind: static method of ReplacerRenderer
Returns: string - The rendered content.
| Param | Type | Description |
|---|---|---|
| content | string |
Content to be searched through to make replacements. |
| config | object |
A provided configuration to use. |
Example (ReplacerRenderer.render(content, config))
const html = ReplacerRenderer.render(content, config);To run the test suite, first install the dependencies, then run npm test:
npm install
npm test
DEBUG=Uttori* npm test