Skip to content

Commit

Permalink
(enh) make emitter a beta config option
Browse files Browse the repository at this point in the history
- make emitter configurable
- mark this API as beta/private for now
- make rendering HTML a responsibility of the emitter
  (though it can of course delegate)

That wraps emitting/rendering up nicely into a single object.
  • Loading branch information
joshgoebel committed Feb 29, 2020
1 parent bc2f74d commit 1e14962
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/highlight.js
Expand Up @@ -5,7 +5,6 @@ https://highlightjs.org/

import deepFreeze from './vendor/deep_freeze';
import TokenTreeEmitter from './lib/token_tree';
import HTMLRenderer from './lib/html_renderer';
import * as regex from './lib/regex';
import * as utils from './lib/utils';
import * as MODES from './lib/modes';
Expand Down Expand Up @@ -44,7 +43,10 @@ const HLJS = function(hljs) {
classPrefix: 'hljs-',
tabReplace: null,
useBR: false,
languages: undefined
languages: undefined,
// beta configuration options, subject to change, welcome to discuss
// https://github.com/highlightjs/highlight.js/issues/1086
__emitter: TokenTreeEmitter
};

/* Utility functions */
Expand Down Expand Up @@ -322,7 +324,7 @@ const HLJS = function(hljs) {
var top = continuation || language;
var continuations = {}; // keep continuations for sub-languages
var result;
var emitter = new TokenTreeEmitter();
var emitter = new options.__emitter(options);
processContinuations();
var mode_buffer = '';
var relevance = 0;
Expand All @@ -341,7 +343,7 @@ const HLJS = function(hljs) {
processLexeme(codeToHighlight.substr(index));
emitter.closeAllNodes();
emitter.finalize();
result = new HTMLRenderer(emitter, options).value();
result = emitter.toHTML();

return {
relevance: relevance,
Expand Down Expand Up @@ -395,7 +397,7 @@ const HLJS = function(hljs) {
languageSubset = languageSubset || options.languages || Object.keys(languages);
var result = {
relevance: 0,
emitter: new TokenTreeEmitter(),
emitter: new options.__emitter(options),
value: escape(code)
};
var second_best = result;
Expand Down
16 changes: 14 additions & 2 deletions src/lib/token_tree.js
@@ -1,3 +1,5 @@
import HTMLRenderer from './html_renderer';

class TokenTree {
constructor() {
this.rootNode = { children: [] };
Expand Down Expand Up @@ -68,17 +70,22 @@ class TokenTree {
Currently this is all private API, but this is the minimal API necessary
that an Emitter must implement to fully support the parser.
API:
Minimal interface:
- addKeyword(text, kind)
- addText(text)
- addSublanguage(emitter, subLangaugeName)
- finalize()
- openNode(kind)
- closeNode()
- closeAllNodes()
- toHTML()
*/
export default class TokenTreeEmitter extends TokenTree {
constructor() {
constructor(options) {
super();
this.options = options;
}

addKeyword(text, kind) {
Expand All @@ -102,6 +109,11 @@ export default class TokenTreeEmitter extends TokenTree {
this.add(node);
}

toHTML() {
let renderer = new HTMLRenderer(this, this.options);
return renderer.value();
}

finalize() {
return;
}
Expand Down

0 comments on commit 1e14962

Please sign in to comment.