Skip to content

Commit

Permalink
Update Tokenizer to recognise inequalities and parse them as text
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbrzn committed Feb 1, 2018
1 parent 3880c5c commit 369b0ba
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var decodeCodePoint = require("entities/lib/decode_codepoint.js"),
xmlMap = require("entities/maps/xml.json"),

i = 0,


MARKDOWN = i++,
TEXT = i++,
BEFORE_TAG_NAME = i++, //after <
IN_TAG_NAME = i++,
Expand Down Expand Up @@ -144,17 +145,26 @@ function Tokenizer(options, cbs){
this._ended = false;
this._xmlMode = !!(options && options.xmlMode);
this._decodeEntities = !!(options && options.decodeEntities);
this._isMarkdownCode = false;
}

Tokenizer.prototype._stateMarkdown = function(c){
if(c === '`') {
this._state = TEXT;
}
}

Tokenizer.prototype._stateText = function(c){
// Parse open tag if it is not Markdown
if(c === "<" && !this._isMarkdownCode){
if(this._index > this._sectionStart){
this._cbs.ontext(this._getSection());
if(c === '`'){
this._state = MARKDOWN;
} else if(c === "<"){
var isInequality = (this._index + 1 < this._buffer.length) && (this._buffer.charAt(this._index + 1) === '=');
if(!isInequality){
if (this._index > this._sectionStart) {
this._cbs.ontext(this._getSection());
}
this._state = BEFORE_TAG_NAME;
this._sectionStart = this._index;
}
this._state = BEFORE_TAG_NAME;
this._sectionStart = this._index;
} else if(this._decodeEntities && this._special === SPECIAL_NONE && c === "&"){
if(this._index > this._sectionStart){
this._cbs.ontext(this._getSection());
Expand Down Expand Up @@ -637,15 +647,14 @@ Tokenizer.prototype.write = function(chunk){
Tokenizer.prototype._parse = function(){
while(this._index < this._buffer.length && this._running){
var c = this._buffer.charAt(this._index);
// Detect Markdown code so that it is parsed as text instead of HTML
if(c === '`'){
this._isMarkdownCode = !this._isMarkdownCode;
}
if(this._state === TEXT) {

if(this._state === MARKDOWN){
this._stateMarkdown(c);
} else if(this._state === TEXT){
this._stateText(c);
} else if(this._state === BEFORE_TAG_NAME){
this._stateBeforeTagName(c);
} else if(this._state === IN_TAG_NAME) {
} else if(this._state === IN_TAG_NAME){
this._stateInTagName(c);
} else if(this._state === BEFORE_CLOSING_TAG_NAME){
this._stateBeforeCloseingTagName(c);
Expand Down

0 comments on commit 369b0ba

Please sign in to comment.