-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved HTTP content highlighting (#1598)
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
1 parent
2288c25
commit 1b75da9
Showing
9 changed files
with
218 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/languages/javascript+http/javascript_inclusion.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters