Skip to content

Commit bc400ac

Browse files
fix: Refactor table tokens (#2166)
BREAKING CHANGE: - `table` tokens `header` property changed to contain an array of objects for each header cell with `text` and `tokens` properties. - `table` tokens `cells` property changed to `rows` and is an array of rows where each row contains an array of objects for each cell with `text` and `tokens` properties. v2: ```json { "type": "table", "align": [null, null], "raw": "| a | b |\n|---|---|\n| 1 | 2 |\n", "header": ["a", "b"], "cells": [["1", "2"]], "tokens": { "header": [ [{ "type": "text", "raw": "a", "text": "a" }], [{ "type": "text", "raw": "b", "text": "b" }] ], "cells": [[ [{ "type": "text", "raw": "1", "text": "1" }], [{ "type": "text", "raw": "2", "text": "2" }] ]] } } ``` v3: ```json { "type": "table", "align": [null, null], "raw": "| a | b |\n|---|---|\n| 1 | 2 |\n", "header": [ { "text": "a", "tokens": [{ "type": "text", "raw": "a", "text": "a" }] }, { "text": "b", "tokens": [{ "type": "text", "raw": "b", "text": "b" }] } ], "rows": [ { "text": "1", "tokens": [{ "type": "text", "raw": "1", "text": "1" }] }, { "text": "2", "tokens": [{ "type": "text", "raw": "2", "text": "2" }] } ] } ```
1 parent eb33d3b commit bc400ac

5 files changed

Lines changed: 113 additions & 74 deletions

File tree

src/Parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,22 @@ module.exports = class Parser {
103103
l2 = token.header.length;
104104
for (j = 0; j < l2; j++) {
105105
cell += this.renderer.tablecell(
106-
this.parseInline(token.tokens.header[j]),
106+
this.parseInline(token.header[j].tokens),
107107
{ header: true, align: token.align[j] }
108108
);
109109
}
110110
header += this.renderer.tablerow(cell);
111111

112112
body = '';
113-
l2 = token.cells.length;
113+
l2 = token.rows.length;
114114
for (j = 0; j < l2; j++) {
115-
row = token.tokens.cells[j];
115+
row = token.rows[j];
116116

117117
cell = '';
118118
l3 = row.length;
119119
for (k = 0; k < l3; k++) {
120120
cell += this.renderer.tablecell(
121-
this.parseInline(row[k]),
121+
this.parseInline(row[k].tokens),
122122
{ header: false, align: token.align[k] }
123123
);
124124
}

src/Tokenizer.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ module.exports = class Tokenizer {
352352
if (cap) {
353353
const item = {
354354
type: 'table',
355-
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
355+
header: splitCells(cap[1]).map(c => { return { text: c }; }),
356356
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
357-
cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
357+
rows: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
358358
};
359359

360360
if (item.header.length === item.align.length) {
@@ -374,32 +374,27 @@ module.exports = class Tokenizer {
374374
}
375375
}
376376

377-
l = item.cells.length;
377+
l = item.rows.length;
378378
for (i = 0; i < l; i++) {
379-
item.cells[i] = splitCells(item.cells[i], item.header.length);
379+
item.rows[i] = splitCells(item.rows[i], item.header.length).map(c => { return { text: c }; });
380380
}
381381

382382
// parse child tokens inside headers and cells
383-
item.tokens = {
384-
header: [],
385-
cells: []
386-
};
387383

388384
// header child tokens
389385
l = item.header.length;
390386
for (j = 0; j < l; j++) {
391-
item.tokens.header[j] = [];
392-
this.lexer.inlineTokens(item.header[j], item.tokens.header[j]);
387+
item.header[j].tokens = [];
388+
this.lexer.inlineTokens(item.header[j].text, item.header[j].tokens);
393389
}
394390

395391
// cell child tokens
396-
l = item.cells.length;
392+
l = item.rows.length;
397393
for (j = 0; j < l; j++) {
398-
row = item.cells[j];
399-
item.tokens.cells[j] = [];
394+
row = item.rows[j];
400395
for (k = 0; k < row.length; k++) {
401-
item.tokens.cells[j][k] = [];
402-
this.lexer.inlineTokens(row[k], item.tokens.cells[j][k]);
396+
row[k].tokens = [];
397+
this.lexer.inlineTokens(row[k].text, row[k].tokens);
403398
}
404399
}
405400

src/marked.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ marked.walkTokens = function(tokens, callback) {
260260
callback(token);
261261
switch (token.type) {
262262
case 'table': {
263-
for (const cell of token.tokens.header) {
264-
marked.walkTokens(cell, callback);
263+
for (const cell of token.header) {
264+
marked.walkTokens(cell.tokens, callback);
265265
}
266-
for (const row of token.tokens.cells) {
266+
for (const row of token.rows) {
267267
for (const cell of row) {
268-
marked.walkTokens(cell, callback);
268+
marked.walkTokens(cell.tokens, callback);
269269
}
270270
}
271271
break;

test/unit/Lexer-spec.js

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,30 @@ lheading 2
176176
`,
177177
tokens: [{
178178
type: 'table',
179-
header: ['a', 'b'],
180179
align: [null, null],
181-
cells: [['1', '2']],
182180
raw: '| a | b |\n|---|---|\n| 1 | 2 |\n',
183-
tokens: {
184-
header: [
185-
[{ type: 'text', raw: 'a', text: 'a' }],
186-
[{ type: 'text', raw: 'b', text: 'b' }]
187-
],
188-
cells: [[
189-
[{ type: 'text', raw: '1', text: '1' }],
190-
[{ type: 'text', raw: '2', text: '2' }]
191-
]]
192-
}
181+
header: [
182+
{
183+
text: 'a',
184+
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
185+
},
186+
{
187+
text: 'b',
188+
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
189+
}
190+
],
191+
rows: [
192+
[
193+
{
194+
text: '1',
195+
tokens: [{ type: 'text', raw: '1', text: '1' }]
196+
},
197+
{
198+
text: '2',
199+
tokens: [{ type: 'text', raw: '2', text: '2' }]
200+
}
201+
]
202+
]
193203
}]
194204
});
195205
});
@@ -203,22 +213,38 @@ lheading 2
203213
`,
204214
tokens: [{
205215
type: 'table',
206-
header: ['a', 'b', 'c'],
207216
align: ['left', 'center', 'right'],
208-
cells: [['1', '2', '3']],
209217
raw: '| a | b | c |\n|:--|:-:|--:|\n| 1 | 2 | 3 |\n',
210-
tokens: {
211-
header: [
212-
[{ type: 'text', raw: 'a', text: 'a' }],
213-
[{ type: 'text', raw: 'b', text: 'b' }],
214-
[{ type: 'text', raw: 'c', text: 'c' }]
215-
],
216-
cells: [[
217-
[{ type: 'text', raw: '1', text: '1' }],
218-
[{ type: 'text', raw: '2', text: '2' }],
219-
[{ type: 'text', raw: '3', text: '3' }]
220-
]]
221-
}
218+
header: [
219+
{
220+
text: 'a',
221+
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
222+
},
223+
{
224+
text: 'b',
225+
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
226+
},
227+
{
228+
text: 'c',
229+
tokens: [{ type: 'text', raw: 'c', text: 'c' }]
230+
}
231+
],
232+
rows: [
233+
[
234+
{
235+
text: '1',
236+
tokens: [{ type: 'text', raw: '1', text: '1' }]
237+
},
238+
{
239+
text: '2',
240+
tokens: [{ type: 'text', raw: '2', text: '2' }]
241+
},
242+
{
243+
text: '3',
244+
tokens: [{ type: 'text', raw: '3', text: '3' }]
245+
}
246+
]
247+
]
222248
}]
223249
});
224250
});
@@ -232,20 +258,30 @@ a | b
232258
`,
233259
tokens: [{
234260
type: 'table',
235-
header: ['a', 'b'],
236261
align: [null, null],
237-
cells: [['1', '2']],
238262
raw: 'a | b\n--|--\n1 | 2\n',
239-
tokens: {
240-
header: [
241-
[{ type: 'text', raw: 'a', text: 'a' }],
242-
[{ type: 'text', raw: 'b', text: 'b' }]
243-
],
244-
cells: [[
245-
[{ type: 'text', raw: '1', text: '1' }],
246-
[{ type: 'text', raw: '2', text: '2' }]
247-
]]
248-
}
263+
header: [
264+
{
265+
text: 'a',
266+
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
267+
},
268+
{
269+
text: 'b',
270+
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
271+
}
272+
],
273+
rows: [
274+
[
275+
{
276+
text: '1',
277+
tokens: [{ type: 'text', raw: '1', text: '1' }]
278+
},
279+
{
280+
text: '2',
281+
tokens: [{ type: 'text', raw: '2', text: '2' }]
282+
}
283+
]
284+
]
249285
}]
250286
});
251287
});

test/unit/Parser-spec.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,29 @@ describe('Parser', () => {
6868
await expectHtml({
6969
tokens: [{
7070
type: 'table',
71-
header: ['a', 'b'],
7271
align: ['left', 'right'],
73-
cells: [['1', '2']],
74-
tokens: {
75-
header: [
76-
[{ type: 'text', text: 'a' }],
77-
[{ type: 'text', text: 'b' }]
78-
],
79-
cells: [
80-
[
81-
[{ type: 'text', text: '1' }],
82-
[{ type: 'text', text: '2' }]
83-
]
72+
header: [
73+
{
74+
text: 'a',
75+
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
76+
},
77+
{
78+
text: 'b',
79+
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
80+
}
81+
],
82+
rows: [
83+
[
84+
{
85+
text: '1',
86+
tokens: [{ type: 'text', raw: '1', text: '1' }]
87+
},
88+
{
89+
text: '2',
90+
tokens: [{ type: 'text', raw: '2', text: '2' }]
91+
}
8492
]
85-
}
93+
]
8694
}],
8795
html: `
8896
<table>

0 commit comments

Comments
 (0)