vaderjs 1.1.8 → 1.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.
Files changed (3) hide show
  1. package/index.js +5 -5
  2. package/package.json +2 -2
  3. package/vader.js +139 -18
package/index.js CHANGED
@@ -1,12 +1,12 @@
1
-
2
1
  /**
3
2
  * @file index.js
4
3
  * @description This is the entry point for the library. This file exports all the necessary classes and functions.
5
4
  * @version 1.1.5
6
- *
5
+ *
7
6
  */
8
- import Vader from "./vader.js";
7
+ import Vader, { include, useRef } from './vader.js';
9
8
  // @ts-ignore
10
- import { VaderRouter } from "./vader-router.js";
9
+ import VaderRouter from './vaderRouter.js';
11
10
 
12
- export { Vader, VaderRouter };
11
+ export { include, VaderRouter };
12
+ export default Vader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "A Reactive Framework for Single-Page Applications (SPA)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "bugs": {
23
23
  "url": "https://github.com/Postr-Inc/Vader.js/issues"
24
24
  },
25
- "homepage": "https://github.com/Postr-Inc/Vader.js#readme",
25
+ "homepage": "https://vader-js.pages.dev",
26
26
  "devDependencies": {
27
27
  "typescript": "^5.2.2"
28
28
  }
package/vader.js CHANGED
@@ -1,10 +1,108 @@
1
1
  let dom = /**@type {Obect} **/ {};
2
+ let states = {};
3
+ /**
4
+ * @function markdown
5
+ * @param {String} content
6
+ * @description Allows you to convert markdown to html
7
+ */
8
+ function markdown(content) {
9
+ const lines = content.split('\n');
10
+ let result = '';
11
+
12
+ lines.forEach((line) => {
13
+ let heading = line.match(/^#{1,6}\s/);
14
+ let bold = line.match(/\*\*(.*?)\*\*/g);
15
+ let italic = line.match(/\*(.*?)\*/g);
16
+
17
+ let link = line.match(/\[(.*?)\]\((.*?)\)/g);
18
+ let ul = line.match(/^\-\s/);
19
+ let ol = line.match(/^\d\.\s/);
20
+ let hr = line.match(/^\-\-\-\s/);
21
+ let blockquote = line.match(/^\>\s/);
22
+ let image = line.match(/\!\[(.*?)\]\((.*?)\)/g);
23
+
24
+ let codeBlock = line.match(/\`\`\`/g);
25
+ let codeBlockEnd = line.match(/\`\`\`/g);
26
+
27
+ if (heading) {
28
+ // @ts-ignore
29
+ let headingLevel = heading[0].match(/#/g).length;
30
+ line = line.replace(heading[0], `<h${headingLevel}>`);
31
+ line += `</h${headingLevel}>`;
32
+ }
33
+ if (bold) {
34
+ bold.forEach((b) => {
35
+ line = line.replace(b, `<strong>${b.replace(/\*\*/g, "")}</strong>`);
36
+ });
37
+ }
38
+ if (italic) {
39
+ italic.forEach((i) => {
40
+ line = line.replace(i, `<em>${i.replace(/\*/g, "")}</em>`);
41
+ });
42
+ }
43
+
44
+
45
+ if(link){
46
+ link.forEach((l) => {
47
+ // @ts-ignore
48
+ let text = l.match(/\[(.*?)\]/g)[0].replace(/\[|\]/g, "");
49
+ // @ts-ignore
50
+ let href = l.match(/\((.*?)\)/g)[0].replace(/\(|\)/g, "");
51
+ line = line.replace(l, `<a href="${href}">${text}</a>`);
52
+ });
53
+ }
54
+ if (ul) {
55
+ line = line.replace(ul[0], `<li
56
+ style="list-style-type: disc;"
57
+ >`);
58
+ line += `</li>`;
59
+ }
60
+ if (ol) {
61
+ line = line.replace(ol[0], `<li
62
+ style="list-style-type: decimal;"
63
+ >`);
64
+ line += `</li>`;
65
+ }
66
+ if (hr) {
67
+ line = line.replace(hr[0], `<hr/>`);
68
+ }
69
+ if (blockquote) {
70
+ line = line.replace(blockquote[0], `<blockquote>`);
71
+ line += `</blockquote>`;
72
+ }
73
+ if (image) {
74
+ image.forEach((i) => {
75
+ // @ts-ignore
76
+ let alt = i.match(/\[(.*?)\]/g)[0].replace(/\[|\]/g, "");
77
+ // @ts-ignore
78
+ let src = i.match(/\((.*?)\)/g)[0].replace(/\(|\)/g, "");
79
+ i.replace(i, `<img src="${src}" alt="${alt}"/>`);
80
+ line = line.replace(i, `<img src="${src}" alt="${alt}"/>`).replace('!','')
81
+ });
82
+ }
83
+ if(codeBlock){
84
+ line = line.replace(codeBlock[0], `<pre><code>`);
85
+ }
86
+ if(codeBlockEnd){
87
+ line = line.replace(codeBlockEnd[0], `</code></pre>`);
88
+ }
89
+
90
+
91
+ result += `${line}\n`;
92
+ });
93
+
94
+ return result;
95
+ }
96
+
97
+
98
+
2
99
  /**
3
100
  * @function useRef
4
101
  * @description Allows you to get reference to DOM element
5
102
  * @param {String} ref
6
103
  * @returns {void | Object} {current, update}
7
104
  */
105
+
8
106
  export const useRef = (ref) => {
9
107
  const element = document.querySelector(`[ref="${ref}"]`);
10
108
  const getElement = () => element;
@@ -441,7 +539,7 @@ export class Component {
441
539
  });
442
540
  }) ||
443
541
  {},
444
- () => {}
542
+
445
543
  );
446
544
 
447
545
  const getField = (fieldName) => {
@@ -472,7 +570,7 @@ export class Component {
472
570
  * @description Allows you to create a state
473
571
  * @param {String} key
474
572
  * @param {*} initialValue
475
- * @param {Function} callback
573
+ * @param {*} callback
476
574
  * @url - useState works similarly to - https://react.dev/reference/react/useState
477
575
  * @returns {Array} [state, setState]
478
576
  * @example
@@ -482,10 +580,13 @@ export class Component {
482
580
  *
483
581
  * setCount(count + 1)
484
582
  */
485
- useState(key, initialValue, callback) {
486
- if (!this.states[key]) {
487
- this.states[key] = initialValue;
488
- }
583
+ useState(key, initialValue, callback = null) {
584
+
585
+ if(!this.states[key]){
586
+ this.states[key] = initialValue;
587
+ }
588
+
589
+
489
590
  return [
490
591
  this.states[key],
491
592
  /**
@@ -497,6 +598,7 @@ export class Component {
497
598
  (value) => {
498
599
  this.states[key] = value;
499
600
  this.updateComponent();
601
+ // @ts-ignore
500
602
  typeof callback === "function" ? callback() : null;
501
603
  },
502
604
  ];
@@ -690,9 +792,24 @@ export class Component {
690
792
  * @see {@link Component}
691
793
  * @see {@link Component#componentDidMount}
692
794
  */
795
+
693
796
  html(strings, ...args) {
694
797
  // @ts-ignore
695
- let caller = new Error().stack.split("\n")[2].trim();
798
+ if (
799
+ // @ts-ignore
800
+ new Error().stack &&
801
+ // @ts-ignore
802
+ new Error().stack.split("\n").length > 0 &&
803
+ // @ts-ignore
804
+ new Error().stack.split("\n")[2] &&
805
+ // @ts-ignore
806
+ new Error().stack.split("\n")[2].includes("render") &&
807
+ !this.componentMounted
808
+ ) {
809
+ this.componentMounted = true;
810
+ this.componentDidMount();
811
+ console.log("component mounted");
812
+ }
696
813
 
697
814
  let result = "";
698
815
  for (let i = 0; i < strings.length; i++) {
@@ -702,6 +819,8 @@ export class Component {
702
819
  }
703
820
  }
704
821
 
822
+ result = new Function("useRef", `return \`${result}\``)(useRef)
823
+
705
824
  if (!result.trim().startsWith("<body>")) {
706
825
  console.warn(
707
826
  "You should wrap your html in a body tag, vader may not grab all html!"
@@ -771,6 +890,9 @@ export class Component {
771
890
  // @ts-ignore
772
891
  dom[element.getAttribute("ref")] = element;
773
892
  }
893
+ if(element.nodeName === "MARKDOWN"){
894
+ element.innerHTML = markdown(element.innerHTML.trim())
895
+ }
774
896
 
775
897
  if (element.hasAttribute("class")) {
776
898
  const allowClassComments =
@@ -853,10 +975,6 @@ export class Component {
853
975
 
854
976
  data-component="${this.name}">${result}</div>`;
855
977
  }
856
- if (caller.includes("render") && !this.componentMounted) {
857
- this.componentMounted = true;
858
- this.componentDidMount();
859
- }
860
978
 
861
979
  return result;
862
980
  }
@@ -873,7 +991,7 @@ export class Component {
873
991
  * </div>
874
992
  * `);
875
993
  */
876
- async render() {}
994
+ async render(props) {}
877
995
  }
878
996
 
879
997
  /**
@@ -941,8 +1059,9 @@ let cache = {};
941
1059
  * @returns {Promise} - modified string with html content
942
1060
  * @param {string} path
943
1061
  */
944
-
1062
+
945
1063
  export const include = async (path) => {
1064
+
946
1065
  if (
947
1066
  path.startsWith("/") &&
948
1067
  !path.includes("/src/") &&
@@ -965,10 +1084,11 @@ export const include = async (path) => {
965
1084
  })
966
1085
  .then(async (data) => {
967
1086
  // Handle includes
968
- let includes = data.match(/<include src="(.*)"\/>/gs);
1087
+ let includes = data.match(/<include src="(.*)"\/>/g);
969
1088
  if (includes) {
970
- // Use Promise.all to fetch all includes concurrently
1089
+
971
1090
  const includePromises = includes.map((e) => {
1091
+
972
1092
  // @ts-ignore
973
1093
  let includePath = e.match(/<include src="(.*)"\/>/)[1];
974
1094
 
@@ -981,19 +1101,20 @@ export const include = async (path) => {
981
1101
  includePath = "/src" + includePath;
982
1102
  }
983
1103
  return include(includePath).then((includeData) => {
984
- // Replace the include tag with the fetched data
985
1104
  data = data.replace(e, includeData);
1105
+ console.log("included", includePath);
986
1106
  });
987
1107
  });
988
1108
 
989
1109
  // Wait for all includes to be fetched and replaced
990
1110
  return Promise.all(includePromises).then(() => {
991
1111
  cache[path] = data;
992
- return new Function(`return \`${data}\`;`)();
1112
+
1113
+ return data
993
1114
  });
994
1115
  } else {
995
1116
  cache[path] = data;
996
- return new Function(`return \`${data}\`;`)();
1117
+ return data;
997
1118
  }
998
1119
  });
999
1120
  };