forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prism-typescript.js
42 lines (35 loc) · 1.85 KB
/
prism-typescript.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
(function (Prism) {
Prism.languages.typescript = Prism.languages.extend('javascript', {
'class-name': {
pattern: /(\b(?:class|extends|implements|instanceof|interface|new|type)\s+)(?!keyof\b)[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?:\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>)?/,
lookbehind: true,
greedy: true,
inside: null // see below
},
// From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words
'keyword': /\b(?:abstract|as|asserts|async|await|break|case|catch|class|const|constructor|continue|debugger|declare|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|is|keyof|let|module|namespace|new|null|of|package|private|protected|public|readonly|return|require|set|static|super|switch|this|throw|try|type|typeof|undefined|var|void|while|with|yield)\b/,
'builtin': /\b(?:string|Function|any|number|boolean|Array|symbol|console|Promise|unknown|never)\b/,
});
// doesn't work with TS because TS is too complex
delete Prism.languages.typescript['parameter'];
// a version of typescript specifically for highlighting types
var typeInside = Prism.languages.extend('typescript', {});
delete typeInside['class-name'];
Prism.languages.typescript['class-name'].inside = typeInside;
Prism.languages.insertBefore('typescript', 'function', {
'generic-function': {
// e.g. foo<T extends "bar" | "baz">( ...
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>(?=\s*\()/,
greedy: true,
inside: {
'function': /^#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
'generic': {
pattern: /<[\s\S]+/, // everything after the first <
alias: 'class-name',
inside: typeInside
}
}
}
});
Prism.languages.ts = Prism.languages.typescript;
}(Prism));