Textbox.io 2.x Documentation : Macros: Writing Content-Aware Code

What is a macro?

Textbox.io provides a Macro API enabling you to create functions that detect particular content in the editor as a user types, and perform meaningful actions to manipulate the content.  Specifically macros are evaluated when the user presses the space or the enter keys. 

Currently, Textbox provides the ability to add a "Simple" macro, using:

editor.macros.addSimpleMacro('startString', 'endString', callback)

where any text entered before the spacebar is pressed that begins with startString and ends with endString forms a pattern. When a match is found, callback is applied with the match as an argument.

callback can return a replacement (if one is necessary), or the original that was matched.

Built-in macros

A collection of macros are enabled by default. The list of macros, and how to control their availability, is documented in the macros configuration object.

Example: Red text

In the example provided below, we match on any text that begins with [red] and ends with [/red], and wrap the text in a span where the color is set to red. For example, any of the following would be matches:

  • [red]hello world[/red]
  • [red]testing[/red]
  • [red] She sells sea shells by the sea shore[/red]

 

See Also

macros configuration object

editor.macros runtime API