1.0.0 • Published 3 years ago

@uttori/plugin-renderer-replacer v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

view on npm npm module downloads Build Status Dependency Status Coverage Status

Uttori Renderer - Replacer

Uttori plugin for replacing text in a rendering pipeline with Regular Expressions.

Install

npm install --save @uttori/plugin-renderer-replacer

Config

{
  // Registration Events
  events: {
    renderContent: [],
    renderCollection: [],
    validateConfig: [],
  },

  // Replace Rules
  rules: [
    {
      test: 'shamrock',
      output: '☘️',
    },
    {
      test: /cow[-\s]?boy/gm,
      output: '🤠',
    },
  ]
}

API Reference

ReplacerRenderer

Uttori Replacer Renderer

Kind: global class

ReplacerRenderer.configKey ⇒ string

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] };

ReplacerRenderer.defaultConfig() ⇒ object

The default configuration.

Kind: static method of ReplacerRenderer
Returns: object - The configuration.
Example (ReplacerRenderer.defaultConfig())

const config = { ...ReplacerRenderer.defaultConfig(), ...context.config[ReplacerRenderer.configKey] };

ReplacerRenderer.validateConfig(config, _context)

Validates the provided configuration for required entries.

Kind: static method of ReplacerRenderer

ParamTypeDescription
configobjectA configuration object.
config.configKeyobjectA configuration object specifically for this plugin.
_contextobjectUnused

Example (ReplacerRenderer.validateConfig(config, _context))

ReplacerRenderer.validateConfig({ ... });

ReplacerRenderer.register(context)

Register the plugin with a provided set of events on a provided Hook system.

Kind: static method of ReplacerRenderer

ParamTypeDescription
contextobjectA Uttori-like context.
context.hooksobjectAn event system / hook system to use.
context.hooks.onfunctionAn event registration function.
context.configobjectA provided configuration to use.
context.config.eventsobjectAn 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);

ReplacerRenderer.renderContent(content, context) ⇒ string

Replace content in a provided string with a provided context.

Kind: static method of ReplacerRenderer
Returns: string - The rendered content.

ParamTypeDescription
contentstringContent to be converted to HTML.
contextobjectA Uttori-like context.
context.configobjectA provided configuration to use.

Example (ReplacerRenderer.renderContent(content, context))

const context = {
  config: {
    [ReplacerRenderer.configKey]: {
      ...,
    },
  },
};
ReplacerRenderer.renderContent(content, context);

ReplacerRenderer.renderCollection(collection, context) ⇒ Array.<object>

Replace content in a collection of Uttori documents with a provided context.

Kind: static method of ReplacerRenderer
Returns: Array.<object> - } The rendered documents.

ParamTypeDescription
collectionArray.<object>A collection of Uttori documents.
contextobjectA Uttori-like context.
context.configobjectA provided configuration to use.

Example (ReplacerRenderer.renderCollection(collection, context))

const context = {
  config: {
    [ReplacerRenderer.configKey]: {
      ...,
    },
  },
};
ReplacerRenderer.renderCollection(collection, context);

ReplacerRenderer.render(content, config) ⇒ string

Replace content in a provided string with a provided set of rules.

Kind: static method of ReplacerRenderer
Returns: string - The rendered content.

ParamTypeDescription
contentstringContent to be searched through to make replacements.
configobjectA provided configuration to use.

Example (ReplacerRenderer.render(content, config))

const html = ReplacerRenderer.render(content, config);

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test
DEBUG=Uttori* npm test

Contributors

License