forked from vuejs/docs
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhighlight.js
158 lines (140 loc) · 4.1 KB
/
highlight.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const { parse } = require('@babel/parser')
const { default: traverse } = require('@babel/traverse')
const prism = require('prismjs')
const loadLanguages = require('prismjs/components/index')
const MARKER = '__INLINE_TEMPLATE_STRING__'
/**
* @param {(code: string, lang?: string) => string} fancyHighlight
*/
module.exports = fancyHighlight => {
prism.languages['vue-html'] = prism.languages.extend('markup', {})
prism.languages.insertBefore('vue-html', 'tag', {
interpolation: {
pattern: /{{((?!}})(.|\n))*}}/,
inside: {
punctuation: /^{{|}}$/,
'inline-js language-js': {
pattern: /.*/,
inside: prism.languages.javascript
}
}
}
})
prism.languages['vue-html'].tag.inside = prism.languages.insertBefore(
'inside',
'attr-value',
{
directive: {
pattern: /(?<=\s)((v-[a-z0-9-]+)|:|@|#)(((?<=[:@#])|:)([a-z0-9-]+|(\[[^\]]+\])))?(\.[a-z0-9]+)*="[^"]+"/i,
inside: {
'punctuation directive-shorthand': /^[:@#]/,
'identifier directive-name': /^v-[a-z0-9-]+/i,
'directive-argument': [
{
pattern: /^((?<=[:@#])|:)[a-z0-9-]+/i,
inside: {
punctuation: /^:/,
identifier: /[a-z0-9-]+/i
}
},
{
pattern: /^((?<=[:@#])|:)(\[[^\]]+\])/,
inside: {
punctuation: /^\[|\]$/,
'inline-js language-js': {
pattern: /.*/,
inside: prism.languages.javascript
}
}
}
],
'directive-modifier': {
pattern: /^.[a-z0-9-]+/i,
inside: {
punctuation: /^./,
identifier: /[a-z0-9-]+/i
}
},
'directive-expression': {
pattern: /^=(?:"[^"]*"|'[^']*'|[^\s'">=]+)/i,
inside: {
punctuation: [/^=/, /^["']|["']$/],
'inline-js language-js': {
pattern: /.*/,
inside: prism.languages.javascript
}
}
}
}
}
},
prism.languages['vue-html'].tag
)
/**
* Highlight template strings in JavaScript.
*
* @param {string} code
* @param {string|undefined} lang?
*/
return function highlight(code, lang) {
if (lang !== 'js') {
return fancyHighlight(code, lang)
}
const source = extractTemplate(code)
if (!source) return fancyHighlight(code, lang)
try {
const copy = code.replace(source, MARKER)
const output = fancyHighlight(copy, lang)
const template = `<span class="language-html">${prism.highlight(
source,
prism.languages['vue-html'],
'html'
)}</span>`
return output.replace(MARKER, template)
} catch (error) {
console.log(error)
return fancyHighlight(code, lang)
}
}
}
/**
* @param {string} code
*/
function extractTemplate(code) {
try {
const AST = parse(code, { sourceType: 'module' })
let source = ''
traverse(AST, {
/**
* @param {import("@babel/traverse").NodePath<import('@babel/types').ObjectExpression>} node$
*/
ObjectExpression(node$) {
/**
* @type {import("@babel/traverse").NodePath<import("@babel/types").ObjectProperty>|null}
*/
const property$ = node$.get('properties').find(node$ => {
if (node$.isObjectProperty()) {
const key$ = node$.get('key')
return key$.isIdentifier({ name: 'template' })
}
})
if (!property$) return
const template$ = property$.get('value')
if (template$.isTemplateLiteral()) {
if (template$.get('quasis').length === 1) {
const item$ = template$.get('quasis')[0]
if (item$.isTemplateElement()) {
source = item$.node.value.raw
}
}
} else if (template$.isStringLiteral()) {
source = template$.node.value
}
}
})
return source
} catch (error) {
return ''
}
}
module.exports()