@yozora/tokenizer-table 2.0.0-alpha.0 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,14 +84,14 @@ so you can use `YozoraParser` / `GfmExParser` directly.
84
84
  registered in *YastParser* as a plugin-in before it can be used.
85
85
 
86
86
  ```typescript {4,9}
87
- import { DefaultYastParser } from '@yozora/core-parser'
87
+ import { DefaultParser } from '@yozora/core-parser'
88
88
  import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
89
89
  import TextTokenizer from '@yozora/tokenizer-text'
90
90
  import TableTokenizer from '@yozora/tokenizer-table'
91
91
 
92
- const parser = new DefaultYastParser()
93
- .useBlockFallbackTokenizer(new ParagraphTokenizer())
94
- .useInlineFallbackTokenizer(new TextTokenizer())
92
+ const parser = new DefaultParser()
93
+ .useFallbackTokenizer(new ParagraphTokenizer())
94
+ .useFallbackTokenizer(new TextTokenizer())
95
95
  .useTokenizer(new TableTokenizer())
96
96
 
97
97
  // parse source markdown content
@@ -227,7 +227,6 @@ Name | Type | Required | Default
227
227
  [@yozora/tokenizer-link]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link#readme
228
228
  [@yozora/tokenizer-link-reference]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link-reference#readme
229
229
  [@yozora/tokenizer-list]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list#readme
230
- [@yozora/tokenizer-list-item]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list-item#readme
231
230
  [@yozora/tokenizer-math]: https://github.com/yozorajs/yozora/tree/main/tokenizers/math#readme
232
231
  [@yozora/tokenizer-paragraph]: https://github.com/yozorajs/yozora/tree/main/tokenizers/paragraph#readme
233
232
  [@yozora/tokenizer-setext-heading]: https://github.com/yozorajs/yozora/tree/main/tokenizers/setext-heading#readme
@@ -287,7 +286,6 @@ Name | Type | Required | Default
287
286
  [doc-@yozora/tokenizer-definition]: https://yozora.guanghechen.com/docs/package/tokenizer-definition
288
287
  [doc-@yozora/tokenizer-link-reference]: https://yozora.guanghechen.com/docs/package/tokenizer-link-reference
289
288
  [doc-@yozora/tokenizer-list]: https://yozora.guanghechen.com/docs/package/tokenizer-list
290
- [doc-@yozora/tokenizer-list-item]: https://yozora.guanghechen.com/docs/package/tokenizer-list-item
291
289
  [doc-@yozora/tokenizer-math]: https://yozora.guanghechen.com/docs/package/tokenizer-math
292
290
  [doc-@yozora/tokenizer-paragraph]: https://yozora.guanghechen.com/docs/package/tokenizer-paragraph
293
291
  [doc-@yozora/tokenizer-setext-heading]: https://yozora.guanghechen.com/docs/package/tokenizer-setext-heading
package/lib/cjs/index.js CHANGED
@@ -107,7 +107,7 @@ const match = function (api) {
107
107
  end: coreTokenizer.calcEndYastNodePoint(nodePoints, nextIndex - 1),
108
108
  },
109
109
  columns,
110
- children: [row],
110
+ rows: [row],
111
111
  };
112
112
  return {
113
113
  token,
@@ -119,11 +119,10 @@ const match = function (api) {
119
119
  if (line.firstNonWhitespaceIndex >= line.endIndex) {
120
120
  return { status: 'notMatched' };
121
121
  }
122
- const tableToken = token;
123
- const row = calcTableRow(line, tableToken.columns);
122
+ const row = calcTableRow(line, token.columns);
124
123
  if (row == null)
125
124
  return { status: 'notMatched' };
126
- tableToken.children.push(row);
125
+ token.rows.push(row);
127
126
  return { status: 'opening', nextIndex: line.endIndex };
128
127
  }
129
128
  function calcTableRow(line, columns) {
@@ -174,7 +173,7 @@ const match = function (api) {
174
173
  _tokenizer,
175
174
  nodeType: ast.TableCellType,
176
175
  position: { start: startPoint, end: endPoint },
177
- children: phrasingContent == null ? [] : [phrasingContent],
176
+ contents: phrasingContent == null ? [] : [phrasingContent],
178
177
  };
179
178
  cells.push(cell);
180
179
  if (cells.length >= columns.length)
@@ -187,7 +186,7 @@ const match = function (api) {
187
186
  _tokenizer,
188
187
  nodeType: ast.TableCellType,
189
188
  position: { start: Object.assign({}, endPoint), end: Object.assign({}, endPoint) },
190
- children: [],
189
+ contents: [],
191
190
  };
192
191
  cells.push(cell);
193
192
  }
@@ -195,37 +194,18 @@ const match = function (api) {
195
194
  _tokenizer,
196
195
  nodeType: ast.TableRowType,
197
196
  position: { start: startPoint, end: endPoint },
198
- children: cells,
197
+ cells,
199
198
  };
200
199
  return row;
201
200
  }
202
201
  };
203
202
 
204
- const parse = () => ({
205
- parse: (token, children) => {
206
- let node;
207
- switch (token.nodeType) {
208
- case ast.TableType: {
209
- node = {
210
- type: ast.TableType,
211
- columns: token.columns,
212
- children: children,
213
- };
214
- break;
215
- }
216
- case ast.TableRowType: {
217
- node = {
218
- type: ast.TableRowType,
219
- children: children,
220
- };
221
- break;
222
- }
223
- case ast.TableCellType: {
224
- node = {
225
- type: ast.TableCellType,
226
- children: children,
227
- };
228
- for (const phrasingContent of node.children) {
203
+ const parse = api => ({
204
+ parse: tokens => tokens.map(token => {
205
+ const tableRows = token.rows.map((row) => {
206
+ const tableCells = row.cells.map((cell) => {
207
+ const contents = cell.contents ? api.parseBlockTokens(cell.contents) : [];
208
+ for (const phrasingContent of contents) {
229
209
  if (phrasingContent.type !== coreTokenizer.PhrasingContentType)
230
210
  continue;
231
211
  const nextContents = [];
@@ -244,13 +224,28 @@ const parse = () => ({
244
224
  }
245
225
  phrasingContent.contents = nextContents;
246
226
  }
247
- break;
248
- }
249
- default:
250
- return null;
251
- }
252
- return node;
253
- },
227
+ const tableCell = {
228
+ type: ast.TableCellType,
229
+ position: cell.position,
230
+ children: contents,
231
+ };
232
+ return tableCell;
233
+ });
234
+ const tableRow = {
235
+ type: ast.TableRowType,
236
+ position: row.position,
237
+ children: tableCells,
238
+ };
239
+ return tableRow;
240
+ });
241
+ const table = {
242
+ type: ast.TableType,
243
+ position: token.position,
244
+ columns: token.columns,
245
+ children: tableRows,
246
+ };
247
+ return table;
248
+ }),
254
249
  });
255
250
 
256
251
  const uniqueName = '@yozora/tokenizer-table';
package/lib/esm/index.js CHANGED
@@ -103,7 +103,7 @@ const match = function (api) {
103
103
  end: calcEndYastNodePoint(nodePoints, nextIndex - 1),
104
104
  },
105
105
  columns,
106
- children: [row],
106
+ rows: [row],
107
107
  };
108
108
  return {
109
109
  token,
@@ -115,11 +115,10 @@ const match = function (api) {
115
115
  if (line.firstNonWhitespaceIndex >= line.endIndex) {
116
116
  return { status: 'notMatched' };
117
117
  }
118
- const tableToken = token;
119
- const row = calcTableRow(line, tableToken.columns);
118
+ const row = calcTableRow(line, token.columns);
120
119
  if (row == null)
121
120
  return { status: 'notMatched' };
122
- tableToken.children.push(row);
121
+ token.rows.push(row);
123
122
  return { status: 'opening', nextIndex: line.endIndex };
124
123
  }
125
124
  function calcTableRow(line, columns) {
@@ -170,7 +169,7 @@ const match = function (api) {
170
169
  _tokenizer,
171
170
  nodeType: TableCellType,
172
171
  position: { start: startPoint, end: endPoint },
173
- children: phrasingContent == null ? [] : [phrasingContent],
172
+ contents: phrasingContent == null ? [] : [phrasingContent],
174
173
  };
175
174
  cells.push(cell);
176
175
  if (cells.length >= columns.length)
@@ -183,7 +182,7 @@ const match = function (api) {
183
182
  _tokenizer,
184
183
  nodeType: TableCellType,
185
184
  position: { start: Object.assign({}, endPoint), end: Object.assign({}, endPoint) },
186
- children: [],
185
+ contents: [],
187
186
  };
188
187
  cells.push(cell);
189
188
  }
@@ -191,37 +190,18 @@ const match = function (api) {
191
190
  _tokenizer,
192
191
  nodeType: TableRowType,
193
192
  position: { start: startPoint, end: endPoint },
194
- children: cells,
193
+ cells,
195
194
  };
196
195
  return row;
197
196
  }
198
197
  };
199
198
 
200
- const parse = () => ({
201
- parse: (token, children) => {
202
- let node;
203
- switch (token.nodeType) {
204
- case TableType: {
205
- node = {
206
- type: TableType,
207
- columns: token.columns,
208
- children: children,
209
- };
210
- break;
211
- }
212
- case TableRowType: {
213
- node = {
214
- type: TableRowType,
215
- children: children,
216
- };
217
- break;
218
- }
219
- case TableCellType: {
220
- node = {
221
- type: TableCellType,
222
- children: children,
223
- };
224
- for (const phrasingContent of node.children) {
199
+ const parse = api => ({
200
+ parse: tokens => tokens.map(token => {
201
+ const tableRows = token.rows.map((row) => {
202
+ const tableCells = row.cells.map((cell) => {
203
+ const contents = cell.contents ? api.parseBlockTokens(cell.contents) : [];
204
+ for (const phrasingContent of contents) {
225
205
  if (phrasingContent.type !== PhrasingContentType)
226
206
  continue;
227
207
  const nextContents = [];
@@ -240,13 +220,28 @@ const parse = () => ({
240
220
  }
241
221
  phrasingContent.contents = nextContents;
242
222
  }
243
- break;
244
- }
245
- default:
246
- return null;
247
- }
248
- return node;
249
- },
223
+ const tableCell = {
224
+ type: TableCellType,
225
+ position: cell.position,
226
+ children: contents,
227
+ };
228
+ return tableCell;
229
+ });
230
+ const tableRow = {
231
+ type: TableRowType,
232
+ position: row.position,
233
+ children: tableCells,
234
+ };
235
+ return tableRow;
236
+ });
237
+ const table = {
238
+ type: TableType,
239
+ position: token.position,
240
+ columns: token.columns,
241
+ children: tableRows,
242
+ };
243
+ return table;
244
+ }),
250
245
  });
251
246
 
252
247
  const uniqueName = '@yozora/tokenizer-table';
@@ -2,4 +2,4 @@ export { match as tableMatch } from './match';
2
2
  export { parse as tableParse } from './parse';
3
3
  export { TableTokenizer, TableTokenizer as default } from './tokenizer';
4
4
  export { uniqueName as TableTokenizerName } from './types';
5
- export type { IHookContext as ITableHookContext, ITableCellToken, ITableRowToken, ITableToken, ITokenizerProps as ITableTokenizerProps, } from './types';
5
+ export type { IThis as ITableHookContext, ITableCellToken, ITableRowToken, IToken as ITableToken, ITokenizerProps as ITableTokenizerProps, } from './types';
@@ -1,5 +1,5 @@
1
1
  import type { IMatchBlockHookCreator } from '@yozora/core-tokenizer';
2
- import type { IHookContext, IToken, T } from './types';
2
+ import type { IThis, IToken, T } from './types';
3
3
  /**
4
4
  * A table is an arrangement of data with rows and columns, consisting of
5
5
  * a single header row, a delimiter row separating the header from the data,
@@ -15,7 +15,7 @@ import type { IHookContext, IToken, T } from './types';
15
15
  * @see https://github.com/syntax-tree/mdast#tablerow
16
16
  * @see https://github.com/syntax-tree/mdast#tablecell
17
17
  */
18
- export declare const match: IMatchBlockHookCreator<T, IToken, IHookContext>;
18
+ export declare const match: IMatchBlockHookCreator<T, IToken, IThis>;
19
19
  /**
20
20
  * Find delimiter row
21
21
  *
@@ -1,3 +1,3 @@
1
1
  import type { IParseBlockHookCreator } from '@yozora/core-tokenizer';
2
- import type { IHookContext, INode, IToken, T } from './types';
3
- export declare const parse: IParseBlockHookCreator<T, IToken, INode, IHookContext>;
2
+ import type { INode, IThis, IToken, T } from './types';
3
+ export declare const parse: IParseBlockHookCreator<T, IToken, INode, IThis>;
@@ -1,14 +1,14 @@
1
1
  import type { IBlockTokenizer, IMatchBlockHookCreator, IParseBlockHookCreator } from '@yozora/core-tokenizer';
2
2
  import { BaseBlockTokenizer } from '@yozora/core-tokenizer';
3
- import type { IHookContext, INode, IToken, ITokenizerProps, T } from './types';
3
+ import type { INode, IThis, IToken, ITokenizerProps, T } from './types';
4
4
  /**
5
5
  * Lexical Analyzer for Table, table-row and table-cell.
6
6
  * @see https://github.github.com/gfm/#table
7
7
  * @see https://github.com/syntax-tree/mdast#tablerow
8
8
  * @see https://github.com/syntax-tree/mdast#tablecell
9
9
  */
10
- export declare class TableTokenizer extends BaseBlockTokenizer<T, IToken, INode, IHookContext> implements IBlockTokenizer<T, IToken, INode, IHookContext> {
10
+ export declare class TableTokenizer extends BaseBlockTokenizer<T, IToken, INode, IThis> implements IBlockTokenizer<T, IToken, INode, IThis> {
11
11
  constructor(props?: ITokenizerProps);
12
- readonly match: IMatchBlockHookCreator<T, IToken, IHookContext>;
13
- readonly parse: IParseBlockHookCreator<T, IToken, INode, IHookContext>;
12
+ readonly match: IMatchBlockHookCreator<T, IToken, IThis>;
13
+ readonly parse: IParseBlockHookCreator<T, IToken, INode, IThis>;
14
14
  }
@@ -1,10 +1,9 @@
1
- import type { ITable, ITableCell, ITableColumn, ITableRow, TableCellType, TableRowType, TableType } from '@yozora/ast';
2
- import type { IBaseBlockTokenizerProps, IPartialYastBlockToken, IPhrasingContentToken, ITokenizer, IYastBlockToken } from '@yozora/core-tokenizer';
3
- export declare type T = TableType | TableRowType | TableCellType;
4
- export declare type INode = ITable | ITableRow | ITableCell;
5
- export declare type IToken = ITableToken | ITableRowToken | ITableCellToken;
1
+ import type { ITable, ITableColumn, IYastNodePosition, TableCellType, TableRowType, TableType } from '@yozora/ast';
2
+ import type { IBaseBlockTokenizerProps, IPartialYastBlockToken, IPhrasingContentToken, ITokenizer } from '@yozora/core-tokenizer';
3
+ export declare type T = TableType;
4
+ export declare type INode = ITable;
6
5
  export declare const uniqueName = "@yozora/tokenizer-table";
7
- export interface ITableToken extends IPartialYastBlockToken<TableType> {
6
+ export interface IToken extends IPartialYastBlockToken<TableType> {
8
7
  /**
9
8
  * Table column configuration items
10
9
  */
@@ -12,19 +11,14 @@ export interface ITableToken extends IPartialYastBlockToken<TableType> {
12
11
  /**
13
12
  * Table rows
14
13
  */
15
- children: ITableRowToken[];
14
+ rows: ITableRowToken[];
16
15
  }
17
- export interface ITableRowToken extends IYastBlockToken<TableRowType> {
18
- /**
19
- * Table cells
20
- */
21
- children: ITableCellToken[];
16
+ export declare type IThis = ITokenizer;
17
+ export declare type ITokenizerProps = Partial<IBaseBlockTokenizerProps>;
18
+ export interface ITableRowToken extends IPartialYastBlockToken<TableRowType> {
19
+ cells: ITableCellToken[];
22
20
  }
23
- export interface ITableCellToken extends IYastBlockToken<TableCellType> {
24
- /**
25
- * Contents of table cell.
26
- */
27
- children: IPhrasingContentToken[];
21
+ export interface ITableCellToken extends IPartialYastBlockToken<TableCellType> {
22
+ position: IYastNodePosition;
23
+ contents: IPhrasingContentToken[];
28
24
  }
29
- export declare type IHookContext = ITokenizer;
30
- export declare type ITokenizerProps = Partial<IBaseBlockTokenizerProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-table",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "author": {
5
5
  "name": "guanghechen",
6
6
  "url": "https://github.com/guanghechen/"
@@ -35,9 +35,9 @@
35
35
  "test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
36
36
  },
37
37
  "dependencies": {
38
- "@yozora/ast": "^2.0.0-alpha.0",
39
- "@yozora/character": "^2.0.0-alpha.0",
40
- "@yozora/core-tokenizer": "^2.0.0-alpha.0"
38
+ "@yozora/ast": "^2.0.0-alpha.1",
39
+ "@yozora/character": "^2.0.0-alpha.1",
40
+ "@yozora/core-tokenizer": "^2.0.0-alpha.1"
41
41
  },
42
- "gitHead": "0171501339c49ffd02ed16a63447fa20a47a29a7"
42
+ "gitHead": "86202e1d2b03ccfc2ab030517d9d314f7aee7666"
43
43
  }