Skip to content

Commit aa3d329

Browse files
committed
add emitTokens docs
1 parent cf559ad commit aa3d329

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/mode-reference.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,52 @@ Disables autodetection for this language.
105105
(defaults to false, meaning auto-detect is enabled)
106106

107107

108+
__emitTokens
109+
^^^^^^^^^^^^
110+
111+
.. warning::
112+
113+
**This is currently still private/beta API**, though it's expected to be fairly stable.
114+
115+
It should land in version 12.0.
116+
117+
Allows grammars to bundle custom parsers - bypassing the default parser and grammar mode definitions. This should be a function that accepts the raw source code as the first argument and an "Emitter" object as the second.
118+
119+
A custom parser may parse the source as it sees fit - making calls to the Emitter along the way - allowing Highlight.js to generate and theme the final HTML.
120+
121+
The **Emitter** API is trivial:
122+
123+
- ``addText(text)``
124+
- ``startScope(name)``
125+
- ``endScope()``
126+
127+
Given:
128+
129+
::
130+
131+
hello beautiful world!
132+
133+
134+
Assuming beautiful is a keyword our Emitter calls might look something like:
135+
136+
::
137+
138+
addText("hello ")
139+
startScope("keyword")
140+
addText("beautiful")
141+
endScope()
142+
addText(" world!")
143+
144+
Resulting in the following generated HTML:
145+
146+
.. code-block:: html
147+
148+
hello <span class="hljs-keyword">beautiful</span> world!
149+
150+
.. note::
151+
152+
The intended use of ``addText`` is larger chunks of plain text, not individual characters. Custom parsers should buffer plain text output into complete strings rather than sending output one character at a time.
153+
108154
compilerExtensions
109155
^^^^^^^^^^^^^^^^^^
110156

0 commit comments

Comments
 (0)