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" }] } ] } ```
This commit is contained in:
parent
eb33d3b3a9
commit
bc400ac789
@ -103,22 +103,22 @@ module.exports = class Parser {
|
|||||||
l2 = token.header.length;
|
l2 = token.header.length;
|
||||||
for (j = 0; j < l2; j++) {
|
for (j = 0; j < l2; j++) {
|
||||||
cell += this.renderer.tablecell(
|
cell += this.renderer.tablecell(
|
||||||
this.parseInline(token.tokens.header[j]),
|
this.parseInline(token.header[j].tokens),
|
||||||
{ header: true, align: token.align[j] }
|
{ header: true, align: token.align[j] }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
header += this.renderer.tablerow(cell);
|
header += this.renderer.tablerow(cell);
|
||||||
|
|
||||||
body = '';
|
body = '';
|
||||||
l2 = token.cells.length;
|
l2 = token.rows.length;
|
||||||
for (j = 0; j < l2; j++) {
|
for (j = 0; j < l2; j++) {
|
||||||
row = token.tokens.cells[j];
|
row = token.rows[j];
|
||||||
|
|
||||||
cell = '';
|
cell = '';
|
||||||
l3 = row.length;
|
l3 = row.length;
|
||||||
for (k = 0; k < l3; k++) {
|
for (k = 0; k < l3; k++) {
|
||||||
cell += this.renderer.tablecell(
|
cell += this.renderer.tablecell(
|
||||||
this.parseInline(row[k]),
|
this.parseInline(row[k].tokens),
|
||||||
{ header: false, align: token.align[k] }
|
{ header: false, align: token.align[k] }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -352,9 +352,9 @@ module.exports = class Tokenizer {
|
|||||||
if (cap) {
|
if (cap) {
|
||||||
const item = {
|
const item = {
|
||||||
type: 'table',
|
type: 'table',
|
||||||
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
|
header: splitCells(cap[1]).map(c => { return { text: c }; }),
|
||||||
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
|
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
|
||||||
cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
|
rows: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
|
||||||
};
|
};
|
||||||
|
|
||||||
if (item.header.length === item.align.length) {
|
if (item.header.length === item.align.length) {
|
||||||
@ -374,32 +374,27 @@ module.exports = class Tokenizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
l = item.cells.length;
|
l = item.rows.length;
|
||||||
for (i = 0; i < l; i++) {
|
for (i = 0; i < l; i++) {
|
||||||
item.cells[i] = splitCells(item.cells[i], item.header.length);
|
item.rows[i] = splitCells(item.rows[i], item.header.length).map(c => { return { text: c }; });
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse child tokens inside headers and cells
|
// parse child tokens inside headers and cells
|
||||||
item.tokens = {
|
|
||||||
header: [],
|
|
||||||
cells: []
|
|
||||||
};
|
|
||||||
|
|
||||||
// header child tokens
|
// header child tokens
|
||||||
l = item.header.length;
|
l = item.header.length;
|
||||||
for (j = 0; j < l; j++) {
|
for (j = 0; j < l; j++) {
|
||||||
item.tokens.header[j] = [];
|
item.header[j].tokens = [];
|
||||||
this.lexer.inlineTokens(item.header[j], item.tokens.header[j]);
|
this.lexer.inlineTokens(item.header[j].text, item.header[j].tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
// cell child tokens
|
// cell child tokens
|
||||||
l = item.cells.length;
|
l = item.rows.length;
|
||||||
for (j = 0; j < l; j++) {
|
for (j = 0; j < l; j++) {
|
||||||
row = item.cells[j];
|
row = item.rows[j];
|
||||||
item.tokens.cells[j] = [];
|
|
||||||
for (k = 0; k < row.length; k++) {
|
for (k = 0; k < row.length; k++) {
|
||||||
item.tokens.cells[j][k] = [];
|
row[k].tokens = [];
|
||||||
this.lexer.inlineTokens(row[k], item.tokens.cells[j][k]);
|
this.lexer.inlineTokens(row[k].text, row[k].tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,12 +260,12 @@ marked.walkTokens = function(tokens, callback) {
|
|||||||
callback(token);
|
callback(token);
|
||||||
switch (token.type) {
|
switch (token.type) {
|
||||||
case 'table': {
|
case 'table': {
|
||||||
for (const cell of token.tokens.header) {
|
for (const cell of token.header) {
|
||||||
marked.walkTokens(cell, callback);
|
marked.walkTokens(cell.tokens, callback);
|
||||||
}
|
}
|
||||||
for (const row of token.tokens.cells) {
|
for (const row of token.rows) {
|
||||||
for (const cell of row) {
|
for (const cell of row) {
|
||||||
marked.walkTokens(cell, callback);
|
marked.walkTokens(cell.tokens, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -176,20 +176,30 @@ lheading 2
|
|||||||
`,
|
`,
|
||||||
tokens: [{
|
tokens: [{
|
||||||
type: 'table',
|
type: 'table',
|
||||||
header: ['a', 'b'],
|
|
||||||
align: [null, null],
|
align: [null, null],
|
||||||
cells: [['1', '2']],
|
|
||||||
raw: '| a | b |\n|---|---|\n| 1 | 2 |\n',
|
raw: '| a | b |\n|---|---|\n| 1 | 2 |\n',
|
||||||
tokens: {
|
header: [
|
||||||
header: [
|
{
|
||||||
[{ type: 'text', raw: 'a', text: 'a' }],
|
text: 'a',
|
||||||
[{ type: 'text', raw: 'b', text: 'b' }]
|
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
|
||||||
],
|
},
|
||||||
cells: [[
|
{
|
||||||
[{ type: 'text', raw: '1', text: '1' }],
|
text: 'b',
|
||||||
[{ type: 'text', raw: '2', text: '2' }]
|
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' }]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -203,22 +213,38 @@ lheading 2
|
|||||||
`,
|
`,
|
||||||
tokens: [{
|
tokens: [{
|
||||||
type: 'table',
|
type: 'table',
|
||||||
header: ['a', 'b', 'c'],
|
|
||||||
align: ['left', 'center', 'right'],
|
align: ['left', 'center', 'right'],
|
||||||
cells: [['1', '2', '3']],
|
|
||||||
raw: '| a | b | c |\n|:--|:-:|--:|\n| 1 | 2 | 3 |\n',
|
raw: '| a | b | c |\n|:--|:-:|--:|\n| 1 | 2 | 3 |\n',
|
||||||
tokens: {
|
header: [
|
||||||
header: [
|
{
|
||||||
[{ type: 'text', raw: 'a', text: 'a' }],
|
text: 'a',
|
||||||
[{ type: 'text', raw: 'b', text: 'b' }],
|
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
|
||||||
[{ type: 'text', raw: 'c', text: 'c' }]
|
},
|
||||||
],
|
{
|
||||||
cells: [[
|
text: 'b',
|
||||||
[{ type: 'text', raw: '1', text: '1' }],
|
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
|
||||||
[{ type: 'text', raw: '2', text: '2' }],
|
},
|
||||||
[{ type: 'text', raw: '3', text: '3' }]
|
{
|
||||||
]]
|
text: 'c',
|
||||||
}
|
tokens: [{ type: 'text', raw: 'c', text: 'c' }]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
rows: [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
text: '1',
|
||||||
|
tokens: [{ type: 'text', raw: '1', text: '1' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: '2',
|
||||||
|
tokens: [{ type: 'text', raw: '2', text: '2' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: '3',
|
||||||
|
tokens: [{ type: 'text', raw: '3', text: '3' }]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -232,20 +258,30 @@ a | b
|
|||||||
`,
|
`,
|
||||||
tokens: [{
|
tokens: [{
|
||||||
type: 'table',
|
type: 'table',
|
||||||
header: ['a', 'b'],
|
|
||||||
align: [null, null],
|
align: [null, null],
|
||||||
cells: [['1', '2']],
|
|
||||||
raw: 'a | b\n--|--\n1 | 2\n',
|
raw: 'a | b\n--|--\n1 | 2\n',
|
||||||
tokens: {
|
header: [
|
||||||
header: [
|
{
|
||||||
[{ type: 'text', raw: 'a', text: 'a' }],
|
text: 'a',
|
||||||
[{ type: 'text', raw: 'b', text: 'b' }]
|
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
|
||||||
],
|
},
|
||||||
cells: [[
|
{
|
||||||
[{ type: 'text', raw: '1', text: '1' }],
|
text: 'b',
|
||||||
[{ type: 'text', raw: '2', text: '2' }]
|
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' }]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -68,21 +68,29 @@ describe('Parser', () => {
|
|||||||
await expectHtml({
|
await expectHtml({
|
||||||
tokens: [{
|
tokens: [{
|
||||||
type: 'table',
|
type: 'table',
|
||||||
header: ['a', 'b'],
|
|
||||||
align: ['left', 'right'],
|
align: ['left', 'right'],
|
||||||
cells: [['1', '2']],
|
header: [
|
||||||
tokens: {
|
{
|
||||||
header: [
|
text: 'a',
|
||||||
[{ type: 'text', text: 'a' }],
|
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
|
||||||
[{ type: 'text', text: 'b' }]
|
},
|
||||||
],
|
{
|
||||||
cells: [
|
text: 'b',
|
||||||
[
|
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
|
||||||
[{ type: 'text', text: '1' }],
|
}
|
||||||
[{ type: 'text', text: '2' }]
|
],
|
||||||
]
|
rows: [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
text: '1',
|
||||||
|
tokens: [{ type: 'text', raw: '1', text: '1' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: '2',
|
||||||
|
tokens: [{ type: 'text', raw: '2', text: '2' }]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
]
|
||||||
}],
|
}],
|
||||||
html: `
|
html: `
|
||||||
<table>
|
<table>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user