markdown-up 3.12.1 → 4.0.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "3.12.1",
4
+ "version": "4.0.0",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -22,15 +22,16 @@
22
22
  "test": "node --test test/"
23
23
  },
24
24
  "dependencies": {
25
- "bare-script": "~3.8",
25
+ "bare-script": "~4.0",
26
26
  "element-model": "~1.2",
27
- "markdown-model": "~2.2",
28
27
  "schema-markdown": "~1.2"
29
28
  },
30
29
  "devDependencies": {
31
- "c8": "~10.1",
32
- "eslint": "~9.39",
30
+ "@eslint/js": "~10.0",
31
+ "c8": "~11.0",
32
+ "eslint": "~10.1",
33
+ "globals": "~17.4",
33
34
  "jsdoc": "~4.0",
34
- "jsdom": "~27.1"
35
+ "jsdom": "~29.0"
35
36
  }
36
37
  }
package/lib/dataTable.js DELETED
@@ -1,219 +0,0 @@
1
- // Licensed under the MIT License
2
- // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
-
4
- /** @module lib/dataTable */
5
-
6
- import {formatValue} from './dataUtil.js';
7
- import {markdownElements} from 'markdown-model/lib/elements.js';
8
- import {parseMarkdown} from 'markdown-model/lib/parser.js';
9
- import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
10
- import {validateType} from 'schema-markdown/lib/schema.js';
11
- import {valueCompare} from 'bare-script/lib/value.js';
12
-
13
-
14
- // The data table model's Schema Markdown
15
- export const dataTableTypes = parseSchemaMarkdown(`\
16
- group "Data Table"
17
-
18
-
19
- # A data table model
20
- struct DataTable
21
-
22
- # The table's fields
23
- optional string[len > 0] fields
24
-
25
- # The table's category fields
26
- optional string[len > 0] categories
27
-
28
- # The field formatting for "categories" and "fields"
29
- optional DataTableFieldFormat{len > 0} formats
30
-
31
- # The numeric formatting precision (default is 2)
32
- optional int(>= 0) precision
33
-
34
- # The datetime format
35
- optional DataTableDatetimeFormat datetime
36
-
37
- # If true, trim formatted values (default is true)
38
- optional bool trim
39
-
40
-
41
- # A data table field formatting model
42
- struct DataTableFieldFormat
43
-
44
- # The field alignment
45
- optional DataTableFieldAlignment align
46
-
47
- # If true, don't wrap text
48
- optional bool nowrap
49
-
50
- # If true, format the field as Markdown
51
- optional bool markdown
52
-
53
- # The field header (default is the field name)
54
- optional string header
55
-
56
-
57
- # A field alignment
58
- enum DataTableFieldAlignment
59
- left
60
- right
61
- center
62
-
63
-
64
- # A datetime format
65
- enum DataTableDatetimeFormat
66
-
67
- # ISO datetime year format
68
- year
69
-
70
- # ISO datetime month format
71
- month
72
-
73
- # ISO datetime day format
74
- day
75
- `);
76
-
77
-
78
- /**
79
- * Validate a data table model
80
- *
81
- * @param {Object} dataTable - The
82
- * [data table model]{@link https://craigahobbs.github.io/bare-script/library/model.html#var.vName='DataTable'}
83
- * @returns {Object}
84
- * @throws [ValidationError]{@link https://craigahobbs.github.io/schema-markdown-js/module-lib_schema.ValidationError.html}
85
- */
86
- export function validateDataTable(dataTable) {
87
- return validateType(dataTableTypes, 'DataTable', dataTable);
88
- }
89
-
90
-
91
- /**
92
- * The data table options object
93
- *
94
- * @typedef {Object} DataTableOptions
95
- * @property {?function} [copyFn] - The
96
- * [Markdown copy function]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~CopyFn}
97
- * @property {?function} [urlFn] - The
98
- * [Markdown URL modifier function]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~URLFn}
99
- */
100
-
101
-
102
- /**
103
- * Render a data table
104
- *
105
- * @param {Object[]} data - The data array
106
- * @param {?Object} [dataTable = null] - The
107
- * [data table model]{@link https://craigahobbs.github.io/bare-script/library/model.html#var.vName='DataTable'}
108
- * @param {?Object} [options = null] - The [data table options]{@link module:lib/dataTable~DataTableOptions}
109
- * @returns {Object} The data table [element model]{@link https://github.com/craigahobbs/element-model#readme}
110
- */
111
- export function dataTableElements(data, dataTable = null, options = null) {
112
- // Compute the table's field names
113
- const categories = (dataTable !== null ? (dataTable.categories ?? []) : []);
114
- let tableFields = (dataTable !== null ? (dataTable.fields ?? null) : null);
115
- if (tableFields === null) {
116
- const allFields = new Set();
117
- for (const row of data) {
118
- for (const fieldName of Object.keys(row)) {
119
- allFields.add(fieldName);
120
- }
121
- }
122
- tableFields = Array.from(allFields.values());
123
- if (categories !== null) {
124
- tableFields = tableFields.filter((key) => categories.indexOf(key) === -1);
125
- }
126
- }
127
-
128
- // Generate the data table's element model
129
- const fieldFormats = (dataTable !== null ? (dataTable.formats ?? null) : null);
130
- const markdownOptions = {
131
- 'copyFn': (options !== null ? options.copyFn : null) ?? null,
132
- 'urlFn': (options !== null ? options.urlFn : null) ?? null
133
- };
134
- const formatPrecision = (dataTable !== null ? (dataTable.precision ?? null) : null);
135
- const formatDatetime = (dataTable !== null ? (dataTable.datetime ?? null) : null);
136
- const formatTrim = (dataTable !== null ? (dataTable.trim ?? null) : null);
137
- return {
138
- 'html': 'table',
139
- 'elem': [
140
- // Table header
141
- {
142
- 'html': 'tr',
143
- 'elem': [
144
- categories.map((field) => {
145
- const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
146
- const formatHeader = (fieldFormat !== null ? (fieldFormat.header ?? null) : null);
147
- return {
148
- 'html': 'th',
149
- 'attr': createFieldAttr(fieldFormat),
150
- 'elem': {'text': formatHeader ?? field}
151
- };
152
- }),
153
- tableFields.map((field) => {
154
- const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
155
- const formatHeader = (fieldFormat !== null ? (fieldFormat.header ?? null) : null);
156
- return {
157
- 'html': 'th',
158
- 'attr': createFieldAttr(fieldFormat),
159
- 'elem': {'text': formatHeader ?? field}
160
- };
161
- })
162
- ]
163
- },
164
-
165
- // Table data
166
- data.map((row, ixRow) => {
167
- const rowPrev = ixRow > 0 ? data[ixRow - 1] : null;
168
- let skip = rowPrev !== null;
169
- return {
170
- 'html': 'tr',
171
- 'elem': [
172
- categories.map((field) => {
173
- const value = row[field] ?? null;
174
-
175
- // Skip this value?
176
- if (skip) {
177
- skip = (valueCompare(value, rowPrev[field] ?? null) === 0);
178
- }
179
-
180
- const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
181
- const fieldElements = fieldFormat !== null && fieldFormat.markdown
182
- ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
183
- : {'text': formatValue(value, formatPrecision, formatDatetime, formatTrim)};
184
- return {
185
- 'html': 'td',
186
- 'attr': createFieldAttr(fieldFormat),
187
- 'elem': (skip ? null : fieldElements)
188
- };
189
- }),
190
- tableFields.map((field) => {
191
- const value = row[field] ?? null;
192
- const fieldFormat = (fieldFormats !== null ? (fieldFormats[field] ?? null) : null);
193
- const fieldElements = fieldFormat !== null && fieldFormat.markdown
194
- ? markdownElements(parseMarkdown(`${value}`), markdownOptions)
195
- : {'text': formatValue(value, formatPrecision, formatDatetime, formatTrim)};
196
- return {
197
- 'html': 'td',
198
- 'attr': createFieldAttr(fieldFormat),
199
- 'elem': fieldElements
200
- };
201
- })
202
- ]
203
- };
204
- })
205
- ]
206
- };
207
- }
208
-
209
-
210
- function createFieldAttr(fieldFormat) {
211
- const fieldStyles = [];
212
- if (fieldFormat !== null && 'align' in fieldFormat) {
213
- fieldStyles.push(`text-align: ${fieldFormat.align}`);
214
- }
215
- if (fieldFormat !== null && fieldFormat.nowrap) {
216
- fieldStyles.push('white-space: nowrap');
217
- }
218
- return (fieldStyles.length ? {'style': fieldStyles.join('; ')} : null);
219
- }
package/lib/dataUtil.js DELETED
@@ -1,72 +0,0 @@
1
- // Licensed under the MIT License
2
- // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
-
4
- import {valueString, valueType} from 'bare-script/lib/value.js';
5
-
6
-
7
- // Helper function to format labels
8
- export function formatValue(value, precision = null, datetime = null, trim = null) {
9
- const type = valueType(value);
10
- if (type === 'datetime') {
11
- if (datetime === 'year') {
12
- // Round to nearest year
13
- let valueRounded = value;
14
- if (value.getMonth() > 5) {
15
- valueRounded = new Date(value);
16
- valueRounded.setFullYear(value.getFullYear() + 1);
17
- }
18
-
19
- const isoFormat = valueString(valueRounded);
20
- return isoFormat.slice(0, isoFormat.indexOf('T') - 6);
21
- } else if (datetime === 'month') {
22
- // Round to the nearest month
23
- let valueRounded = value;
24
- if (value.getDate() > 15) {
25
- valueRounded = new Date(value);
26
- valueRounded.setMonth(value.getMonth() + 1);
27
- }
28
-
29
- const isoFormat = valueString(valueRounded);
30
- return isoFormat.slice(0, isoFormat.indexOf('T') - 3);
31
- } else if (datetime === 'day') {
32
- const isoFormat = valueString(value);
33
- return isoFormat.slice(0, isoFormat.indexOf('T'));
34
- }
35
- return valueString(value).replace(rDateCleanup, '');
36
- } else if (type === 'number') {
37
- const numberFormat = value.toFixed(precision ?? defaultPrecision);
38
- return (trim ?? defaultTrim) ? numberFormat.replace(rNumberCleanup, '') : numberFormat;
39
- }
40
- return `${value}`;
41
- }
42
-
43
- const defaultPrecision = 2;
44
- const defaultTrim = true;
45
- const rNumberCleanup = /\.0*$/;
46
- const rDateCleanup = /(?:(?:(?:-01)?T00:00)?:00)?(?:\.\d{1,6})?(?:Z|[+-]\d\d:\d\d)$/;
47
-
48
-
49
- // Helper function to compute a value's parameter
50
- export function valueParameter(value, minValue, maxValue) {
51
- if (minValue === maxValue) {
52
- return 0;
53
- }
54
-
55
- if (valueType(minValue) === 'datetime') {
56
- const minDateValue = minValue.valueOf();
57
- return (value.valueOf() - minDateValue) / (maxValue.valueOf() - minDateValue);
58
- }
59
-
60
- return (value - minValue) / (maxValue - minValue);
61
- }
62
-
63
-
64
- // Helper function to compute a value from a parameter
65
- export function parameterValue(param, minValue, maxValue) {
66
- if (valueType(minValue) === 'datetime') {
67
- const minDateValue = minValue.valueOf();
68
- return new Date(minDateValue + param * (maxValue.valueOf() - minDateValue));
69
- }
70
-
71
- return minValue + param * (maxValue - minValue);
72
- }