1 | 'use strict';
|
2 |
|
3 |
|
4 | var test = require('tap').test
|
5 | , transform = require('../lib/transform')
|
6 |
|
7 | function inspect(obj, depth) {
|
8 | console.log(require('util').inspect(obj, false, depth || 5, true));
|
9 | }
|
10 |
|
11 | function check(md, anchors, mode) {
|
12 | test('transforming ' + md , function (t) {
|
13 | var res = transform(md, mode)
|
14 |
|
15 |
|
16 | var data = res.data.split('\n');
|
17 |
|
18 |
|
19 | var startLines = transform.start.split('\n')
|
20 | , anchorLines = anchors.split('\n')
|
21 | , endLines = transform.end.split('\n')
|
22 | , mdLines = md.split('\n')
|
23 |
|
24 | var rig = startLines
|
25 | .concat(anchorLines.slice(0, -2))
|
26 | .concat(endLines)
|
27 | .concat('')
|
28 | .concat(mdLines);
|
29 |
|
30 | t.ok(res.transformed, 'transforms it');
|
31 | t.deepEqual(data, rig, 'generates correct anchors')
|
32 | t.end()
|
33 | })
|
34 | }
|
35 |
|
36 |
|
37 | check(
|
38 | [ '# My Module'
|
39 | , 'Some text here'
|
40 | , '## API'
|
41 | , '### Method One'
|
42 | , 'works like this'
|
43 | , '### Method Two'
|
44 | , '#### Main Usage'
|
45 | , 'some main usage here'
|
46 | ].join('\n')
|
47 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
48 | , '- [My Module](#my-module)\n'
|
49 | , '\t- [API](#api)\n'
|
50 | , '\t\t- [Method One](#method-one)\n'
|
51 | , '\t\t- [Method Two](#method-two)\n'
|
52 | , '\t\t\t- [Main Usage](#main-usage)\n\n\n'
|
53 | ].join('')
|
54 | )
|
55 |
|
56 | check(
|
57 | [ '# My Module using \\r\\n line endings'
|
58 | , 'Some text here'
|
59 | , '## API'
|
60 | , '### Method One'
|
61 | , 'works like this'
|
62 | , '### Method Two'
|
63 | , '#### Main Usage'
|
64 | , 'some main usage here'
|
65 | ].join('\r\n')
|
66 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
67 | , '- [My Module using \\r\\n line endings](#my-module-using-\\r\\n-line-endings)\n'
|
68 | , '\t- [API](#api)\n'
|
69 | , '\t\t- [Method One](#method-one)\n'
|
70 | , '\t\t- [Method Two](#method-two)\n'
|
71 | , '\t\t\t- [Main Usage](#main-usage)\n\n\n'
|
72 | ].join('')
|
73 | )
|
74 |
|
75 | check(
|
76 | [ 'My Module'
|
77 | , '========='
|
78 | , 'Some text here'
|
79 | , 'API'
|
80 | , '---------'
|
81 | ].join('\n')
|
82 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
83 | , '- [My Module](#my-module)\n'
|
84 | , '\t- [API](#api)\n\n\n'
|
85 | ].join('')
|
86 | )
|
87 |
|
88 | check(
|
89 | [ '# My Module #'
|
90 | , 'Some text here'
|
91 | , '## API ##'
|
92 | ].join('\n')
|
93 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
94 | , '- [My Module](#my-module)\n'
|
95 | , '\t- [API](#api)\n\n\n'
|
96 | ].join('')
|
97 | )
|
98 |
|
99 | check(
|
100 | [ '## Title should be included'
|
101 | , ''
|
102 | , '```js'
|
103 | , 'function foo () {'
|
104 | , ' // ## This title should be ignored'
|
105 | , '}'
|
106 | , '## This title should also be ignored'
|
107 | , '```'
|
108 | , ''
|
109 | , '## Title should also be included'
|
110 | ].join('\n')
|
111 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
112 | , '- [Title should be included](#title-should-be-included)\n'
|
113 | , '- [Title should also be included](#title-should-also-be-included)\n\n\n'
|
114 | ].join('')
|
115 | )
|
116 |
|
117 | check(
|
118 | [ '# Repeating A Title'
|
119 | , ''
|
120 | , '# Repeating A Title'
|
121 | ].join('\n')
|
122 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
123 | , '- [Repeating A Title](#repeating-a-title)\n'
|
124 | , '- [Repeating A Title](#repeating-a-title-1)\n\n\n'
|
125 | ].join('')
|
126 | )
|
127 |
|
128 | check(
|
129 | [ '## Header'
|
130 | , 'some content'
|
131 | , '-- preceded by two dashes but has content, therefore "some content" should not be header'
|
132 | ].join('\n')
|
133 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n',
|
134 | '- [Header](#header)\n\n\n',
|
135 | ].join('')
|
136 | )
|
137 |
|
138 | check(
|
139 | [ '# Different Kinds'
|
140 | , ''
|
141 | , 'In the Right Order'
|
142 | , '=================='
|
143 | ].join('\n')
|
144 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
145 | , '- [Different Kinds](#different-kinds)\n'
|
146 | , '- [In the Right Order](#in-the-right-order)\n\n\n'
|
147 | ].join('')
|
148 | )
|
149 |
|
150 | check(
|
151 | [ 'Different Kinds 2'
|
152 | , '==============='
|
153 | , ''
|
154 | , '# In the Right Order 2'
|
155 | ].join('\n')
|
156 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
157 | , '- [Different Kinds 2](#different-kinds-2)\n'
|
158 | , '- [In the Right Order 2](#in-the-right-order-2)\n\n\n'
|
159 | ].join('')
|
160 | )
|
161 |
|
162 | test('transforming when old toc exists', function (t) {
|
163 | var md = [
|
164 | '# Header above'
|
165 | , ''
|
166 | , 'The above header should be ignored since it is above the existing toc'
|
167 | , ''
|
168 | , '<!-- START doctoc generated TOC please keep comment here to allow auto update -->'
|
169 | , '<!-- DON\'T EDIT THIS SECTION INSTEAD RE-RUN doctoc TO UPDATE -->'
|
170 | , '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*'
|
171 | , ''
|
172 | , '- [OldHeader](#oldheader)'
|
173 | , ''
|
174 | , '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
|
175 | , '## Header'
|
176 | , 'some content'
|
177 | , ''
|
178 | ].join('\n')
|
179 |
|
180 | var res = transform(md)
|
181 |
|
182 | t.ok(res.transformed, 'transforms it')
|
183 |
|
184 | t.deepEqual(
|
185 | res.toc.split('\n')
|
186 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
|
187 | '',
|
188 | '- [Header](#header)',
|
189 | '' ]
|
190 | , 'replaces old toc'
|
191 | )
|
192 |
|
193 | t.deepEqual(
|
194 | res.wrappedToc.split('\n')
|
195 | , [ '<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
|
196 | '<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
|
197 | '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
|
198 | '',
|
199 | '- [Header](#header)',
|
200 | '',
|
201 | '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
|
202 | ]
|
203 | , 'wraps old toc'
|
204 | )
|
205 |
|
206 | t.deepEqual(
|
207 | res.data.split('\n')
|
208 | , [ '# Header above',
|
209 | '',
|
210 | 'The above header should be ignored since it is above the existing toc',
|
211 | '',
|
212 | '<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
|
213 | '<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
|
214 | '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
|
215 | '',
|
216 | '- [Header](#header)',
|
217 | '',
|
218 | '<!-- END doctoc generated TOC please keep comment here to allow auto update -->',
|
219 | '## Header',
|
220 | 'some content',
|
221 | '' ]
|
222 | , 'updates the content with the new toc and ignores header before existing toc'
|
223 | )
|
224 | t.end()
|
225 | })
|
226 |
|
227 |
|
228 | check(
|
229 | [ '# My Module'
|
230 | , 'Some text here'
|
231 | , '## API'
|
232 | , '### Method One'
|
233 | , 'works like this'
|
234 | , '### Method Two'
|
235 | , '#### Main Usage'
|
236 | , 'some main usage here'
|
237 | ].join('\n')
|
238 | , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n'
|
239 | , '- [My Module](#markdown-header-my-module)\n'
|
240 | , '\t- [API](#markdown-header-api)\n'
|
241 | , '\t\t- [Method One](#markdown-header-method-one)\n'
|
242 | , '\t\t- [Method Two](#markdown-header-method-two)\n'
|
243 | , '\t\t\t- [Main Usage](#markdown-header-main-usage)\n\n\n'
|
244 | ].join('')
|
245 | , 'bitbucket.org'
|
246 | )
|