forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-remark-prismjs): add support for language extensions (gat…
…sbyjs#11932) * Extended 'gatsby-remark-prismjs' plugin to support adding new language definitions and extend current languages.
- Loading branch information
1 parent
f7e2dd5
commit a20afb1
Showing
8 changed files
with
371 additions
and
7 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
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 |
---|---|---|
|
@@ -631,3 +631,4 @@ color<span class=\\"token punctuation\\">:</span> red<span class=\\"token punctu | |
"type": "root", | ||
} | ||
`; | ||
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
143 changes: 143 additions & 0 deletions
143
packages/gatsby-remark-prismjs/src/__tests__/load-prism-language-extension.js
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,143 @@ | ||
const loadLanguageExtension = require(`../load-prism-language-extension`) | ||
const Prism = require(`prismjs`) | ||
|
||
describe(`extend/add prism language`, () => { | ||
it(`should throw an error if the request is not an array or an object`, () => { | ||
let request = 4 | ||
|
||
expect(() => loadLanguageExtension(request)).toThrow() | ||
|
||
request = `A weird string value, instead of an object or array` | ||
|
||
expect(() => loadLanguageExtension(request)).toThrow() | ||
}) | ||
it(`should not throw an error if the request is an array`, () => { | ||
let request = [] | ||
|
||
expect(() => loadLanguageExtension(request)).not.toThrow() | ||
}) | ||
it(`should not throw an error if the request is a valid object (containing 'language' and 'definition')`, () => { | ||
let request = { | ||
language: `aTypicalLanguage`, | ||
definition: /aRegexp/, | ||
} | ||
|
||
expect(() => loadLanguageExtension(request)).not.toThrow() | ||
}) | ||
it(`should throw an error if the request is not a valid object (containing 'language' and 'definition')`, () => { | ||
let request = {} | ||
|
||
expect(() => loadLanguageExtension(request)).toThrow() | ||
}) | ||
it(`should throw an error if the request is an array containing an invalid object (not containing 'language' and 'definition')`, () => { | ||
let request = [ | ||
{ | ||
language: `aTypicalLanguage`, | ||
definition: /aRegexp/, | ||
}, | ||
{}, | ||
] | ||
|
||
expect(() => loadLanguageExtension(request)).toThrow() | ||
}) | ||
it(`should extend pre-loaded language`, () => { | ||
const request = { | ||
extend: `clike`, | ||
definition: { | ||
flexc_keyword: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
} | ||
|
||
loadLanguageExtension(request) | ||
|
||
expect(Prism.languages[request.extend]).toBeDefined() | ||
expect(Prism.languages[request.extend]).toHaveProperty(`flexc_keyword`) | ||
expect(Prism.languages[request.extend][`flexc_keyword`]).toEqual( | ||
request.definition.flexc_keyword | ||
) | ||
}) | ||
it(`should extend not pre-loaded language`, () => { | ||
const request = { | ||
extend: `c`, | ||
definition: { | ||
flexc_keyword: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
} | ||
|
||
loadLanguageExtension(request) | ||
|
||
expect(Prism.languages[request.extend]).toBeDefined() | ||
expect(Prism.languages[request.extend]).toHaveProperty(`flexc_keyword`) | ||
expect(Prism.languages[request.extend][`flexc_keyword`]).toEqual( | ||
request.definition.flexc_keyword | ||
) | ||
}) | ||
it(`should add new language from existing language`, () => { | ||
const request = { | ||
language: `flexc`, | ||
extend: `c`, | ||
definition: { | ||
flexc_keyword: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
} | ||
|
||
const languagesBeforeLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).not.toHaveProperty(request.language) | ||
|
||
loadLanguageExtension(request) | ||
|
||
let languagesAfterLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).toHaveProperty(request.language) | ||
expect(languagesAfterLoaded.length).toBe(languagesBeforeLoaded.length + 1) | ||
expect(Prism.languages[request.language][`flexc_keyword`]).toEqual( | ||
request.definition.flexc_keyword | ||
) | ||
}) | ||
it(`should add new language`, () => { | ||
const request = { | ||
language: `flexc2`, //Check if it is possible to reset scope somehow, instead of giving a new name. | ||
definition: { | ||
flexc_keyword: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
} | ||
|
||
const languagesBeforeLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).not.toHaveProperty(request.language) | ||
|
||
loadLanguageExtension(request) | ||
|
||
let languagesAfterLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).toHaveProperty(request.language) | ||
expect(languagesAfterLoaded.length).toBe(languagesBeforeLoaded.length + 1) | ||
expect(Prism.languages[request.language][`flexc_keyword`]).toEqual( | ||
request.definition.flexc_keyword | ||
) | ||
}) | ||
it(`should work to make two requests by sending an array`, () => { | ||
const request = [ | ||
{ | ||
language: `flexc3`, //Check if it is possible to reset scope somehow, instead of giving a new name. | ||
definition: { | ||
flexc_keyword: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
}, | ||
{ | ||
extend: `c`, | ||
definition: { | ||
new_token: `(__cm|__circ|_lpp_indirect|__accum|__size_t|__ptrdiff_t|__wchar_t|__fixed|__abscall|__extcall|__stkcall|__sat|__i64_t|__i32_t|__i16_t|__r32_t|__r16_t|__u64_t|__u32_t|__u16_t|__a40_t|__a24_t)`, | ||
}, | ||
}, | ||
] | ||
|
||
const languagesBeforeLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).not.toHaveProperty(`flexc3`) | ||
expect(Prism.languages[`c`]).not.toHaveProperty(`new_token`) | ||
|
||
loadLanguageExtension(request) | ||
|
||
let languagesAfterLoaded = Object.keys(Prism.languages) | ||
expect(Prism.languages).toHaveProperty(`flexc3`) | ||
expect(languagesAfterLoaded.length).toBe(languagesBeforeLoaded.length + 1) | ||
expect(Prism.languages[`c`]).toHaveProperty(`new_token`) | ||
}) | ||
}) |
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
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
Oops, something went wrong.