3 var xtend = require('xtend');
4 var toggle = require('state-toggle');
5 var vfileLocation = require('vfile-location');
6 var unescape = require('./unescape');
7 var decode = require('./decode');
8 var tokenizer = require('./tokenizer');
10 module.exports = Parser;
12 function Parser(doc, file) {
15 this.options = xtend(this.options);
23 this.toOffset = vfileLocation(file).toOffset;
24 this.unescape = unescape(this, 'escape');
25 this.decode = decode(this);
28 var proto = Parser.prototype;
31 proto.setOptions = require('./set-options');
32 proto.parse = require('./parse');
34 /* Expose `defaults`. */
35 proto.options = require('./defaults');
37 /* Enter and exit helpers. */
38 proto.exitStart = toggle('atStart', true);
39 proto.enterList = toggle('inList', false);
40 proto.enterLink = toggle('inLink', false);
41 proto.enterBlock = toggle('inBlock', false);
43 /* Nodes that can interupt a paragraph:
46 * A paragraph, followed by a thematic break.
50 * In the above example, the thematic break “interupts”
52 proto.interruptParagraph = [
58 ['setextHeading', {commonmark: false}],
59 ['definition', {commonmark: false}],
60 ['footnote', {commonmark: false}]
63 /* Nodes that can interupt a list:
70 * In the above example, the thematic break “interupts”
72 proto.interruptList = [
73 ['fencedCode', {pedantic: false}],
74 ['thematicBreak', {pedantic: false}],
75 ['definition', {commonmark: false}],
76 ['footnote', {commonmark: false}]
79 /* Nodes that can interupt a blockquote:
86 * In the above example, the thematic break “interupts”
88 proto.interruptBlockquote = [
89 ['indentedCode', {commonmark: true}],
90 ['fencedCode', {commonmark: true}],
91 ['atxHeading', {commonmark: true}],
92 ['setextHeading', {commonmark: true}],
93 ['thematicBreak', {commonmark: true}],
94 ['html', {commonmark: true}],
95 ['list', {commonmark: true}],
96 ['definition', {commonmark: false}],
97 ['footnote', {commonmark: false}]
101 proto.blockTokenizers = {
102 newline: require('./tokenize/newline'),
103 indentedCode: require('./tokenize/code-indented'),
104 fencedCode: require('./tokenize/code-fenced'),
105 blockquote: require('./tokenize/blockquote'),
106 atxHeading: require('./tokenize/heading-atx'),
107 thematicBreak: require('./tokenize/thematic-break'),
108 list: require('./tokenize/list'),
109 setextHeading: require('./tokenize/heading-setext'),
110 html: require('./tokenize/html-block'),
111 footnote: require('./tokenize/footnote-definition'),
112 definition: require('./tokenize/definition'),
113 table: require('./tokenize/table'),
114 paragraph: require('./tokenize/paragraph')
117 proto.inlineTokenizers = {
118 escape: require('./tokenize/escape'),
119 autoLink: require('./tokenize/auto-link'),
120 url: require('./tokenize/url'),
121 html: require('./tokenize/html-inline'),
122 link: require('./tokenize/link'),
123 reference: require('./tokenize/reference'),
124 strong: require('./tokenize/strong'),
125 emphasis: require('./tokenize/emphasis'),
126 deletion: require('./tokenize/delete'),
127 code: require('./tokenize/code-inline'),
128 break: require('./tokenize/break'),
129 text: require('./tokenize/text')
132 /* Expose precedence. */
133 proto.blockMethods = keys(proto.blockTokenizers);
134 proto.inlineMethods = keys(proto.inlineTokenizers);
137 proto.tokenizeBlock = tokenizer('block');
138 proto.tokenizeInline = tokenizer('inline');
139 proto.tokenizeFactory = tokenizer;
141 /* Get all keys in `value`. */
142 function keys(value) {