vaderjs 1.2.7 → 1.2.9

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/worker.js ADDED
@@ -0,0 +1,173 @@
1
+
2
+ onmessage = (e)=>{
3
+ let time_started = Date.now()
4
+ let strings = e.data.strings
5
+ let args = e.data.args
6
+ let l = e.data.location.split('/').slice(0,-1).join('/')
7
+ let result = "";
8
+ for (let i = 0; i < strings.length; i++) {
9
+ result += strings[i];
10
+ if (i < args.length) {
11
+ result += args[i];
12
+ }
13
+ }
14
+
15
+ let comments = result.match(/--([^>]*)--/gs)
16
+ if(comments){
17
+ while(comments.length){
18
+ let comment = comments.pop()
19
+ console.log(comment)
20
+ // @ts-ignore
21
+ result = result.replace(comment,'')
22
+ }
23
+ }
24
+
25
+
26
+ // Convert headings (e.g., #1-6 Heading => <h1-6>Heading</h1-6>)
27
+ // @ts-ignore
28
+ result = result.replace(/(#+)(.*)/g, (match, hashes, text) => {
29
+ if(!match.includes('<')){
30
+ let level = hashes.length;
31
+ return `<h ${level} class="markdown_heading">${text}</h${level}>`;
32
+ }
33
+ });
34
+
35
+
36
+ // Convert bold (e.g., **Bold** => <b>Bold</b>)
37
+ result = result.replace(/\*\*(.*?)\*\*/g, (match, text) => {
38
+ return `<b class="markdown_bold">${text}</b>`;
39
+ });
40
+
41
+ // Convert italic (e.g., *Italic* => <i>Italic</i>)
42
+ result = result.replace(/\*(.*?)\*/g, (match, text) => {
43
+ return `<i class="markdown_italic">${text}</i>`;
44
+ });
45
+
46
+ // Convert code (e.g., `code` => <code>code</code>)
47
+ result = result.replace(/`(.*?)`/g, (match, text) => {
48
+ return `<code>${text}</code>`;
49
+ });
50
+
51
+ // Convert links (e.g., [Text](URL) => <a href="URL">Text</a>)
52
+ result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
53
+ return `<a class="markdown_link" href="${url}">${text}</a>`;
54
+ });
55
+
56
+ // Convert images (e.g., ![Alt](URL) => <img src="URL" alt="Alt" />)
57
+ result = result.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
58
+ return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
59
+ });
60
+
61
+ // Convert unordered lists (e.g., * Item => <ul><li>Item</li></ul>)
62
+ result.split('\n').forEach((line, index, arr) => {
63
+ if (line.match(/^\s*-\s+(.*?)$/gm)) {
64
+ if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
65
+ result = result.replace(line, `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside;"><li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`);
66
+ } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*-\s+(.*?)$/gm)) {
67
+ result = result.replace(line, `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li></ul>`);
68
+ } else {
69
+ result = result.replace(line, `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`);
70
+ }
71
+ }
72
+ });
73
+
74
+
75
+ // Convert ordered lists (e.g., 1. Item => <ol><li>Item</li></ol>) in order
76
+
77
+ result.split('\n').forEach((line, index, arr) => {
78
+ if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
79
+ if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
80
+ result = result.replace(line, `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`);
81
+ } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
82
+ result = result.replace(line, `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li></ol>`);
83
+ } else {
84
+ result = result.replace(line, `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`);
85
+ }
86
+ }
87
+ });
88
+
89
+
90
+ result = result.replace(/^\s*-\s+(.*?)$/gm, (match, text) => {
91
+ return `<li class="markdown_list_item">${text}</li>`;
92
+ });
93
+ result = result.replace(/^\s*---\s*$/gm, '<hr class="markdown_horizontal" />');
94
+
95
+
96
+
97
+
98
+
99
+ if(!result.includes('<body>')){
100
+ throw new Error(`Vader Error: You must enclose your html in a body tag for all components. \n\n${result}`)
101
+ }
102
+ /**
103
+ * @type {string[]}
104
+ * @description - grabbing all className attributes and replace them with class
105
+ */
106
+ // @ts-ignore
107
+ result = result.replace(/classname/g,'class')
108
+ /**
109
+ * @type {string[]}
110
+ * @description - grabbing all image tags and replace the src attribute with the absolute path
111
+ */
112
+ // @ts-ignore
113
+ let images = result.match(/<img([^>]*)>/g)
114
+ if(images){
115
+ for(let i = 0; i < images.length; i++){
116
+ let image = images[i]
117
+ let src = image.match(/src="([^"]*)"/)
118
+ let alt = image.match(/alt="([^"]*)"/)
119
+ if(src){
120
+ if(!src[1].includes('http') || !result.includes('<!-- #vader-disable_relative-paths -->')){
121
+ // @ts-ignore
122
+ result = result.replace(src[1],`${l}/${src[1]}`)
123
+ }else{
124
+ throw new Error(`Vader Error: You cannot use relative paths in the src attribute of ${src[0]}. Use absolute paths instead. \n\n${src[0]}`)
125
+ }
126
+ }
127
+ if(!alt && !result.includes('<!-- #vader-disable_accessibility -->')){
128
+ throw new Error(`Vader Error: You must include an alt attribute in the image tag \n\n${image} of class ${e.data.name}. `)
129
+ }
130
+
131
+ // @ts-ignore
132
+ if(!caches.match(`${l}/${src[1]}`)){
133
+ caches.open('vader').then((cache)=>{
134
+ // @ts-ignore
135
+ cache.add(`${l}/${src[1]}`)
136
+ // @ts-ignore
137
+ console.log('cached', `${l}/${src[1]}`)
138
+ }).catch((err)=>{
139
+ console.log(err)
140
+ })
141
+ }else{
142
+ // @ts-ignore
143
+ console.log('already cached', caches.match(`${l}/${src[1]}`))
144
+ }
145
+ }
146
+ }
147
+
148
+ let href = result.match(/href="([^"]*)"/g)
149
+ if(href){
150
+ while(href.length){
151
+ let h = href.pop()
152
+ // @ts-ignore
153
+ h = h.replace('href="','').replace('"','')
154
+ if(!h.includes('http') || !result.includes('<!-- #vader-disable_relative-paths -->')){
155
+ result = result.replace(`href="${h}"`,`href="#${h}"`)
156
+ }else{
157
+ throw new Error(`Vader Error: You cannot use relative paths in ${e.data.file}. Use absolute paths instead. \n\n${h}`)
158
+ }
159
+ }
160
+ }
161
+
162
+ let time_ended = Date.now()
163
+ let time_taken = time_ended - time_started
164
+ let hasran = false
165
+ if(l.includes('localhost') || l.includes('127.0.0.1') && !hasran){
166
+ hasran = true
167
+ result+= `\$\{console.log('%c${e.data.name} component rendered in ${time_taken}ms','color:#fff;background:#000;padding:5px;border-radius:5px;font-size:12px;font-weight:bold'),""\}`
168
+ }
169
+
170
+
171
+ postMessage(`<div data-component=${e.data.name}>${result}</div>`)
172
+
173
+ }