@newlogic-digital/core 1.0.3 → 1.0.4

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/package.json +5 -4
  2. package/prism.js +90 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@newlogic-digital/core",
3
3
  "type": "module",
4
- "version": "1.0.3",
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",
@@ -10,19 +10,20 @@
10
10
  "npm-publish": "npm publish --tag next"
11
11
  },
12
12
  "dependencies": {
13
- "posthtml-prism": "^1.0.4",
14
13
  "@vituum/tailwind": "^0.1.2",
15
14
  "@vituum/posthtml": "^0.1.0",
16
15
  "@vituum/juice": "^0.1.3",
17
16
  "@vituum/twig": "^0.1.1",
18
17
  "@vituum/latte": "^0.1.1",
19
- "prismjs": "~1.29.0",
18
+ "posthtml-prism": "^1.0.4",
19
+ "prismjs": "^1.29.0",
20
20
  "html-minifier-terser": "^7.0.0",
21
21
  "lodash": "^4.17.21",
22
22
  "vituum": "^0.0.27"
23
23
  },
24
24
  "files": [
25
- "index.js"
25
+ "index.js",
26
+ "prism.js"
26
27
  ],
27
28
  "engines": {
28
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