@newlogic-digital/core 1.0.2 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/latte/CodeFilter.php +16 -0
- package/package.json +6 -3
- package/prism.js +90 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
<?php
|
2
|
+
|
3
|
+
namespace App\Latte;
|
4
|
+
|
5
|
+
class CodeFilter
|
6
|
+
{
|
7
|
+
|
8
|
+
public static function execute($value, $type = null, $mirror = null): \Latte\Runtime\Html
|
9
|
+
{
|
10
|
+
if ($mirror) {
|
11
|
+
$mirror = $value;
|
12
|
+
}
|
13
|
+
|
14
|
+
return new \Latte\Runtime\Html("$mirror<pre><code class='language-{$type}'>$value</code></pre>");
|
15
|
+
}
|
16
|
+
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@newlogic-digital/core",
|
3
3
|
"type": "module",
|
4
|
-
"version": "1.0.
|
4
|
+
"version": "1.0.5",
|
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,16 @@
|
|
15
15
|
"@vituum/juice": "^0.1.3",
|
16
16
|
"@vituum/twig": "^0.1.1",
|
17
17
|
"@vituum/latte": "^0.1.1",
|
18
|
-
"
|
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
|
-
"
|
25
|
+
"latte",
|
26
|
+
"index.js",
|
27
|
+
"prism.js"
|
25
28
|
],
|
26
29
|
"engines": {
|
27
30
|
"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
|