@newlogic-digital/core 1.0.0 → 1.0.3

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 (2) hide show
  1. package/index.js +68 -63
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -7,11 +7,34 @@ import lodash from 'lodash'
7
7
  import minifier from 'html-minifier-terser'
8
8
  import fs from 'fs'
9
9
  import { dirname, resolve } from 'path'
10
- import Prism from 'prismjs'
11
- import loadLanguages from 'prismjs/components/index.js'
12
- import NormalizeWhitespace from 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace.js'
10
+ import postHtml from 'posthtml'
11
+ import highlight from './prism.js'
13
12
 
14
- loadLanguages(['markup', 'css', 'javascript'])
13
+ const posthtmlPrism = {
14
+ name: '@vituum/vite-plugin-posthtml-prism',
15
+ enforce: 'post',
16
+ transformIndexHtml: {
17
+ enforce: 'post',
18
+ transform: async(html, { filename }) => {
19
+ filename = filename.replace('?raw', '')
20
+
21
+ if (!filename.endsWith('ui.json')) {
22
+ console.log(filename)
23
+ return
24
+ }
25
+
26
+ const plugins = [highlight({ inline: false })]
27
+
28
+ const result = await postHtml(plugins).process(html)
29
+
30
+ return result.html
31
+ }
32
+ }
33
+ }
34
+
35
+ const wrapPreCode = (code, lang) => {
36
+ return `<pre class="language-${lang}"><code class="language-${lang}">${code}</code></pre>`
37
+ }
15
38
 
16
39
  const stripIndent = (string) => {
17
40
  const indent = () => {
@@ -33,7 +56,27 @@ const stripIndent = (string) => {
33
56
  return string.replace(regex, '')
34
57
  }
35
58
 
59
+ const parseMinifyHtml = async (input, name) => {
60
+ const minify = await minifier.minify(input, {
61
+ collapseWhitespace: true,
62
+ collapseInlineTagWhitespace: false,
63
+ minifyCSS: true,
64
+ removeAttributeQuotes: true,
65
+ quoteCharacter: '\'',
66
+ minifyJS: true
67
+ })
68
+
69
+ if (name) {
70
+ return JSON.stringify({
71
+ [name]: minify
72
+ })
73
+ } else {
74
+ return JSON.stringify(minify)
75
+ }
76
+ }
77
+
36
78
  const defaultConfig = {
79
+ format: 'twig',
37
80
  posthtml: {},
38
81
  juice: {},
39
82
  tailwind: {},
@@ -121,26 +164,15 @@ const defaultConfig = {
121
164
  const name = Reflect.apply(Twig.expression.parse, this, [token.stack, context])
122
165
  const output = this.parse(token.output, context)
123
166
 
124
- const minify = await minifier.minify(output, {
125
- collapseWhitespace: true,
126
- collapseInlineTagWhitespace: false,
127
- minifyCSS: true,
128
- removeAttributeQuotes: true,
129
- quoteCharacter: '\'',
130
- minifyJS: true
131
- })
132
-
133
167
  if (name === '_null') {
134
168
  return {
135
169
  chain,
136
- output: JSON.stringify(minify)
170
+ output: await parseMinifyHtml(output)
137
171
  }
138
172
  } else {
139
173
  return {
140
174
  chain,
141
- output: JSON.stringify({
142
- [name]: minify
143
- })
175
+ output: await parseMinifyHtml(output, name)
144
176
  }
145
177
  }
146
178
  }
@@ -179,52 +211,9 @@ const defaultConfig = {
179
211
  type = type.replace(":mirror", "")
180
212
  }
181
213
 
182
- const Normalize = new NormalizeWhitespace({
183
- 'remove-trailing': true,
184
- 'remove-indent': true,
185
- 'left-trim': true,
186
- 'right-trim': true,
187
- });
188
-
189
- const wrap = (code, lang) => {
190
- return `<pre class="language-${lang}"><code>${code}</code></pre>`
191
- }
192
-
193
- const highlight = (str, lang) => {
194
- if (!lang) {
195
- return wrap(str, 'text')
196
- }
197
- lang = lang.toLowerCase()
198
- const rawLang = lang
199
- if (lang === 'vue' || lang === 'html') {
200
- lang = 'markup'
201
- }
202
- if (lang === 'md') {
203
- lang = 'markdown'
204
- }
205
- if (lang === 'ts') {
206
- lang = 'typescript'
207
- }
208
- if (lang === 'py') {
209
- lang = 'python'
210
- }
211
- if (!Prism.languages[lang]) {
212
- try {
213
- loadLanguages([lang])
214
- } catch (e) {
215
- console.warn(`Syntax highlight for language "${lang}" is not supported.`)
216
- }
217
- }
218
- if (Prism.languages[lang]) {
219
- const code = Prism.highlight(Normalize.normalize(str), Prism.languages[lang], lang)
220
- return wrap(code, rawLang)
221
- }
222
- return wrap(str, 'text')
223
- }
224
-
225
214
  return {
226
215
  chain: chain,
227
- output: `${mirror ? output : ""}${highlight(output, type)}`
216
+ output: `${mirror ? output : ""}${wrapPreCode(output, type)}`
228
217
  };
229
218
  }
230
219
  });
@@ -238,7 +227,22 @@ const defaultConfig = {
238
227
  ]
239
228
  },
240
229
  latte: {
241
- isStringFilter: (filename) => dirname(filename).endsWith('emails')
230
+ isStringFilter: (filename) => dirname(filename).endsWith('emails'),
231
+ globals: {
232
+ srcPath: resolve(process.cwd(), 'src'),
233
+ templatesPath: resolve(process.cwd(), 'src/templates')
234
+ },
235
+ functions: {
236
+ pages: () => {
237
+ return fs.readdirSync(resolve(process.cwd(), 'src/views')).filter(file => fs.statSync(resolve(process.cwd(), 'src/views/' + file)).isFile())
238
+ }
239
+ },
240
+ filters: {
241
+ json: async (input, name) => {
242
+ return await parseMinifyHtml(input, name)
243
+ },
244
+ code: 'node_modules/@newlogic-digital/core/latte/CodeFilter.php'
245
+ }
242
246
  }
243
247
  }
244
248
 
@@ -248,13 +252,14 @@ const integration = (userConfig = {}) => {
248
252
  return {
249
253
  config: {
250
254
  integrations: [posthtml(userConfig.posthtml), juice(userConfig.juice), tailwind(userConfig.tailwind), twig(userConfig.twig), latte(userConfig.latte)],
255
+ plugins: [posthtmlPrism],
251
256
  server: {
252
257
  open: true,
253
258
  https: true,
254
259
  reload: file => (file.endsWith('.tpl') || file.endsWith('.latte')) && !file.includes('temp/')
255
260
  },
256
261
  templates: {
257
- format: 'twig'
262
+ format: userConfig.format
258
263
  },
259
264
  imports: {
260
265
  paths: ['./src/styles/**', './src/scripts/**', '!./src/styles/Utils/**']
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@newlogic-digital/core",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.3",
5
5
  "main": "index.js",
6
6
  "author": "New Logic Studio s.r.o.",
7
7
  "description": "Set of tools that can be used to create modern web applications",
@@ -10,6 +10,7 @@
10
10
  "npm-publish": "npm publish --tag next"
11
11
  },
12
12
  "dependencies": {
13
+ "posthtml-prism": "^1.0.4",
13
14
  "@vituum/tailwind": "^0.1.2",
14
15
  "@vituum/posthtml": "^0.1.0",
15
16
  "@vituum/juice": "^0.1.3",