@newlogic-digital/core 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +32 -64
  2. package/package.json +1 -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,53 +56,6 @@ const stripIndent = (string) => {
33
56
  return string.replace(regex, '')
34
57
  }
35
58
 
36
- const parsePrism = (type, input) => {
37
- const Normalize = new NormalizeWhitespace({
38
- 'remove-trailing': true,
39
- 'remove-indent': true,
40
- 'left-trim': true,
41
- 'right-trim': true,
42
- });
43
-
44
- const wrap = (code, lang) => {
45
- return `<pre class="language-${lang}"><code>${code}</code></pre>`
46
- }
47
-
48
- const highlight = (str, lang) => {
49
- if (!lang) {
50
- return wrap(str, 'text')
51
- }
52
- lang = lang.toLowerCase()
53
- const rawLang = lang
54
- if (lang === 'vue' || lang === 'html') {
55
- lang = 'markup'
56
- }
57
- if (lang === 'md') {
58
- lang = 'markdown'
59
- }
60
- if (lang === 'ts') {
61
- lang = 'typescript'
62
- }
63
- if (lang === 'py') {
64
- lang = 'python'
65
- }
66
- if (!Prism.languages[lang]) {
67
- try {
68
- loadLanguages([lang])
69
- } catch (e) {
70
- console.warn(`Syntax highlight for language "${lang}" is not supported.`)
71
- }
72
- }
73
- if (Prism.languages[lang]) {
74
- const code = Prism.highlight(Normalize.normalize(str), Prism.languages[lang], lang)
75
- return wrap(code, rawLang)
76
- }
77
- return wrap(str, 'text')
78
- }
79
-
80
- return highlight(input, type);
81
- }
82
-
83
59
  const parseMinifyHtml = async (input, name) => {
84
60
  const minify = await minifier.minify(input, {
85
61
  collapseWhitespace: true,
@@ -237,7 +213,7 @@ const defaultConfig = {
237
213
 
238
214
  return {
239
215
  chain: chain,
240
- output: `${mirror ? output : ""}${parsePrism(type, output)}`
216
+ output: `${mirror ? output : ""}${wrapPreCode(output, type)}`
241
217
  };
242
218
  }
243
219
  });
@@ -259,22 +235,13 @@ const defaultConfig = {
259
235
  functions: {
260
236
  pages: () => {
261
237
  return fs.readdirSync(resolve(process.cwd(), 'src/views')).filter(file => fs.statSync(resolve(process.cwd(), 'src/views/' + file)).isFile())
262
- },
263
- // code: (input, type = '') => {
264
- // let mirror = false;
265
- //
266
- // if (type.includes(":mirror")) {
267
- // mirror = true;
268
- // type = type.replace(":mirror", "")
269
- // }
270
- //
271
- // return `${mirror ? input : ""}${parsePrism(type, input)}`
272
- // }
238
+ }
273
239
  },
274
240
  filters: {
275
241
  json: async (input, name) => {
276
242
  return await parseMinifyHtml(input, name)
277
- }
243
+ },
244
+ code: 'node_modules/@newlogic-digital/core/latte/CodeFilter.php'
278
245
  }
279
246
  }
280
247
  }
@@ -285,6 +252,7 @@ const integration = (userConfig = {}) => {
285
252
  return {
286
253
  config: {
287
254
  integrations: [posthtml(userConfig.posthtml), juice(userConfig.juice), tailwind(userConfig.tailwind), twig(userConfig.twig), latte(userConfig.latte)],
255
+ plugins: [posthtmlPrism],
288
256
  server: {
289
257
  open: true,
290
258
  https: true,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@newlogic-digital/core",
3
3
  "type": "module",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
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",