markdown-it-adv-table 0.1.0
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/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/adv-table.d.ts +12 -0
- package/dist/adv-table.js +29 -0
- package/dist/cell.d.ts +33 -0
- package/dist/cell.js +105 -0
- package/dist/csv-table.d.ts +11 -0
- package/dist/csv-table.js +86 -0
- package/dist/debug.d.ts +7 -0
- package/dist/debug.js +27 -0
- package/dist/fence.d.ts +8 -0
- package/dist/fence.js +97 -0
- package/dist/flat-table.d.ts +25 -0
- package/dist/flat-table.js +162 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +100 -0
- package/dist/lexer.d.ts +48 -0
- package/dist/lexer.js +153 -0
- package/dist/merge.d.ts +26 -0
- package/dist/merge.js +65 -0
- package/dist/table-builder.d.ts +32 -0
- package/dist/table-builder.js +117 -0
- package/dist/table.d.ts +101 -0
- package/dist/table.js +387 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +47 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yamavol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StateBlock } from "markdown-it/index.js";
|
|
2
|
+
import { TableAttr } from "./table.js";
|
|
3
|
+
/**
|
|
4
|
+
* AdvTableParser delegates parsing to the appropriate parser
|
|
5
|
+
* based on the format option specified.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AdvTableParser {
|
|
8
|
+
readonly attrs: TableAttr;
|
|
9
|
+
readonly params: string;
|
|
10
|
+
constructor(param: string);
|
|
11
|
+
parse(state: StateBlock, startLine: number, endLine: number): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CsvTableParser } from "./csv-table.js";
|
|
2
|
+
import { FlatTableParser } from "./flat-table.js";
|
|
3
|
+
import { TableSpec } from "./table.js";
|
|
4
|
+
/**
|
|
5
|
+
* AdvTableParser delegates parsing to the appropriate parser
|
|
6
|
+
* based on the format option specified.
|
|
7
|
+
*/
|
|
8
|
+
export class AdvTableParser {
|
|
9
|
+
attrs;
|
|
10
|
+
params;
|
|
11
|
+
constructor(param) {
|
|
12
|
+
this.params = param;
|
|
13
|
+
this.attrs = TableSpec.parseInfoString(this.params);
|
|
14
|
+
}
|
|
15
|
+
parse(state, startLine, endLine) {
|
|
16
|
+
if (this.attrs.format === "csv") {
|
|
17
|
+
const parser = CsvTableParser.new(this.params, ",");
|
|
18
|
+
parser.parse(state, startLine, endLine);
|
|
19
|
+
}
|
|
20
|
+
else if (this.attrs.format === "tsv") {
|
|
21
|
+
const parser = CsvTableParser.new(this.params, "\t");
|
|
22
|
+
parser.parse(state, startLine, endLine);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const parser = FlatTableParser.new(this.params, state, startLine, endLine);
|
|
26
|
+
parser.parse(state, startLine, endLine);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/cell.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type HAlign = "left" | "center" | "right";
|
|
2
|
+
/**
|
|
3
|
+
* A state machine to track cell position and its attributes
|
|
4
|
+
*/
|
|
5
|
+
export declare class CellState {
|
|
6
|
+
private static readonly _initialPos;
|
|
7
|
+
private _pos;
|
|
8
|
+
private _numCols;
|
|
9
|
+
private _colsAttr;
|
|
10
|
+
private _cells;
|
|
11
|
+
constructor(columns: number);
|
|
12
|
+
get numCols(): number;
|
|
13
|
+
get pos(): [number, number];
|
|
14
|
+
get row(): number;
|
|
15
|
+
set row(num: number);
|
|
16
|
+
get col(): number;
|
|
17
|
+
set col(num: number);
|
|
18
|
+
init(numCols: number): void;
|
|
19
|
+
setColWidth(col: number, width?: string): void;
|
|
20
|
+
setColAlign(col: number, align?: HAlign): void;
|
|
21
|
+
setColSpan(row: number, col: number, cspan: number): void;
|
|
22
|
+
setRowSpan(row: number, col: number, rspan: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* Return the next available cell
|
|
25
|
+
*/
|
|
26
|
+
next(): {
|
|
27
|
+
row: number;
|
|
28
|
+
col: number;
|
|
29
|
+
};
|
|
30
|
+
isUsed(row: number, col: number): boolean;
|
|
31
|
+
private markUsed;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
package/dist/cell.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A state machine to track cell position and its attributes
|
|
3
|
+
*/
|
|
4
|
+
export class CellState {
|
|
5
|
+
static _initialPos = [-1, -1];
|
|
6
|
+
_pos;
|
|
7
|
+
_numCols;
|
|
8
|
+
_colsAttr;
|
|
9
|
+
_cells;
|
|
10
|
+
constructor(columns) {
|
|
11
|
+
this._numCols = Math.max(columns, 1);
|
|
12
|
+
this._pos = [...CellState._initialPos];
|
|
13
|
+
this._colsAttr = Array(this._numCols).fill({});
|
|
14
|
+
this._cells = {};
|
|
15
|
+
}
|
|
16
|
+
get numCols() {
|
|
17
|
+
return this._numCols;
|
|
18
|
+
}
|
|
19
|
+
get pos() {
|
|
20
|
+
return this._pos;
|
|
21
|
+
}
|
|
22
|
+
get row() {
|
|
23
|
+
return this._pos[0];
|
|
24
|
+
}
|
|
25
|
+
set row(num) {
|
|
26
|
+
this._pos[0] = num;
|
|
27
|
+
}
|
|
28
|
+
get col() {
|
|
29
|
+
return this._pos[1];
|
|
30
|
+
}
|
|
31
|
+
set col(num) {
|
|
32
|
+
this._pos[1] = num;
|
|
33
|
+
}
|
|
34
|
+
init(numCols) {
|
|
35
|
+
this._numCols = numCols;
|
|
36
|
+
this._pos = [...CellState._initialPos];
|
|
37
|
+
this._cells = {};
|
|
38
|
+
}
|
|
39
|
+
setColWidth(col, width) {
|
|
40
|
+
this._colsAttr[col].width = width;
|
|
41
|
+
}
|
|
42
|
+
setColAlign(col, align) {
|
|
43
|
+
this._colsAttr[col].align = align;
|
|
44
|
+
}
|
|
45
|
+
setColSpan(row, col, cspan) {
|
|
46
|
+
const key = mapKey(row, col);
|
|
47
|
+
this._cells[key].colspan = cspan;
|
|
48
|
+
for (let c = col; c < col + cspan; c++) {
|
|
49
|
+
const key = mapKey(row, c);
|
|
50
|
+
const cell = this._cells[key];
|
|
51
|
+
cell.assigned = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
setRowSpan(row, col, rspan) {
|
|
55
|
+
const key = mapKey(row, col);
|
|
56
|
+
this._cells[key].rowspan = rspan;
|
|
57
|
+
for (let r = row; r < row + rspan; r++) {
|
|
58
|
+
const key = mapKey(r, col);
|
|
59
|
+
const cell = this._cells[key];
|
|
60
|
+
cell.assigned = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Return the next available cell
|
|
65
|
+
*/
|
|
66
|
+
next() {
|
|
67
|
+
if (this.row === -1) {
|
|
68
|
+
this.row = 0;
|
|
69
|
+
this.col = 0;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// skip reserved cells for cspan/rspan, and return next valid cell. WIP
|
|
73
|
+
while (this.isUsed(this.row, this.col)) {
|
|
74
|
+
this.col++;
|
|
75
|
+
if (this.col >= this._numCols) {
|
|
76
|
+
this.col = 0;
|
|
77
|
+
this.row++;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
this.markUsed(this.row, this.col);
|
|
82
|
+
return {
|
|
83
|
+
row: this.row,
|
|
84
|
+
col: this.col,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
isUsed(row, col) {
|
|
88
|
+
const key = mapKey(row, col);
|
|
89
|
+
return this._cells[key]?.assigned === true;
|
|
90
|
+
}
|
|
91
|
+
markUsed(row, col) {
|
|
92
|
+
const key = mapKey(row, col);
|
|
93
|
+
const map = this._cells[key];
|
|
94
|
+
if (map === undefined) {
|
|
95
|
+
this._cells[key] = { assigned: true };
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
map.assigned = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function mapKey(row, column) {
|
|
103
|
+
return `${row}-${column}`;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=cell.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StateBlock } from "markdown-it/index.js";
|
|
2
|
+
import { TableSpec } from "./table.js";
|
|
3
|
+
export declare class CsvTableParser {
|
|
4
|
+
private _tableSpec;
|
|
5
|
+
readonly delim: string;
|
|
6
|
+
constructor(tableSpec: TableSpec, delimitor?: string);
|
|
7
|
+
static new(params: string, delim: string): CsvTableParser;
|
|
8
|
+
parse(state: StateBlock, startLine: number, endLine: number): void;
|
|
9
|
+
static numCols(cells: string[][]): number;
|
|
10
|
+
static parsecsv(text: String, delim?: string): string[][];
|
|
11
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Lexer, unwrapLiteral } from "./lexer.js";
|
|
2
|
+
import { TableBuilder } from "./table-builder.js";
|
|
3
|
+
import { TableSpec } from "./table.js";
|
|
4
|
+
export class CsvTableParser {
|
|
5
|
+
_tableSpec;
|
|
6
|
+
delim;
|
|
7
|
+
constructor(tableSpec, delimitor = ",") {
|
|
8
|
+
this._tableSpec = tableSpec;
|
|
9
|
+
this.delim = delimitor;
|
|
10
|
+
}
|
|
11
|
+
static new(params, delim) {
|
|
12
|
+
const attr = TableSpec.parseInfoString(params);
|
|
13
|
+
const tablespec = new TableSpec(attr);
|
|
14
|
+
return new CsvTableParser(tablespec, delim);
|
|
15
|
+
}
|
|
16
|
+
parse(state, startLine, endLine) {
|
|
17
|
+
const lines = state.getLines(startLine, endLine, state.blkIndent, false);
|
|
18
|
+
const cells = CsvTableParser.parsecsv(lines, this.delim);
|
|
19
|
+
if (this._tableSpec.attr.cols === undefined) {
|
|
20
|
+
const numCols = CsvTableParser.numCols(cells);
|
|
21
|
+
this._tableSpec.colspecs.lazyInit(numCols);
|
|
22
|
+
}
|
|
23
|
+
const builder = new TableBuilder(state, this._tableSpec);
|
|
24
|
+
const md = state.md;
|
|
25
|
+
const istate = new md.inline.State("", md, state.env, []);
|
|
26
|
+
builder.startTable();
|
|
27
|
+
for (let row = 0; row < cells.length; row++) {
|
|
28
|
+
for (let col = 0; col < cells[row].length; col++) {
|
|
29
|
+
const itoken = istate.push("text", "", 0);
|
|
30
|
+
itoken.content = cells[row][col];
|
|
31
|
+
// manually assign inline token to disable being inline-processed.
|
|
32
|
+
builder.insertCell(state => {
|
|
33
|
+
const token = state.push("inline", "", 0);
|
|
34
|
+
token.content = "";
|
|
35
|
+
token.children = [itoken];
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
builder.endTable();
|
|
40
|
+
}
|
|
41
|
+
static numCols(cells) {
|
|
42
|
+
let colMax = 1;
|
|
43
|
+
for (let row = 0; row < cells.length; row++) {
|
|
44
|
+
colMax = Math.max(colMax, cells[row].length);
|
|
45
|
+
}
|
|
46
|
+
return colMax;
|
|
47
|
+
}
|
|
48
|
+
static parsecsv(text, delim = ",") {
|
|
49
|
+
const lines = text.split("\n");
|
|
50
|
+
let result = [];
|
|
51
|
+
for (const line of lines) {
|
|
52
|
+
// skip empty line
|
|
53
|
+
if (line.trim().length === 0) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const lexer = new Lexer(line);
|
|
57
|
+
const columns = [];
|
|
58
|
+
let cp = 0;
|
|
59
|
+
while (cp != Lexer.EOF) {
|
|
60
|
+
lexer.skipWhitespace();
|
|
61
|
+
cp = lexer.peek();
|
|
62
|
+
if (Lexer.cmp(cp, "\"")) {
|
|
63
|
+
let literal = lexer.consumeLiteral("\"");
|
|
64
|
+
literal = unwrapLiteral(literal, "\"");
|
|
65
|
+
columns.push(literal);
|
|
66
|
+
}
|
|
67
|
+
else if (Lexer.cmp(cp, delim)) {
|
|
68
|
+
lexer.skipWhitespace();
|
|
69
|
+
// if next letter is "," insert empty column
|
|
70
|
+
if (Lexer.cmp(lexer.peekNext(), delim)) {
|
|
71
|
+
columns.push("");
|
|
72
|
+
}
|
|
73
|
+
lexer.next();
|
|
74
|
+
}
|
|
75
|
+
else if (!Lexer.isEOF(cp)) {
|
|
76
|
+
columns.push(lexer.consumeRe(/^[^,]+/));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
result.push(columns);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/dist/debug.d.ts
ADDED
package/dist/debug.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
;
|
|
2
|
+
export function createDebug(namespace) {
|
|
3
|
+
const nsList = process.env.DEBUG?.split(",").map((s) => s.trim()) ?? [];
|
|
4
|
+
const isEnabled = nsList.some((ns) => {
|
|
5
|
+
// if (ns === "*") { // Intentionally disabled because this output
|
|
6
|
+
// return true; // should not be mixed with other libraries.
|
|
7
|
+
// }
|
|
8
|
+
if (ns === namespace) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
if (ns.endsWith("*")) {
|
|
12
|
+
return namespace.startsWith(ns.slice(0, -1));
|
|
13
|
+
}
|
|
14
|
+
return ns === namespace;
|
|
15
|
+
});
|
|
16
|
+
const debug = (...args) => {
|
|
17
|
+
if (isEnabled) {
|
|
18
|
+
const fmtArgs = args.map(arg => ` ${arg}`).join("");
|
|
19
|
+
process.stderr.write(`${fmtArgs}\n`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
debug.isEnabled = () => isEnabled;
|
|
23
|
+
return debug;
|
|
24
|
+
}
|
|
25
|
+
;
|
|
26
|
+
export const debug = createDebug("adv-table:d");
|
|
27
|
+
export default debug;
|
package/dist/fence.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { StateBlock } from "markdown-it/index.js";
|
|
2
|
+
import type { RuleBlock } from "markdown-it/lib/parser_block.mjs";
|
|
3
|
+
export type Parser = (info: string, state: StateBlock, startLine: number, endLine: number) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Parse fenced code block and if the name matches, the content is passed to
|
|
6
|
+
* the custom parser.
|
|
7
|
+
*/
|
|
8
|
+
export declare function fence_custom(names: string | string[], parser: Parser): RuleBlock;
|
package/dist/fence.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse fenced code block and if the name matches, the content is passed to
|
|
3
|
+
* the custom parser.
|
|
4
|
+
*/
|
|
5
|
+
export function fence_custom(names, parser) {
|
|
6
|
+
if (typeof names === "string") {
|
|
7
|
+
names = [names];
|
|
8
|
+
}
|
|
9
|
+
return (state, startLine, endLine, silent) => {
|
|
10
|
+
let pos = state.bMarks[startLine] + state.tShift[startLine];
|
|
11
|
+
let max = state.eMarks[startLine];
|
|
12
|
+
// if it's indented more than 3 spaces, it should be a code block
|
|
13
|
+
if (state.sCount[startLine] - state.blkIndent >= 4) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (pos + 3 > max) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const marker = state.src.charCodeAt(pos);
|
|
20
|
+
if (marker !== 0x7E /* ~ */ && marker !== 0x60 /* ` */) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
// scan marker length
|
|
24
|
+
let mem = pos;
|
|
25
|
+
pos = state.skipChars(pos, marker);
|
|
26
|
+
let len = pos - mem;
|
|
27
|
+
if (len < 3) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const markup = state.src.slice(mem, pos);
|
|
31
|
+
const params = state.src.slice(pos, max);
|
|
32
|
+
if (marker === 0x60 /* ` */) {
|
|
33
|
+
if (params.indexOf(String.fromCharCode(marker)) >= 0) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (!names.some(name => params.startsWith(name))) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
// Since start is found, we can report success here in validation mode
|
|
41
|
+
if (silent) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
// search end of block
|
|
45
|
+
let nextLine = startLine;
|
|
46
|
+
let haveEndMarker = false;
|
|
47
|
+
for (;;) {
|
|
48
|
+
nextLine++;
|
|
49
|
+
if (nextLine >= endLine) {
|
|
50
|
+
// unclosed block should be autoclosed by end of document.
|
|
51
|
+
// also block seems to be autoclosed by end of parent
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine];
|
|
55
|
+
max = state.eMarks[nextLine];
|
|
56
|
+
if (pos < max && state.sCount[nextLine] < state.blkIndent) {
|
|
57
|
+
// non-empty line with negative indent should stop the list:
|
|
58
|
+
// - ```
|
|
59
|
+
// test
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (state.src.charCodeAt(pos) !== marker) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (state.sCount[nextLine] - state.blkIndent >= 4) {
|
|
66
|
+
// closing fence should be indented less than 4 spaces
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
pos = state.skipChars(pos, marker);
|
|
70
|
+
// closing code fence must be at least as long as the opening one
|
|
71
|
+
if (pos - mem < len) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// make sure tail has spaces only
|
|
75
|
+
pos = state.skipSpaces(pos);
|
|
76
|
+
if (pos < max) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
haveEndMarker = true;
|
|
80
|
+
// found!
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
len = state.sCount[startLine];
|
|
84
|
+
state.line = nextLine + (haveEndMarker ? 1 : 0);
|
|
85
|
+
if (parser !== undefined) {
|
|
86
|
+
parser(params, state, startLine, nextLine);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const token = state.push("fence", "code", 0);
|
|
90
|
+
token.info = params;
|
|
91
|
+
token.content = state.getLines(startLine + 1, nextLine, len, true);
|
|
92
|
+
token.markup = markup;
|
|
93
|
+
token.map = [startLine, state.line];
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { StateBlock } from "markdown-it/index.js";
|
|
2
|
+
import { CellAttr, TableSpec } from "./table.js";
|
|
3
|
+
/**
|
|
4
|
+
* FlatTableParser parses the flat-table syntax.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FlatTableParser {
|
|
7
|
+
private _tableSpec;
|
|
8
|
+
constructor(tableSpec: TableSpec);
|
|
9
|
+
static new(params: string, state: StateBlock, startLine: number, endLine: number): FlatTableParser;
|
|
10
|
+
/**
|
|
11
|
+
* Parse custom table syntax into tokens.
|
|
12
|
+
*
|
|
13
|
+
* @param state markdown-it parser state
|
|
14
|
+
* @param startLine the first line which contains the table syntax.
|
|
15
|
+
* @param endLine the last line which contains the table syntax.
|
|
16
|
+
*/
|
|
17
|
+
parse(state: StateBlock, startLine: number, endLine: number): void;
|
|
18
|
+
getline(state: StateBlock, lineNumber: number): string;
|
|
19
|
+
static getCellIndent(state: StateBlock, line: string): number;
|
|
20
|
+
static isCellStart(line: string): boolean;
|
|
21
|
+
/** extract cellspec from line */
|
|
22
|
+
static cellspec(line: string): string;
|
|
23
|
+
static parseCellspec(line: string): CellAttr;
|
|
24
|
+
static inferColCount(state: StateBlock, lineStart: number, lineEnd: number): number;
|
|
25
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { TableBuilder } from "./table-builder.js";
|
|
2
|
+
import { TableSpec } from "./table.js";
|
|
3
|
+
/**
|
|
4
|
+
* FlatTableParser parses the flat-table syntax.
|
|
5
|
+
*/
|
|
6
|
+
export class FlatTableParser {
|
|
7
|
+
_tableSpec;
|
|
8
|
+
constructor(tableSpec) {
|
|
9
|
+
this._tableSpec = tableSpec;
|
|
10
|
+
}
|
|
11
|
+
static new(params, state, startLine, endLine) {
|
|
12
|
+
const attr = TableSpec.parseInfoString(params);
|
|
13
|
+
if (attr.cols === undefined) {
|
|
14
|
+
attr.cols = FlatTableParser.inferColCount(state, startLine, endLine).toString();
|
|
15
|
+
}
|
|
16
|
+
const tableSpec = new TableSpec(attr);
|
|
17
|
+
return new FlatTableParser(tableSpec);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Parse custom table syntax into tokens.
|
|
21
|
+
*
|
|
22
|
+
* @param state markdown-it parser state
|
|
23
|
+
* @param startLine the first line which contains the table syntax.
|
|
24
|
+
* @param endLine the last line which contains the table syntax.
|
|
25
|
+
*/
|
|
26
|
+
parse(state, startLine, endLine) {
|
|
27
|
+
const builder = new TableBuilder(state, this._tableSpec);
|
|
28
|
+
builder.startTable();
|
|
29
|
+
let currentLine = startLine;
|
|
30
|
+
while (currentLine < endLine) {
|
|
31
|
+
const line = state.getLines(currentLine, currentLine + 1, state.blkIndent, false);
|
|
32
|
+
// skip empty line
|
|
33
|
+
if (line.trim().length === 0) {
|
|
34
|
+
currentLine++;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
// cell starter
|
|
38
|
+
if (FlatTableParser.isCellStart(line)) {
|
|
39
|
+
// find cell content range
|
|
40
|
+
const cellStart = currentLine;
|
|
41
|
+
while (currentLine + 1 < endLine && !FlatTableParser.isCellStart(this.getline(state, currentLine + 1))) {
|
|
42
|
+
currentLine++;
|
|
43
|
+
}
|
|
44
|
+
const cellEnd = currentLine;
|
|
45
|
+
// parse cell content as document
|
|
46
|
+
const md = state.md;
|
|
47
|
+
const cellIndent = FlatTableParser.getCellIndent(state, line);
|
|
48
|
+
const cellSpec = FlatTableParser.cellspec(line);
|
|
49
|
+
const cellAttr = FlatTableParser.parseCellspec(cellSpec);
|
|
50
|
+
const cellContent = state.getLines(cellStart, cellEnd + 1, cellIndent, false).slice(cellSpec.length + cellIndent);
|
|
51
|
+
const cstate = new md.block.State(cellContent, md, state.env, []);
|
|
52
|
+
md.block.tokenize(cstate, 0, cellEnd + 1 - cellStart);
|
|
53
|
+
if (cstate.tokens.length === 3
|
|
54
|
+
&& cstate.tokens[0].type.startsWith("paragraph_open")
|
|
55
|
+
&& cstate.tokens[1].type.startsWith("inline")
|
|
56
|
+
&& cstate.tokens[2].type.startsWith("paragraph_close")) {
|
|
57
|
+
cstate.tokens[0].hidden = true;
|
|
58
|
+
cstate.tokens[2].hidden = true;
|
|
59
|
+
}
|
|
60
|
+
builder.insertCell(state => {
|
|
61
|
+
state.tokens.push(...cstate.tokens);
|
|
62
|
+
}, cellAttr);
|
|
63
|
+
currentLine++;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const shortline = line.length > 10 ? line.slice(0, 10) + "..." : line;
|
|
67
|
+
throw new Error(`parse error: invalid syntax: ${shortline}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
builder.endTable();
|
|
71
|
+
}
|
|
72
|
+
getline(state, lineNumber) {
|
|
73
|
+
return state.getLines(lineNumber, lineNumber + 1, state.blkIndent, false);
|
|
74
|
+
}
|
|
75
|
+
static getCellIndent(state, line) {
|
|
76
|
+
const cellStart = line.search(/\|/);
|
|
77
|
+
const numSpaces = line.slice(cellStart + 1).search(/\S|$/); // number of spaces between symbol and text
|
|
78
|
+
const userIndent = Math.max(0, Math.min(1 + numSpaces, 4));
|
|
79
|
+
return state.blkIndent + userIndent;
|
|
80
|
+
}
|
|
81
|
+
static isCellStart(line) {
|
|
82
|
+
if (line.startsWith("|"))
|
|
83
|
+
return true;
|
|
84
|
+
if (FlatTableParser.cellspec(line).length > 0)
|
|
85
|
+
return true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
/** extract cellspec from line */
|
|
89
|
+
static cellspec(line) {
|
|
90
|
+
const match = /^((r\d+|c\d+|\<|\^|\>|h)*)\|/.exec(line);
|
|
91
|
+
if (match === null) {
|
|
92
|
+
return "";
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return match[1];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
static parseCellspec(line) {
|
|
99
|
+
let pos = 0;
|
|
100
|
+
const attr = {};
|
|
101
|
+
while (pos < line.length) {
|
|
102
|
+
const char = line[pos];
|
|
103
|
+
if (char === undefined) {
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
else if (char === "^") {
|
|
107
|
+
consume();
|
|
108
|
+
attr.align = "center";
|
|
109
|
+
}
|
|
110
|
+
else if (char === "<") {
|
|
111
|
+
consume();
|
|
112
|
+
attr.align = "left";
|
|
113
|
+
}
|
|
114
|
+
else if (char === ">") {
|
|
115
|
+
consume();
|
|
116
|
+
attr.align = "right";
|
|
117
|
+
}
|
|
118
|
+
else if (char === "r") {
|
|
119
|
+
consume();
|
|
120
|
+
attr.rowspan = parseInt(consumeNumber());
|
|
121
|
+
}
|
|
122
|
+
else if (char === "c") {
|
|
123
|
+
consume();
|
|
124
|
+
attr.colspan = parseInt(consumeNumber());
|
|
125
|
+
}
|
|
126
|
+
else if (char === "h") {
|
|
127
|
+
consume();
|
|
128
|
+
attr.header = true;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
consume();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return attr;
|
|
135
|
+
function consume() {
|
|
136
|
+
return line[pos++];
|
|
137
|
+
}
|
|
138
|
+
function consumeNumber() {
|
|
139
|
+
const match = line.slice(pos).match(/^\d+/);
|
|
140
|
+
if (!match)
|
|
141
|
+
throw new Error("cannot consume number");
|
|
142
|
+
pos += match[0].length;
|
|
143
|
+
return match[0];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
static inferColCount(state, lineStart, lineEnd) {
|
|
147
|
+
// TBD: does not handle colspan, rowspan
|
|
148
|
+
let contLen = 0;
|
|
149
|
+
let contLenMax = 0;
|
|
150
|
+
for (let line = lineStart; line < lineEnd; line++) {
|
|
151
|
+
const linetext = state.getLines(line, line + 1, state.blkIndent, false);
|
|
152
|
+
if (FlatTableParser.isCellStart(linetext)) {
|
|
153
|
+
contLen++;
|
|
154
|
+
contLenMax = Math.max(contLenMax, contLen);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
contLen = 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return contLenMax;
|
|
161
|
+
}
|
|
162
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PluginWithOptions } from "markdown-it";
|
|
2
|
+
import { Parser } from "./fence.js";
|
|
3
|
+
export interface PluginOption {
|
|
4
|
+
names: {
|
|
5
|
+
adv: string;
|
|
6
|
+
flat: string;
|
|
7
|
+
csv: string;
|
|
8
|
+
tsv: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface PluginOptionSingle {
|
|
12
|
+
name: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function createParser(option: PluginOption): Parser;
|
|
15
|
+
export declare const advTable: PluginWithOptions<PluginOption>;
|
|
16
|
+
export declare const flatTable: PluginWithOptions<PluginOptionSingle>;
|
|
17
|
+
export declare const csvTable: PluginWithOptions<PluginOptionSingle>;
|
|
18
|
+
export declare const tsvTable: PluginWithOptions<PluginOptionSingle>;
|
|
19
|
+
export default advTable;
|