GitbookIO / kramed

A markdown (kramdown compatible) parser and compiler. Built for speed. (Fork of marked)
MIT License
223 stars 47 forks source link

ignore empty object and plain string in function tok() #1

Closed codepiano closed 10 years ago

codepiano commented 10 years ago

empty objects and plain strings in variable 'this.tokens' will result in additional 'undefined' string in the output of the 'Parser.prototype.parse' function.It will pollute the search_index.json, product a lot of 'undefinedundefined...' index.

AaronO commented 10 years ago

Thanks :+1:

codepiano commented 10 years ago

@AaronO actually,the deep reason is the lodash funtion 'toArray' in gitbook's search_index.js, it will cast object

{ '0': { type: 'heading', depth: 1, text: 'Article' },
  '1': { type: 'paragraph', text: 'test' },
  type: 'normal',
  id: 'gitbook_1',
  links: {} }

to

[{"type":"heading","depth":1,"text":"Article"},{"type":"paragraph","text":"test"},"normal","gitbook_1",{}]

so when the last three values 'normal','gitbook_1' and {} is processed by the tok() function,will produce 'undefined'. I think maybe there is a better way to fix this,avoid to add these attrubutes to the array,but I am not familiar with the data flow,so just add code to ignore these in tok().