markdown-maker 1.10.2 → 1.10.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. package/.github/workflows/node.js.yml +21 -26
  2. package/.vscode/extensions.json +3 -0
  3. package/.vscode/settings.json +8 -3
  4. package/.vscode/snippets.code-snippets +6 -8
  5. package/bundle/cltool.d.ts +86 -0
  6. package/bundle/cltool.js +63 -0
  7. package/bundle/cltool.js.map +1 -0
  8. package/bundle/commands.d.ts +23 -0
  9. package/bundle/commands.js +303 -0
  10. package/bundle/commands.js.map +1 -0
  11. package/bundle/errors.d.ts +11 -0
  12. package/bundle/errors.js +51 -0
  13. package/bundle/errors.js.map +1 -0
  14. package/bundle/main.d.ts +6 -0
  15. package/bundle/main.js +152 -0
  16. package/bundle/main.js.map +1 -0
  17. package/bundle/parser.d.ts +35 -0
  18. package/bundle/parser.js +330 -0
  19. package/bundle/parser.js.map +1 -0
  20. package/bundle/templates/mathjax.d.ts +2 -0
  21. package/bundle/templates/mathjax.js +3 -0
  22. package/bundle/templates/mathjax.js.map +1 -0
  23. package/bundle/templates/presentation.d.ts +2 -0
  24. package/bundle/templates/presentation.js +3 -0
  25. package/bundle/templates/presentation.js.map +1 -0
  26. package/bundle/templates.d.ts +10 -0
  27. package/bundle/templates.js +23 -0
  28. package/bundle/templates.js.map +1 -0
  29. package/bundle/types.d.ts +28 -0
  30. package/bundle/types.js +15 -0
  31. package/bundle/types.js.map +1 -0
  32. package/bundle/version.d.ts +1 -0
  33. package/bundle/version.js +5 -0
  34. package/bundle/version.js.map +1 -0
  35. package/jest.config.js +7 -0
  36. package/package.json +42 -40
  37. package/src/cltool.ts +115 -72
  38. package/src/commands.ts +257 -241
  39. package/src/errors.ts +26 -0
  40. package/src/main.ts +114 -109
  41. package/src/parser.ts +378 -350
  42. package/src/templates.ts +5 -1
  43. package/src/types.ts +33 -0
  44. package/src/version.ts +1 -0
  45. package/tests/_test-util.ts +44 -0
  46. package/tests/advanced.spec.ts +102 -0
  47. package/tests/basic.spec.ts +68 -0
  48. package/tests/clargs.spec.ts +50 -0
  49. package/tests/errors.spec.ts +64 -0
  50. package/tests/html.spec.ts +23 -0
  51. package/tests/line.spec.ts +21 -0
  52. package/tests/marked.spec.ts +40 -0
  53. package/tests/target.spec.ts +41 -0
  54. package/tests/vars.spec.ts +45 -0
  55. package/tsconfig.json +66 -64
  56. package/prettierrc.yaml +0 -4
  57. package/test/advanced.test.js +0 -34
  58. package/test/basic.test.js +0 -67
  59. package/test/clargs.test.js +0 -51
  60. package/test/errors.test.js +0 -47
  61. package/test/hooks.js +0 -9
  62. package/test/hooks.test.js +0 -114
  63. package/test/html.test.js +0 -37
  64. package/test/line.test.js +0 -21
  65. package/test/marked.test.js +0 -43
  66. package/test/target.test.js +0 -43
  67. package/test/tester.test.js +0 -41
  68. package/test/vars.test.js +0 -49
package/src/main.ts CHANGED
@@ -1,122 +1,127 @@
1
+ #!/usr/bin/env node
2
+
1
3
  import path from "path";
2
4
  import { WebSocketServer } from "ws";
3
- import Parser from "./parser";
4
5
  import * as fs from "fs";
5
- const choki = require("chokidar");
6
6
 
7
- import { argParser, CLArgs as CommmandLineArgs, ParserOptions } from "./cltool";
7
+ import choki from "chokidar";
8
+ import Parser from "./parser";
9
+
10
+ /* for adding colours to strings */
11
+ import { enable as ColorsEnable } from "colors.ts";
12
+ ColorsEnable();
13
+
14
+ import { argParser, CommandLineArgs, ParserOptions } from "./cltool";
15
+ import { log } from "console";
8
16
  const configFileName = ".mdmconfig.json";
9
17
 
10
18
  function main() {
11
- let clargs: CommmandLineArgs;
12
- let server: WebSocketServer | undefined;
13
-
14
- /* Read config file or parse args from cmd-line */
15
- if (fs.existsSync(configFileName)) {
16
- let data: CommmandLineArgs = JSON.parse(
17
- fs.readFileSync(configFileName).toString()
18
- ).opts;
19
-
20
- let args: (string | number)[] = [];
21
- Object.entries(data).forEach(([key, value]) => {
22
- if (key != "src" && value !== false) {
23
- args.push("--" + key);
24
- }
25
- if (typeof value != "boolean") {
26
- args.push(value);
27
- }
28
- });
29
-
30
- /* We skip [0] and [1], as it is the binary and source file, even when compiled*/
31
- for (let i = 2; i < process.argv.length; i++)
32
- args.push(process.argv[i]);
33
-
34
- clargs = argParser.parse_args(args.map((x) => x.toString()));
35
- } else {
36
- clargs = argParser.parse_args();
37
- }
38
-
39
- /* if src is init, create config file and exit */
40
- if (clargs.src == "init") {
41
- const template = fs.readFileSync(
42
- path.join(
43
- __dirname,
44
- "..",
45
- "src",
46
- "templates",
47
- "configTemplate.json"
48
- )
49
- );
50
- fs.writeFileSync(configFileName, template);
51
- fs.writeFileSync("main.md", "# Main\n");
52
- return;
53
- }
54
-
55
- /* helper method for calling parser */
56
- const compile = (source, output, cb?) => {
57
- /* load data from file, if it exists,
58
- * otherwise, interpret as string */
59
-
60
- const parser = new Parser(source, clargs);
61
- parser.to(output, (file) => {
62
- console.log(`Compiled ${file}`.green);
63
- if (cb) cb();
64
- });
65
- return parser;
66
- };
67
-
68
- const internalCooldown = 1000;
69
- function watcher(event, path) {
70
- const now = Date.now();
71
-
72
- if (!this.time) this.time = now;
73
-
74
- if (now - this.time < internalCooldown) return;
75
-
76
- console.log(path);
77
-
78
- console.log(`Detected change in ${path}...`);
79
-
80
- try {
81
- compile(clargs.src, clargs.output, () => {
82
- /* after compile, send refresh command to clients */
83
- server.clients.forEach((client) => {
84
- if (client.OPEN) client.send("refresh");
85
- });
86
- });
87
- } catch (e) {
88
- console.log(e.message);
89
- }
90
-
91
- this.time = now;
92
- }
93
-
94
- /* in case source is a directory, look for entry in directory */
95
- if (fs.existsSync(clargs.src) && fs.lstatSync(clargs.src).isDirectory()) {
96
- clargs.src = path.join(clargs.src, clargs.entry);
97
- }
98
-
99
- if (clargs.debug) console.dir(clargs);
100
-
101
- /* compile once */
102
- if (!clargs.watch) compile(clargs.src, clargs.output);
103
- /* watch the folder and recompile on change */ else {
104
- const srcDirName = path.dirname(clargs.src);
105
- console.log(`Watching ${srcDirName} for changes...`.yellow);
106
- server = new WebSocketServer({ port: 7788 });
107
-
108
- const _watcher = choki.watch(srcDirName).on("all", watcher);
109
- try {
110
- compile(clargs.src, clargs.output);
111
- } catch (e) {
112
- console.log(e.message);
113
- }
114
- }
19
+ let clargs: CommandLineArgs;
20
+ let server: WebSocketServer | undefined;
21
+
22
+ /* Read config file or parse args from cmd-line */
23
+ if (fs.existsSync(configFileName)) {
24
+ let data: CommandLineArgs = JSON.parse(
25
+ fs.readFileSync(configFileName).toString()
26
+ ).opts;
27
+
28
+ let args: (string | number)[] = [];
29
+ Object.entries(data).forEach(([key, value]) => {
30
+ if (key != "src" && value !== false) {
31
+ args.push("--" + key);
32
+ }
33
+ if (typeof value != "boolean") {
34
+ args.push(value);
35
+ }
36
+ });
37
+
38
+ /* We skip [0] and [1], as it is the binary and source file, even when compiled*/
39
+ for (let i = 2; i < process.argv.length; i++)
40
+ args.push(process.argv[i]);
41
+
42
+ clargs = argParser.parse_args(args.map((x) => x.toString()));
43
+ } else {
44
+ clargs = argParser.parse_args();
45
+ }
46
+
47
+ /* if src is init, create config file and exit */
48
+ if (clargs.src == "init") {
49
+ const template = fs.readFileSync(
50
+ path.join(
51
+ __dirname,
52
+ "..",
53
+ "src",
54
+ "templates",
55
+ "configTemplate.json"
56
+ )
57
+ );
58
+ fs.writeFileSync(configFileName, template);
59
+ fs.writeFileSync("main.md", "# Main\n");
60
+ console.log(`Created config file ${configFileName.green}`);
61
+ return;
62
+ }
63
+
64
+ /* helper method for calling parser */
65
+ const compile = (source, output, cb?) => {
66
+ /* load data from file, if it exists,
67
+ * otherwise, interpret as string */
68
+
69
+ const parser = new Parser(source, clargs);
70
+ parser.to(output, (file) => {
71
+ console.log(`Compiled ${file}`.green);
72
+ if (cb) cb();
73
+ });
74
+ return parser;
75
+ };
76
+
77
+ const internalCooldown = 1000;
78
+ function watcher(_, path: string) {
79
+ const now = Date.now();
80
+
81
+ if (!this.time) this.time = now;
82
+ if (now - this.time < internalCooldown) return;
83
+ console.log(`Detected change in ${path}...`);
84
+ try {
85
+ compile(clargs.src, clargs.output, () => {
86
+ /* after compile, send refresh command to clients */
87
+ server.clients.forEach((client) => {
88
+ if (client.OPEN) client.send("refresh");
89
+ });
90
+ });
91
+ } catch (e) {
92
+ console.log(e.message);
93
+ }
94
+
95
+ this.time = now;
96
+ }
97
+
98
+ /* in case source is a directory, look for entry in directory */
99
+ if (fs.existsSync(clargs.src) && fs.lstatSync(clargs.src).isDirectory()) {
100
+ clargs.src = path.join(clargs.src, clargs.entry);
101
+ }
102
+
103
+ if (clargs.debug) console.dir(clargs);
104
+
105
+ /* compile once if not watching
106
+ otherwise watch the folder and recompile on change */
107
+ if (!clargs.watch) compile(clargs.src, clargs.output);
108
+ else {
109
+ const srcDirName = path.dirname(clargs.src);
110
+ console.log(`Watching ${srcDirName} for changes...`.yellow);
111
+ server = new WebSocketServer({ port: 7788 });
112
+
113
+ const _watcher = choki.watch(srcDirName).on("all", watcher);
114
+ try {
115
+ compile(clargs.src, clargs.output);
116
+ } catch (e) {
117
+ console.log(e.message);
118
+ }
119
+ }
115
120
  }
116
121
  export default {
117
- Parser,
122
+ Parser,
118
123
  };
119
124
  /* main entrypoint */
120
125
  if (require.main === module) {
121
- main();
126
+ main();
122
127
  }