(input)
| 1 | function tokenize(input) { |
| 2 | const tokens = []; |
| 3 | const length = input.length; |
| 4 | let index = 0; |
| 5 | let type = 'text'; |
| 6 | let value = ''; |
| 7 | let newline = true; |
| 8 | let prefix = ''; |
| 9 | let ordered = false; |
| 10 | |
| 11 | function flush() { |
| 12 | if (type === 'list') { |
| 13 | const previous = tokens[tokens.length - 1]; |
| 14 | if ( |
| 15 | previous && |
| 16 | previous.type === 'list' && |
| 17 | previous.ordered === ordered |
| 18 | ) { |
| 19 | previous.source += `\n${prefix} ${value}`; |
| 20 | previous.children.push({ |
| 21 | type: 'item', |
| 22 | source: value, |
| 23 | children: [{ type: 'text', source: value, value }], |
| 24 | }); |
| 25 | } else { |
| 26 | tokens.push({ |
| 27 | type: 'list', |
| 28 | source: `${prefix} ${value}`, |
| 29 | ordered, |
| 30 | children: [ |
| 31 | { |
| 32 | type: 'item', |
| 33 | source: value, |
| 34 | children: [{ type: 'text', source: value, value }], |
| 35 | }, |
| 36 | ], |
| 37 | }); |
| 38 | } |
| 39 | } else if (type === 'text') { |
| 40 | if (value) { |
| 41 | const previous = tokens[tokens.length - 1]; |
| 42 | if (previous && previous.type === 'text') { |
| 43 | previous.value += value; |
| 44 | } else { |
| 45 | tokens.push({ type, value }); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | value = ''; |
| 50 | } |
| 51 | |
| 52 | while (index < length) { |
| 53 | let current = input[index]; |
| 54 | let next = input[index + 1]; |
| 55 | if (current === '\n') { |
| 56 | if (type === 'text') { |
| 57 | value += current; |
| 58 | flush(); |
| 59 | prefix = ''; |
| 60 | } else if (type === 'list') { |
no test coverage detected