UNPKG

2.12 kBJavaScriptView Raw
1(function (Prism) {
2 Prism.languages.http = {
3 'request-line': {
4 pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,
5 inside: {
6 // HTTP Verb
7 'property': /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,
8 // Path or query argument
9 'attr-name': /:\w+/
10 }
11 },
12 'response-status': {
13 pattern: /^HTTP\/1.[01] \d+.*/m,
14 inside: {
15 // Status, e.g. 200 OK
16 'property': {
17 pattern: /(^HTTP\/1.[01] )\d+.*/i,
18 lookbehind: true
19 }
20 }
21 },
22 // HTTP header name
23 'header-name': {
24 pattern: /^[\w-]+:(?=.)/m,
25 alias: 'keyword'
26 }
27 };
28
29 // Create a mapping of Content-Type headers to language definitions
30 var langs = Prism.languages;
31 var httpLanguages = {
32 'application/javascript': langs.javascript,
33 'application/json': langs.json || langs.javascript,
34 'application/xml': langs.xml,
35 'text/xml': langs.xml,
36 'text/html': langs.html,
37 'text/css': langs.css
38 };
39
40 // Declare which types can also be suffixes
41 var suffixTypes = {
42 'application/json': true,
43 'application/xml': true
44 };
45
46 /**
47 * Returns a pattern for the given content type which matches it and any type which has it as a suffix.
48 *
49 * @param {string} contentType
50 * @returns {string}
51 */
52 function getSuffixPattern(contentType) {
53 var suffix = contentType.replace(/^[a-z]+\//, '');
54 var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])';
55 return '(?:' + contentType + '|' + suffixPattern + ')';
56 }
57
58 // Insert each content type parser that has its associated language
59 // currently loaded.
60 var options;
61 for (var contentType in httpLanguages) {
62 if (httpLanguages[contentType]) {
63 options = options || {};
64
65 var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
66 options[contentType.replace(/\//g, '-')] = {
67 pattern: RegExp('(content-type:\\s*' + pattern + '[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
68 lookbehind: true,
69 inside: httpLanguages[contentType]
70 };
71 }
72 }
73 if (options) {
74 Prism.languages.insertBefore('http', 'header-name', options);
75 }
76
77}(Prism));