-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmarkdown.js
132 lines (107 loc) · 3.2 KB
/
markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const os = require('os');
const renderLine = (ls, padding) => {
const spacing = ''.padEnd(padding, ' ');
const line = ls.join(`${spacing}|${spacing}`);
return `|${spacing}${line}${spacing}|`;
};
const renderCell = (cellValue, column) => {
// To include a pipe | as content within your cell, use a \ before the pipe:
cellValue = cellValue.split(/\\?\|/g).join('\\|');
const valueWidth = cellValue.length;
const width = column.width;
if (width <= valueWidth) {
return cellValue;
}
const spacingWidth = width - valueWidth;
const spacing = ''.padEnd(spacingWidth, ' ');
const align = column.align;
if (align === 'right') {
return spacing + cellValue;
}
if (align === 'center') {
const l = Math.round(spacingWidth * 0.5);
const r = spacingWidth - l;
const spacingL = ''.padEnd(l, ' ');
const spacingR = ''.padEnd(r, ' ');
return spacingL + cellValue + spacingR;
}
return cellValue + spacing;
};
const renderHyphen = (column) => {
const width = column.width;
const align = column.align;
if (align === 'right') {
return ':'.padStart(width, '-');
}
if (align === 'center') {
const hyphen = ''.padEnd(width - 2, '-');
return `:${hyphen}:`;
}
// default to left
return ':'.padEnd(width, '-');
};
const markdownGrid = (data) => {
const options = {
name: '',
padding: 1,
nullPlaceholder: '',
... data.options
};
const padding = options.padding;
// console.log(data, options);
const lines = [];
if (options.name) {
lines.push(`## ${options.name}`);
}
// init columns
const columns = data.columns.map((item) => {
if (typeof item === 'string') {
item = {
name: item
};
}
const column = {
... item
};
column.name = `${column.name}`;
if (typeof column.width !== 'number') {
column.width = column.name.length;
}
column.width = Math.max(column.width, 3);
return column;
});
// header
const headers = [];
columns.forEach((column) => {
headers.push(renderCell(column.name, column));
});
lines.push(renderLine(headers, padding));
const hyphens = [];
columns.forEach((column) => {
hyphens.push(renderHyphen(column));
});
lines.push(renderLine(hyphens, padding));
// rows
data.rows.forEach((row) => {
const cells = [];
columns.forEach((column, i) => {
let cellValue = '';
if (Array.isArray(row)) {
cellValue += row[i];
} else {
let v = row[column.id];
if (typeof v === 'undefined' || v === null) {
v = options.nullPlaceholder;
}
if (column.formatter) {
v = column.formatter(v, row, column);
}
cellValue += v;
}
cells.push(renderCell(cellValue, column));
});
lines.push(renderLine(cells, padding));
});
return lines.join(os.EOL) + os.EOL;
};
module.exports = markdownGrid;