Skip to content

Commit

Permalink
Improved HTTP content highlighting (#1598)
Browse files Browse the repository at this point in the history
Extends the content highlighting capabilities of HTTP.

1. Add support for `application/javascript` and `text/css` content types.
2. Change the languages associated with certain types:
    1. `application/json` now uses `json` with `javascript` as a backup instead of just `javascript`.
    2. All XML types now use `Prism.languages.xml` instead of `markup`.
    3. The HTML type now uses `html` instead of `markup`.
3. Add support for highlighting based on [suffixes](https://en.wikipedia.org/wiki/Media_type#Suffix) for XML and JSON.
    E.g. `image/svg+xml`, `application/xhtml+xml`, `application/atom+xml` will use `xml` and `application/calendar+json` will use `json`.
4. Stylistic changes:
    1. Fix indentation.
    2. Use single quotes for token names.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Dec 1, 2018
1 parent 2288c25 commit 1b75da9
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 62 deletions.
119 changes: 74 additions & 45 deletions components/prism-http.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,79 @@
Prism.languages.http = {
'request-line': {
pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,
inside: {
// HTTP Verb
property: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,
// Path or query argument
'attr-name': /:\w+/
}
},
'response-status': {
pattern: /^HTTP\/1.[01] \d+.*/m,
inside: {
// Status, e.g. 200 OK
property: {
pattern: /(^HTTP\/1.[01] )\d+.*/i,
lookbehind: true
}
(function (Prism) {
Prism.languages.http = {
'request-line': {
pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,
inside: {
// HTTP Verb
'property': /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,
// Path or query argument
'attr-name': /:\w+/
}
},
'response-status': {
pattern: /^HTTP\/1.[01] \d+.*/m,
inside: {
// Status, e.g. 200 OK
'property': {
pattern: /(^HTTP\/1.[01] )\d+.*/i,
lookbehind: true
}
}
},
// HTTP header name
'header-name': {
pattern: /^[\w-]+:(?=.)/m,
alias: 'keyword'
}
},
// HTTP header name
'header-name': {
pattern: /^[\w-]+:(?=.)/m,
alias: 'keyword'
}
};
};

// Create a mapping of Content-Type headers to language definitions
var httpLanguages = {
'application/json': Prism.languages.javascript,
'application/xml': Prism.languages.markup,
'text/xml': Prism.languages.markup,
'text/html': Prism.languages.markup
};
// Create a mapping of Content-Type headers to language definitions
var langs = Prism.languages;
var httpLanguages = {
'application/javascript': langs.javascript,
'application/json': langs.json || langs.javascript,
'application/xml': langs.xml,
'text/xml': langs.xml,
'text/html': langs.html,
'text/css': langs.css
};

// Insert each content type parser that has its associated language
// currently loaded.
for (var contentType in httpLanguages) {
if (httpLanguages[contentType]) {
var options = {};
options[contentType] = {
pattern: RegExp('(content-type:\\s*' + contentType + '[\\w\\W]*?)(?:\\r?\\n|\\r){2}[\\w\\W]*', 'i'),
lookbehind: true,
inside: {
rest: httpLanguages[contentType]
}
};
// Declare which types can also be suffixes
var suffixTypes = {
'application/json': true,
'application/xml': true
};

/**
* Returns a pattern for the given content type which matches it and any type which has it as a suffix.
*
* @param {string} contentType
* @returns {string}
*/
function getSuffixPattern(contentType) {
var suffix = contentType.replace(/^[a-z]+\//, '');
var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])';
return '(?:' + contentType + '|' + suffixPattern + ')';
}

// Insert each content type parser that has its associated language
// currently loaded.
var options;
for (var contentType in httpLanguages) {
if (httpLanguages[contentType]) {
options = options || {};

var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
options[contentType] = {
pattern: RegExp('(content-type:\\s*' + pattern + '[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
lookbehind: true,
inside: {
rest: httpLanguages[contentType]
}
};
}
}
if (options) {
Prism.languages.insertBefore('http', 'header-name', options);
}
}

}(Prism));
2 changes: 1 addition & 1 deletion components/prism-http.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/languages/css+http/css_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Content-type: text/css

a.link:hover {
color: red;
}

----------------------------------------------------

[
["header-name", "Content-type:"],
" text/css",
["text/css", [
["selector", "a.link:hover"],
["punctuation", "{"],
["property", "color"],
["punctuation", ":"],
" red",
["punctuation", ";"],
["punctuation", "}"]
]]
]

----------------------------------------------------

Checks for JavaScript content in HTTP.
20 changes: 10 additions & 10 deletions tests/languages/javascript+http/javascript_inclusion.test
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
Content-type: application/json
Content-type: application/javascript

{"foo":"bar"}
var a = true;

----------------------------------------------------

[
["header-name", "Content-type:"],
" application/json",
["application/json", [
["punctuation", "{"],
["string", "\"foo\""],
["punctuation", ":"],
["string", "\"bar\""],
["punctuation", "}"]
" application/javascript",
["application/javascript", [
["keyword", "var"],
" a ",
["operator", "="],
["boolean", "true"],
["punctuation", ";"]
]]
]

----------------------------------------------------

Checks for JSON content in HTTP.
Checks for JavaScript content in HTTP.
21 changes: 21 additions & 0 deletions tests/languages/json+http/json-suffix_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Content-type: application/x.foo+bar+json

{"foo":"bar"}

----------------------------------------------------

[
["header-name", "Content-type:"],
" application/x.foo+bar+json",
["application/json", [
["punctuation", "{"],
["property", "\"foo\""],
["operator", ":"],
["string", "\"bar\""],
["punctuation", "}"]
]]
]

----------------------------------------------------

Checks for content with JSON suffix in HTTP.
21 changes: 21 additions & 0 deletions tests/languages/json+http/json_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Content-type: application/json

{"foo":"bar"}

----------------------------------------------------

[
["header-name", "Content-type:"],
" application/json",
["application/json", [
["punctuation", "{"],
["property", "\"foo\""],
["operator", ":"],
["string", "\"bar\""],
["punctuation", "}"]
]]
]

----------------------------------------------------

Checks for JSON content in HTTP.
30 changes: 30 additions & 0 deletions tests/languages/markup+http/html_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Content-type: text/html

<b></b>

----------------------------------------------------

[
["header-name", "Content-type:"],
" text/html",
["text/html", [
["tag", [
["tag", [
["punctuation", "<"],
"b"
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"b"
]],
["punctuation", ">"]
]]
]]
]

----------------------------------------------------

Checks for HTML content in HTTP.
30 changes: 30 additions & 0 deletions tests/languages/markup+http/xml-suffix_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Content-type: text/x.anything+something-else+xml

<foo></foo>

----------------------------------------------------

[
["header-name", "Content-type:"],
" text/x.anything+something-else+xml",
["application/xml", [
["tag", [
["tag", [
["punctuation", "<"],
"foo"
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
]]
]

----------------------------------------------------

Checks for content with XML suffix in HTTP.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Content-type: application/xml
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
]]
]

Expand Down

0 comments on commit 1b75da9

Please sign in to comment.