@newlogic-digital/core 1.0.1 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.js +32 -64
  2. package/package.json +5 -3
  3. package/prism.js +90 -0
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.4",
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",
@@ -15,13 +15,15 @@
15
15
  "@vituum/juice": "^0.1.3",
16
16
  "@vituum/twig": "^0.1.1",
17
17
  "@vituum/latte": "^0.1.1",
18
- "prismjs": "~1.29.0",
18
+ "posthtml-prism": "^1.0.4",
19
+ "prismjs": "^1.29.0",
19
20
  "html-minifier-terser": "^7.0.0",
20
21
  "lodash": "^4.17.21",
21
22
  "vituum": "^0.0.27"
22
23
  },
23
24
  "files": [
24
- "index.js"
25
+ "index.js",
26
+ "prism.js"
25
27
  ],
26
28
  "engines": {
27
29
  "node": ">=16.0.0",
package/prism.js ADDED
@@ -0,0 +1,90 @@
1
+ import Prism from 'prismjs'
2
+ import {render} from 'posthtml-render'
3
+ import loadLanguages from 'prismjs/components/index.js'
4
+ import NormalizeWhitespace from 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace.js'
5
+
6
+ const Normalize = new NormalizeWhitespace({
7
+ 'remove-trailing': true,
8
+ 'remove-indent': true,
9
+ 'left-trim': true,
10
+ 'right-trim': true,
11
+ });
12
+
13
+ const createPrismPlugin = options => {
14
+ return tree => {
15
+ const highlightCodeTags = node => tree.match.call(node, {tag: 'code'}, highlightNode)
16
+
17
+ if (options.inline) {
18
+ highlightCodeTags(tree)
19
+ } else {
20
+ tree.match({tag: 'pre'}, highlightCodeTags)
21
+ }
22
+ }
23
+ }
24
+
25
+ const highlightNode = node => {
26
+ const attrs = node.attrs || {}
27
+ const classList = `${attrs.class || ''}`.trimStart()
28
+
29
+ if ('prism-ignore' in attrs) {
30
+ delete node.attrs['prism-ignore']
31
+
32
+ return node
33
+ }
34
+
35
+ if (classList.includes('prism-ignore')) {
36
+ node.attrs.class = node.attrs.class.replace('prism-ignore', '').trim()
37
+
38
+ return node
39
+ }
40
+
41
+ const lang = getExplicitLanguage(classList)
42
+
43
+ if (lang && !classList.includes(`language-${lang}`)) {
44
+ attrs.class = `${classList || ''} language-${lang}`.trimStart()
45
+ }
46
+
47
+ node.attrs = attrs
48
+
49
+ if (node.content) {
50
+ const html = (node.content[0].tag && !node.content[0].content) ? `<${node.content[0].tag}>` : render(node.content)
51
+
52
+ node.content = mapStringOrNode(html, lang)
53
+ }
54
+
55
+ return node
56
+ }
57
+
58
+ const mapStringOrNode = (stringOrNode, lang = null) => {
59
+ if (typeof stringOrNode === 'string') {
60
+ if (lang) {
61
+ if (!Object.keys(Prism.languages).includes(lang)) {
62
+ loadLanguages.silent = true
63
+ loadLanguages([lang])
64
+ }
65
+
66
+ return Prism.highlight(Normalize.normalize(stringOrNode), Prism.languages[lang], lang)
67
+ }
68
+
69
+ return Prism.highlight(Normalize.normalize(stringOrNode), Prism.languages.markup, 'markup')
70
+ }
71
+
72
+ highlightNode(stringOrNode)
73
+
74
+ return stringOrNode
75
+ }
76
+
77
+ const getExplicitLanguage = classList => {
78
+ const matches = classList.match(/(?:lang|language)-(\w*)/)
79
+
80
+ return matches === null ? null : matches[1]
81
+ }
82
+
83
+ const plugin = options => {
84
+ options = options || {}
85
+ options.inline = options.inline || false
86
+
87
+ return tree => createPrismPlugin(options)(tree)
88
+ }
89
+
90
+ export default plugin