markdown-up 3.0.1 → 3.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/README.md CHANGED
@@ -69,7 +69,7 @@ parse CSV, render data tables, draw line charts, and more. For more information,
69
69
 
70
70
  For example:
71
71
 
72
- ```
72
+ ``` markdown
73
73
  # Dynamic Markdown Example
74
74
 
75
75
  ~~~ markdown-script
@@ -117,7 +117,7 @@ following example that uses the
117
117
  [unittest.mds](https://craigahobbs.github.io/markdown-up/library/include.html#var.vGroup='unittest.mds')
118
118
  system library:
119
119
 
120
- ~~~
120
+ ~~~ barescript
121
121
  include <unittest.mds>
122
122
 
123
123
  function testSanity():
package/lib/app.js CHANGED
@@ -41,7 +41,7 @@ struct MarkdownUp
41
41
  struct MarkdownUpLocal
42
42
 
43
43
  # If set, dark mode is enabled
44
- optional int(== 1) darkMode
44
+ optional bool darkMode
45
45
 
46
46
  # The font size
47
47
  optional int(>= 8, <= 18) fontSize
@@ -75,14 +75,14 @@ enum MarkdownUpView
75
75
  * The MarkdownUp application options
76
76
  *
77
77
  * @typedef {Object} MarkdownUpOptions
78
- * @property {boolean} [darkMode = false] - If true, use dark mode by default
79
- * @property {number} [fontSize = 12] - The font size, in points
78
+ * @property {?boolean} [darkMode = false] - If true, use dark mode by default
79
+ * @property {?number} [fontSize = 12] - The font size, in points
80
80
  * @property {?Object} [globals = null] - Global script runtime variables
81
- * @property {number} [lineHeight = 1.2] - The line height, in em
81
+ * @property {?number} [lineHeight = 1.2] - The line height, in em
82
82
  * @property {?string} [markdownText = null] - The default Markdown text
83
- * @property {boolean} [menu = true] - If true, show the menu
84
- * @property {string} [systemPrefix] - The markdown-script system include prefix
85
- * @property {string} [url = 'README.md'] - The default resource URL
83
+ * @property {?boolean} [menu = true] - If true, show the menu
84
+ * @property {?string} [systemPrefix] - The markdown-script system include prefix
85
+ * @property {?string} [url = 'README.md'] - The default resource URL
86
86
  */
87
87
 
88
88
 
@@ -101,7 +101,8 @@ export class MarkdownUp {
101
101
  this.params = null;
102
102
  this.paramsLocal = null;
103
103
  this.paramsSession = null;
104
- this.darkMode = (options !== null ? options.darkMode : null) ?? false;
104
+ this.darkMode = (options !== null ? options.darkMode : null) ??
105
+ (this.window.matchMedia && this.window.matchMedia('(prefers-color-scheme: dark)').matches);
105
106
  this.fontSize = (options !== null ? options.fontSize : null) ?? 12;
106
107
  this.globals = (options !== null ? options.globals : null) ?? null;
107
108
  this.lineHeight = (options !== null ? options.lineHeight : null) ?? 1.2;
@@ -182,23 +183,21 @@ export class MarkdownUp {
182
183
  // Render the element model
183
184
  renderElements(this.window.document.body, result.elements);
184
185
 
185
- // Focus?
186
- if ('focus' in result) {
187
- this.setDocumentFocus(result.focus);
188
- }
189
-
190
186
  // Navigate?
191
187
  // Note: This is done after render since it may have no effect (in which case we need to render)
192
188
  if ('location' in result && result.location !== null) {
193
189
  this.window.location.href = result.location;
194
- return;
195
- }
196
190
 
197
191
  // If there is a header ID, re-navigate to scroll to the header ID since it was just rendered.
198
192
  // The re-render is short-circuited by the unchanged hash param check above.
199
- if (!isError && getHeaderId(this.window.location.hash) !== null) {
193
+ } else if (!isError && getHeaderId(this.window.location.hash) !== null) {
200
194
  this.window.location.href = this.window.location.hash;
201
195
  }
196
+
197
+ // Focus?
198
+ if ('focus' in result) {
199
+ this.setDocumentFocus(result.focus);
200
+ }
202
201
  }
203
202
 
204
203
 
@@ -280,7 +279,7 @@ export class MarkdownUp {
280
279
  const urlOverride = 'url' in this.params && this.params.url !== '';
281
280
  const url = urlOverride ? this.params.url : this.url;
282
281
  const scriptOptions = this.createScriptOptions(url);
283
- let markdownText = null;
282
+ let markdownText;
284
283
  let timeBegin;
285
284
  if (this.markdownText !== null && !urlOverride) {
286
285
  ({markdownText} = this);
@@ -341,6 +340,16 @@ export class MarkdownUp {
341
340
  }
342
341
 
343
342
  // Render the Markdown
343
+ const markdownOptions = {
344
+ 'codeBlocks': {
345
+ 'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
346
+ },
347
+ 'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
348
+ 'headerIds': true
349
+ };
350
+ if (this.window.navigator.clipboard) {
351
+ markdownOptions.copyFn = (text) => this.window.navigator.clipboard.writeText(text);
352
+ }
344
353
  const result = {
345
354
  'title': markdownTitle,
346
355
  'elements': [
@@ -348,13 +357,7 @@ export class MarkdownUp {
348
357
  this.burgerElements(),
349
358
 
350
359
  // Render the markdown
351
- await markdownElementsAsync(markdownModel, {
352
- 'codeBlocks': {
353
- 'markdown-script': (codeBlock) => markdownScriptCodeBlock(codeBlock, scriptOptions)
354
- },
355
- 'urlFn': (urlRaw) => this.modifyURL(urlRaw, url),
356
- 'headerIds': true
357
- })
360
+ await markdownElementsAsync(markdownModel, markdownOptions)
358
361
  ]
359
362
  };
360
363
 
@@ -452,17 +455,17 @@ export class MarkdownUp {
452
455
  }
453
456
  }
454
457
 
455
- // Focus?
456
- if (runtime.documentFocus !== null) {
457
- this.setDocumentFocus(runtime.documentFocus);
458
- }
459
-
460
458
  // Navigate?
461
459
  // Note: This is done after render since it may have no effect (in which case we want to render).
462
460
  if (runtime.windowLocation !== null) {
463
461
  this.window.location.href = runtime.windowLocation;
464
462
  }
465
463
 
464
+ // Focus?
465
+ if (runtime.documentFocus !== null) {
466
+ this.setDocumentFocus(runtime.documentFocus);
467
+ }
468
+
466
469
  // Reset the runtime
467
470
  runtime.reset();
468
471
  };
@@ -519,9 +522,9 @@ export class MarkdownUp {
519
522
  'path': 'M4,2 L20,2 L20,22 L4,22 Z',
520
523
  'path2': 'M7,7.5 L17,7.5 M7,12 L17,12 M7,16.5 L17,16.5'
521
524
  }),
522
- this.menuValueToggle('darkMode', {
525
+ this.menuDarkModeToggle({
523
526
  'path': 'M16,3 A10,10,0,1,1,3,18 A14,14,0,0,0,17,3'
524
- }, true),
527
+ }),
525
528
  this.menuValueCycle('fontSize', this.fontSize, 2, {
526
529
  'path': 'M4,22 L10,2 L14,2 L20,22 M6,14 L18,14',
527
530
  'strokeWidth': 4
@@ -546,18 +549,16 @@ export class MarkdownUp {
546
549
  }
547
550
 
548
551
 
549
- menuValueToggle(valueName, icon, isLocal = false) {
550
- const params = (isLocal ? this.paramsLocal : this.paramsSession);
551
- icon.checked = valueName in params;
552
+ menuValueToggle(valueName, icon) {
553
+ icon.checked = valueName in this.paramsSession;
552
554
  return this.menuButton(
553
555
  () => {
554
- if (valueName in params) {
555
- delete params[valueName];
556
+ if (valueName in this.paramsSession) {
557
+ delete this.paramsSession[valueName];
556
558
  } else {
557
- params[valueName] = 1;
559
+ this.paramsSession[valueName] = 1;
558
560
  }
559
- const storage = (isLocal ? this.window.localStorage : this.window.sessionStorage);
560
- storage.setItem('MarkdownUp', JSON.stringify(params));
561
+ this.window.sessionStorage.setItem('MarkdownUp', JSON.stringify(this.paramsSession));
561
562
  this.render(true);
562
563
  },
563
564
  icon
@@ -582,6 +583,24 @@ export class MarkdownUp {
582
583
  }
583
584
 
584
585
 
586
+ menuDarkModeToggle(icon) {
587
+ const isDarkMode = this.paramsLocal.darkMode ?? this.darkMode;
588
+ icon.checked = isDarkMode;
589
+ return this.menuButton(
590
+ () => {
591
+ if (isDarkMode !== this.darkMode) {
592
+ delete this.paramsLocal.darkMode;
593
+ } else {
594
+ this.paramsLocal.darkMode = !isDarkMode;
595
+ }
596
+ this.window.localStorage.setItem('MarkdownUp', JSON.stringify(this.paramsLocal));
597
+ this.render(true);
598
+ },
599
+ icon
600
+ );
601
+ }
602
+
603
+
585
604
  menuValueCycle(valueName, valueDefault, valueDelta, icon) {
586
605
  return this.menuButton(
587
606
  () => {
package/lib/dataTable.js CHANGED
@@ -89,7 +89,9 @@ export function validateDataTable(dataTable) {
89
89
  * The data table options object
90
90
  *
91
91
  * @typedef {Object} DataTableOptions
92
- * @property {function} [urlFn] - The
92
+ * @property {?function} [copyFn] - The
93
+ * [Markdown copy function]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~CopyFn}
94
+ * @property {?function} [urlFn] - The
93
95
  * [Markdown URL modifier function]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~URLFn}
94
96
  */
95
97
 
@@ -122,7 +124,10 @@ export function dataTableElements(data, dataTable = null, options = null) {
122
124
 
123
125
  // Generate the data table's element model
124
126
  const fieldFormats = (dataTable !== null ? (dataTable.formats ?? null) : null);
125
- const markdownOptions = (options !== null && 'urlFn' in options ? {'urlFn': options.urlFn} : null);
127
+ const markdownOptions = {
128
+ 'copyFn': (options !== null ? options.copyFn : null) ?? null,
129
+ 'urlFn': (options !== null ? options.urlFn : null) ?? null
130
+ };
126
131
  const formatPrecision = (dataTable !== null ? (dataTable.precision ?? null) : null);
127
132
  const formatDatetime = (dataTable !== null ? (dataTable.datetime ?? null) : null);
128
133
  const formatTrim = (dataTable !== null ? (dataTable.trim ?? null) : null);
package/lib/script.js CHANGED
@@ -274,7 +274,7 @@ export class MarkdownScriptRuntime {
274
274
  // Helper method to finish the current Markdown block and add its elements
275
275
  finishMarkdown() {
276
276
  if (this.markdown !== null) {
277
- const markdownOptions = {'headerIds': true, 'urlFn': this.options.urlFn};
277
+ const markdownOptions = {'headerIds': true, 'copyFn': this.options.copyFn, 'urlFn': this.options.urlFn};
278
278
  try {
279
279
  this.addElements(markdownElements(parseMarkdown(this.markdown), markdownOptions));
280
280
  } catch {
@@ -937,11 +937,56 @@ const sessionStorageSetArgs = valueArgsModel([
937
937
  ]);
938
938
 
939
939
 
940
+ //
941
+ // URL functions
942
+ //
943
+
944
+
945
+ // $function: urlObjectCreate
946
+ // $group: URL
947
+ // $doc: Create an object URL (i.e. a file download URL)
948
+ // $arg data: The object data string
949
+ // $arg contentType: Optional (default is "text/plain"). The object content type.
950
+ // $return: The object URL string
951
+ function urlObjectCreate(args, options) {
952
+ const [data, contentType] = valueArgsValidate(urlObjectCreateArgs, args);
953
+ return options.window.URL.createObjectURL(new Blob([data], {'type': contentType}));
954
+ }
955
+
956
+ const urlObjectCreateArgs = valueArgsModel([
957
+ {'name': 'data', 'type': 'string'},
958
+ {'name': 'contentType', 'type': 'string', 'default': 'text/plain'}
959
+ ]);
960
+
961
+
940
962
  //
941
963
  // Window functions
942
964
  //
943
965
 
944
966
 
967
+ // $function: windowClipboardRead
968
+ // $group: Window
969
+ // $doc: Read text from the clipboard
970
+ // $return: The clipboard text
971
+ async function windowClipboardRead(args, options) {
972
+ return await options.window.navigator.clipboard.readText();
973
+ }
974
+
975
+
976
+ // $function: windowClipboardWrite
977
+ // $group: Window
978
+ // $doc: Write text to the clipboard
979
+ // $arg text: The text to write
980
+ async function windowClipboardWrite(args, options) {
981
+ const [text] = valueArgsValidate(windowClipboardWriteArgs, args);
982
+ await options.window.navigator.clipboard.writeText(text);
983
+ }
984
+
985
+ const windowClipboardWriteArgs = valueArgsModel([
986
+ {'name': 'text', 'type': 'string'}
987
+ ]);
988
+
989
+
945
990
  // $function: windowHeight
946
991
  // $group: Window
947
992
  // $doc: Get the browser window's height
@@ -1075,6 +1120,9 @@ export const markdownScriptFunctions = {
1075
1120
  sessionStorageGet,
1076
1121
  sessionStorageRemove,
1077
1122
  sessionStorageSet,
1123
+ urlObjectCreate,
1124
+ windowClipboardRead,
1125
+ windowClipboardWrite,
1078
1126
  windowHeight,
1079
1127
  windowSetLocation,
1080
1128
  windowSetResize,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "3.0.1",
4
+ "version": "3.2.0",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",
@@ -24,14 +24,14 @@
24
24
  "dependencies": {
25
25
  "bare-script": "~3.0",
26
26
  "element-model": "~1.1",
27
- "markdown-model": "~1.8",
27
+ "markdown-model": "~2.1",
28
28
  "schema-markdown": "~1.2",
29
- "schema-markdown-doc": "~1.6"
29
+ "schema-markdown-doc": "~1.8"
30
30
  },
31
31
  "devDependencies": {
32
- "c8": "~9.1",
33
- "eslint": "~8.57",
32
+ "c8": "~10.1",
33
+ "eslint": "~9.11",
34
34
  "jsdoc": "~4.0",
35
- "jsdom": "~24.0"
35
+ "jsdom": "~25.0"
36
36
  }
37
37
  }