Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
3.7 KiB
199 lines
3.7 KiB
10 years ago
|
// Token class
|
||
|
|
||
10 years ago
|
/**
|
||
|
* class Token
|
||
|
**/
|
||
|
|
||
|
/**
|
||
|
* new Token(type, tag, nesting)
|
||
|
*
|
||
|
* Create new token and fill passed properties.
|
||
|
**/
|
||
10 years ago
|
function Token(type, tag, nesting) {
|
||
10 years ago
|
/**
|
||
|
* Token#type -> String
|
||
|
*
|
||
|
* Type of the token (string, e.g. "paragraph_open")
|
||
|
**/
|
||
10 years ago
|
this.type = type;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#tag -> String
|
||
|
*
|
||
|
* html tag name, e.g. "p"
|
||
|
**/
|
||
10 years ago
|
this.tag = tag;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#attrs -> Array
|
||
|
*
|
||
|
* Html attributes. Format: `[ [ name1, value1 ], [ name2, value2 ] ]`
|
||
|
**/
|
||
10 years ago
|
this.attrs = null;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#map -> Array
|
||
|
*
|
||
|
* Source map info. Format: `[ line_begin, line_end ]`
|
||
|
**/
|
||
10 years ago
|
this.map = null;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#nesting -> Number
|
||
|
*
|
||
|
* Level change (number in {-1, 0, 1} set), where:
|
||
|
*
|
||
|
* - `1` means the tag is opening
|
||
|
* - `0` means the tag is self-closing
|
||
|
* - `-1` means the tag is closing
|
||
|
**/
|
||
10 years ago
|
this.nesting = nesting;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#level -> Number
|
||
|
*
|
||
|
* nesting level, the same as `state.level`
|
||
|
**/
|
||
10 years ago
|
this.level = 0;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#children -> Array
|
||
|
*
|
||
|
* An array of child nodes (inline and img tokens)
|
||
|
**/
|
||
10 years ago
|
this.children = null;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#content -> String
|
||
|
*
|
||
|
* In a case of self-closing tag (code, html, fence, etc.),
|
||
|
* it has contents of this tag.
|
||
|
**/
|
||
10 years ago
|
this.content = '';
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Token#markup -> String
|
||
|
*
|
||
|
* '*' or '_' for emphasis, fence string for fence, etc.
|
||
|
**/
|
||
10 years ago
|
this.markup = '';
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#info -> String
|
||
|
*
|
||
4 years ago
|
* Additional information:
|
||
|
*
|
||
|
* - Info string for "fence" tokens
|
||
|
* - The value "auto" for autolink "link_open" and "link_close" tokens
|
||
4 years ago
|
* - The string value of the item marker for ordered-list "list_item_open" tokens
|
||
10 years ago
|
**/
|
||
10 years ago
|
this.info = '';
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Token#meta -> Object
|
||
|
*
|
||
|
* A place for plugins to store an arbitrary data
|
||
10 years ago
|
**/
|
||
10 years ago
|
this.meta = null;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#block -> Boolean
|
||
|
*
|
||
|
* True for block-level tokens, false for inline tokens.
|
||
|
* Used in renderer to calculate line breaks
|
||
|
**/
|
||
10 years ago
|
this.block = false;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token#hidden -> Boolean
|
||
|
*
|
||
|
* If it's true, ignore this element when rendering. Used for tight lists
|
||
|
* to hide paragraphs.
|
||
|
**/
|
||
10 years ago
|
this.hidden = false;
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Token.attrIndex(name) -> Number
|
||
|
*
|
||
|
* Search attribute index by name.
|
||
|
**/
|
||
|
Token.prototype.attrIndex = function attrIndex(name) {
|
||
|
var attrs, i, len;
|
||
|
|
||
|
if (!this.attrs) { return -1; }
|
||
|
|
||
|
attrs = this.attrs;
|
||
|
|
||
|
for (i = 0, len = attrs.length; i < len; i++) {
|
||
|
if (attrs[i][0] === name) { return i; }
|
||
|
}
|
||
|
return -1;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Token.attrPush(attrData)
|
||
|
*
|
||
|
* Add `[ name, value ]` attribute to list. Init attrs if necessary
|
||
|
**/
|
||
|
Token.prototype.attrPush = function attrPush(attrData) {
|
||
|
if (this.attrs) {
|
||
|
this.attrs.push(attrData);
|
||
|
} else {
|
||
|
this.attrs = [ attrData ];
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
9 years ago
|
/**
|
||
|
* Token.attrSet(name, value)
|
||
|
*
|
||
|
* Set `name` attribute to `value`. Override old value if exists.
|
||
|
**/
|
||
|
Token.prototype.attrSet = function attrSet(name, value) {
|
||
|
var idx = this.attrIndex(name),
|
||
|
attrData = [ name, value ];
|
||
|
|
||
|
if (idx < 0) {
|
||
|
this.attrPush(attrData);
|
||
|
} else {
|
||
|
this.attrs[idx] = attrData;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
9 years ago
|
/**
|
||
|
* Token.attrGet(name)
|
||
|
*
|
||
|
* Get the value of attribute `name`, or null if it does not exist.
|
||
|
**/
|
||
|
Token.prototype.attrGet = function attrGet(name) {
|
||
|
var idx = this.attrIndex(name), value = null;
|
||
|
if (idx >= 0) {
|
||
|
value = this.attrs[idx][1];
|
||
|
}
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
|
||
9 years ago
|
/**
|
||
|
* Token.attrJoin(name, value)
|
||
|
*
|
||
|
* Join value to existing attribute via space. Or create new attribute if not
|
||
|
* exists. Useful to operate with token classes.
|
||
|
**/
|
||
|
Token.prototype.attrJoin = function attrJoin(name, value) {
|
||
|
var idx = this.attrIndex(name);
|
||
|
|
||
|
if (idx < 0) {
|
||
|
this.attrPush([ name, value ]);
|
||
|
} else {
|
||
|
this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
1 year ago
|
export default Token;
|