-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutils.js
More file actions
125 lines (118 loc) · 2.73 KB
/
Copy pathutils.js
File metadata and controls
125 lines (118 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const Prism = require('prismjs')
const loadLanguages = require('prismjs/components/')
loadLanguages()
require('./prism-diff-highlight')(Prism)
const HTML_TAG =
/<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/gi
const PSEUDO_CLASSES = [
'active',
'any-link',
'blank',
'checked',
'current',
'default',
'defined',
'dir',
'disabled',
'drop',
'empty',
'enabled',
'first',
'first-child',
'first-of-type',
'fullscreen',
'future',
'focus',
'focus-visible',
'focus-within',
'has',
'host',
'host',
'host-context',
'hover',
'indeterminate',
'in-range',
'invalid',
'is',
'lang',
'last-child',
'last-of-type',
'left',
'link',
'local-link',
'not',
'nth-child',
'nth-col',
'nth-last-child',
'nth-last-col',
'nth-last-of-type',
'nth-of-type',
'only-child',
'only-of-type',
'optional',
'out-of-range',
'past',
'picture-in-picture',
'placeholder-shown',
'read-only',
'read-write',
'required',
'right',
'root',
'scope',
'state',
'target',
'target-within',
'user-invalid',
'valid',
'visited',
'where',
]
Prism.hooks.add('wrap', (env) => {
if (env.type === 'atrule') {
const content = env.content.replace(HTML_TAG, '')
if (content.startsWith('@apply')) {
env.classes.push('atapply')
}
} else if (env.type === 'pseudo-class') {
if (!new RegExp(`^::?(${PSEUDO_CLASSES.join('|')})`).test(env.content)) {
env.classes = env.classes.filter((x) => x !== 'pseudo-class')
}
}
})
module.exports.addImport = function addImport(tree, mod, name) {
tree.children.unshift({
type: 'import',
value: `import { ${name} as _${name} } from '${mod}'`,
})
return `_${name}`
}
module.exports.addDefaultImport = function addImport(tree, mod, name) {
tree.children.unshift({
type: 'import',
value: `import _${name} from '${mod}'`,
})
return `_${name}`
}
module.exports.addExport = function addExport(tree, name, value) {
tree.children.push({
type: 'export',
value: `export const ${name} = ${JSON.stringify(value)}`,
})
}
module.exports.highlightCode = function highlightCode(code, prismLanguage) {
const isDiff = prismLanguage.startsWith('diff-')
const language = isDiff ? prismLanguage.substr(5) : prismLanguage
const grammar = Prism.languages[isDiff ? 'diff' : language]
if (!grammar) {
console.warn(`Unrecognised language: ${prismLanguage}`)
return Prism.util.encode(code)
}
let highlighted = Prism.highlight(code, grammar, prismLanguage)
return language === 'html'
? highlighted.replace(
/\*\*(.*?)\*\*/g,
(_, text) => `<span class="code-highlight bg-code-highlight">${text}</span>`
)
: highlighted
}