markdown-up 2.1.0 → 2.2.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/lib/data.js CHANGED
@@ -517,7 +517,7 @@ export function topData(data, count, categoryFields = null) {
517
517
 
518
518
 
519
519
  // Helper function to format labels
520
- export function formatValue(value, precision = null, datetime = null) {
520
+ export function formatValue(value, precision = null, datetime = null, trim = null) {
521
521
  if (value instanceof Date) {
522
522
  if (datetime === 'year') {
523
523
  // Round to nearest year
@@ -546,12 +546,13 @@ export function formatValue(value, precision = null, datetime = null) {
546
546
  return value.toISOString().replace(rDateCleanup, '');
547
547
  } else if (typeof value === 'number') {
548
548
  const numberFormat = value.toFixed(precision ?? defaultPrecision);
549
- return numberFormat.replace(rNumberCleanup, '');
549
+ return (trim ?? defaultTrim) ? numberFormat.replace(rNumberCleanup, '') : numberFormat;
550
550
  }
551
551
  return `${value}`;
552
552
  }
553
553
 
554
554
  const defaultPrecision = 2;
555
+ const defaultTrim = true;
555
556
  const rNumberCleanup = /\.0*$/;
556
557
  const rDateCleanup = /(?:(?:(?:-01)?T00:00)?:00)?\.\d\d\dZ$/;
557
558
 
package/lib/dataTable.js CHANGED
@@ -24,8 +24,8 @@ struct DataTable
24
24
  # The table's category fields
25
25
  optional string[len > 0] categories
26
26
 
27
- # The "categories" and "fields" to be rendered as Markdown text
28
- optional string[len > 0] markdown
27
+ # The field formatting for "categories" and "fields"
28
+ optional DataTableFieldFormat{len > 0} formats
29
29
 
30
30
  # The numeric formatting precision (default is 2)
31
31
  optional int(>= 0) precision
@@ -33,6 +33,26 @@ struct DataTable
33
33
  # The datetime format
34
34
  optional DataTableDatetimeFormat datetime
35
35
 
36
+ # If true, trim formatted values (default is true)
37
+ optional bool trim
38
+
39
+
40
+ # A data table field formatting model
41
+ struct DataTableFieldFormat
42
+
43
+ # If true, format the field as Markdown
44
+ optional bool markdown
45
+
46
+ # The field alignment
47
+ optional DataTableFieldAlignment align
48
+
49
+
50
+ # A field alignment
51
+ enum DataTableFieldAlignment
52
+ left
53
+ right
54
+ center
55
+
36
56
 
37
57
  # A datetime format
38
58
  enum DataTableDatetimeFormat
@@ -97,10 +117,11 @@ export function dataTableElements(data, dataTable = null, options = null) {
97
117
  }
98
118
 
99
119
  // Generate the data table's element model
100
- const markdown = (dataTable !== null ? (dataTable.markdown ?? null) : null);
101
- const markdownOptions = (markdown !== null && options !== null && 'urlFn' in options ? {'urlFn': options.urlFn} : null);
120
+ const fieldFormats = (dataTable !== null ? (dataTable.formats ?? null) : null);
121
+ const markdownOptions = (options !== null && 'urlFn' in options ? {'urlFn': options.urlFn} : null);
102
122
  const formatPrecision = (dataTable !== null ? (dataTable.precision ?? null) : null);
103
123
  const formatDatetime = (dataTable !== null ? (dataTable.datetime ?? null) : null);
124
+ const formatTrim = (dataTable !== null ? (dataTable.trim ?? null) : null);
104
125
  return {
105
126
  'html': 'table',
106
127
  'elem': [
@@ -108,8 +129,22 @@ export function dataTableElements(data, dataTable = null, options = null) {
108
129
  {
109
130
  'html': 'tr',
110
131
  'elem': [
111
- categories.map((field) => ({'html': 'th', 'elem': {'text': field}})),
112
- tableFields.map((field) => ({'html': 'th', 'elem': {'text': field}}))
132
+ categories.map((field) => {
133
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
134
+ return {
135
+ 'html': 'th',
136
+ 'attr': (fieldFormat !== null && 'align' in fieldFormat ? {'style': `text-align: ${fieldFormat.align}`} : null),
137
+ 'elem': {'text': field}
138
+ };
139
+ }),
140
+ tableFields.map((field) => {
141
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
142
+ return {
143
+ 'html': 'th',
144
+ 'attr': (fieldFormat !== null && 'align' in fieldFormat ? {'style': `text-align: ${fieldFormat.align}`} : null),
145
+ 'elem': {'text': field}
146
+ };
147
+ })
113
148
  ]
114
149
  },
115
150
 
@@ -128,17 +163,29 @@ export function dataTableElements(data, dataTable = null, options = null) {
128
163
  skip = (compareValues(value, rowPrev[field] ?? null) === 0);
129
164
  }
130
165
 
131
- const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
166
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
167
+ const fieldElements = fieldFormat !== null && fieldFormat.markdown
132
168
  ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
133
- : {'text': formatValue(value, formatPrecision, formatDatetime)};
134
- return {'html': 'td', 'elem': skip ? null : fieldElements};
169
+ : {'text': formatValue(value, formatPrecision, formatDatetime, formatTrim)};
170
+ return {
171
+ 'html': 'td',
172
+ 'attr': fieldFormat !== null && 'align' in fieldFormat
173
+ ? {'style': `text-align: ${fieldFormat.align}`} : null,
174
+ 'elem': skip ? null : fieldElements
175
+ };
135
176
  }),
136
177
  tableFields.map((field) => {
137
178
  const value = row[field] ?? null;
138
- const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
179
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
180
+ const fieldElements = fieldFormat !== null && fieldFormat.markdown
139
181
  ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
140
- : {'text': formatValue(value, formatPrecision, formatDatetime)};
141
- return {'html': 'td', 'elem': fieldElements};
182
+ : {'text': formatValue(value, formatPrecision, formatDatetime, formatTrim)};
183
+ return {
184
+ 'html': 'td',
185
+ 'attr': fieldFormat !== null && 'align' in fieldFormat
186
+ ? {'style': `text-align: ${fieldFormat.align}`} : null,
187
+ 'elem': fieldElements
188
+ };
142
189
  })
143
190
  ]
144
191
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "2.1.0",
4
+ "version": "2.2.1",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",