markdown-up 2.1.0 → 2.2.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/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 = true) {
521
521
  if (value instanceof Date) {
522
522
  if (datetime === 'year') {
523
523
  // Round to nearest year
@@ -546,7 +546,7 @@ 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 ? numberFormat.replace(rNumberCleanup, '') : numberFormat;
550
550
  }
551
551
  return `${value}`;
552
552
  }
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
@@ -34,6 +34,23 @@ struct DataTable
34
34
  optional DataTableDatetimeFormat datetime
35
35
 
36
36
 
37
+ # A data table field formatting model
38
+ struct DataTableFieldFormat
39
+
40
+ # If true, format the field as Markdown
41
+ optional bool markdown
42
+
43
+ # The field alignment
44
+ optional DataTableFieldAlignment align
45
+
46
+
47
+ # A field alignment
48
+ enum DataTableFieldAlignment
49
+ left
50
+ right
51
+ center
52
+
53
+
37
54
  # A datetime format
38
55
  enum DataTableDatetimeFormat
39
56
 
@@ -97,8 +114,8 @@ export function dataTableElements(data, dataTable = null, options = null) {
97
114
  }
98
115
 
99
116
  // 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);
117
+ const fieldFormats = (dataTable !== null ? (dataTable.formats ?? null) : null);
118
+ const markdownOptions = (options !== null && 'urlFn' in options ? {'urlFn': options.urlFn} : null);
102
119
  const formatPrecision = (dataTable !== null ? (dataTable.precision ?? null) : null);
103
120
  const formatDatetime = (dataTable !== null ? (dataTable.datetime ?? null) : null);
104
121
  return {
@@ -108,8 +125,22 @@ export function dataTableElements(data, dataTable = null, options = null) {
108
125
  {
109
126
  'html': 'tr',
110
127
  'elem': [
111
- categories.map((field) => ({'html': 'th', 'elem': {'text': field}})),
112
- tableFields.map((field) => ({'html': 'th', 'elem': {'text': field}}))
128
+ categories.map((field) => {
129
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
130
+ return {
131
+ 'html': 'th',
132
+ 'attr': (fieldFormat !== null && 'align' in fieldFormat ? {'style': `text-align: ${fieldFormat.align}`} : null),
133
+ 'elem': {'text': field}
134
+ };
135
+ }),
136
+ tableFields.map((field) => {
137
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
138
+ return {
139
+ 'html': 'th',
140
+ 'attr': (fieldFormat !== null && 'align' in fieldFormat ? {'style': `text-align: ${fieldFormat.align}`} : null),
141
+ 'elem': {'text': field}
142
+ };
143
+ })
113
144
  ]
114
145
  },
115
146
 
@@ -128,17 +159,29 @@ export function dataTableElements(data, dataTable = null, options = null) {
128
159
  skip = (compareValues(value, rowPrev[field] ?? null) === 0);
129
160
  }
130
161
 
131
- const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
162
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
163
+ const fieldElements = fieldFormat !== null && fieldFormat.markdown
132
164
  ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
133
- : {'text': formatValue(value, formatPrecision, formatDatetime)};
134
- return {'html': 'td', 'elem': skip ? null : fieldElements};
165
+ : {'text': formatValue(value, formatPrecision, formatDatetime, false)};
166
+ return {
167
+ 'html': 'td',
168
+ 'attr': fieldFormat !== null && 'align' in fieldFormat
169
+ ? {'style': `text-align: ${fieldFormat.align}`} : null,
170
+ 'elem': skip ? null : fieldElements
171
+ };
135
172
  }),
136
173
  tableFields.map((field) => {
137
174
  const value = row[field] ?? null;
138
- const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
175
+ const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
176
+ const fieldElements = fieldFormat !== null && fieldFormat.markdown
139
177
  ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
140
- : {'text': formatValue(value, formatPrecision, formatDatetime)};
141
- return {'html': 'td', 'elem': fieldElements};
178
+ : {'text': formatValue(value, formatPrecision, formatDatetime, false)};
179
+ return {
180
+ 'html': 'td',
181
+ 'attr': fieldFormat !== null && 'align' in fieldFormat
182
+ ? {'style': `text-align: ${fieldFormat.align}`} : null,
183
+ 'elem': fieldElements
184
+ };
142
185
  })
143
186
  ]
144
187
  };
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.0",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",