@uttori/plugin-renderer-replacer

Uttori plugin for replacing text in a rendering pipeline.

Usage no npm install needed!

<script type="module">
  import uttoriPluginRendererReplacer from 'https://cdn.skypack.dev/@uttori/plugin-renderer-replacer';
</script>

README

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

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({ ... });

ReplacerRenderer.register(context)

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);

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.

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);

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.

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);

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.

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);

Tests

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

npm install
npm test
DEBUG=Uttori* npm test

Contributors

License