Skip to content

Commit

Permalink
Chore: Allow chunkify to accept other extensions than js, jsx and html (
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart authored and sapegin committed Sep 18, 2018
1 parent 0e6e95a commit d4be4f5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
2 changes: 1 addition & 1 deletion loaders/examples-loader.js
Expand Up @@ -30,7 +30,7 @@ function examplesLoader(source) {
: undefined;

// Load examples
const examples = chunkify(source, updateExample);
const examples = chunkify(source, updateExample, query.customLangs);

// We're analysing the examples' source code to figure out the require statements. We do it manually with regexes,
// because webpack unfortunately doesn't expose its smart logic for rewriting requires
Expand Down
42 changes: 42 additions & 0 deletions loaders/utils/__tests__/__snapshots__/chunkify.spec.js.snap
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should even parse examples with custom extensions 1`] = `
Array [
Object {
"content": "Custom extensions",
"type": "markdown",
},
Object {
"content":
<AppButton>
Example in vue
</AppButton>
,
"settings": Object {},
"type": "code",
},
]
`;

exports[`should parse examples settings correctly 1`] = `
Array [
Object {
Expand Down Expand Up @@ -54,6 +72,30 @@ Array [
]
`;
exports[`should parse undefined custom extensions without throwing 1`] = `
Array [
Object {
"content": "Undefined extensions (default)",
"type": "markdown",
},
Object {
"content":
<AppButton>
Example in jsx with undefined extensions
</AppButton>
,
"settings": Object {},
"type": "code",
},
Object {
"content": "\`\`\`pizza
Unknown language: \\"pizza\\"
\`\`\`",
"type": "markdown",
},
]
`;
exports[`should separate Markdown and component examples 1`] = `
Array [
Object {
Expand Down
28 changes: 28 additions & 0 deletions loaders/utils/__tests__/chunkify.spec.js
Expand Up @@ -127,3 +127,31 @@ it('should call updateExample function for example', () => {
const actual = chunkify(markdown, updateExample);
expect(actual).toEqual(expected);
});

it('should even parse examples with custom extensions', () => {
const markdown = `
Custom extensions
\`\`\`vue
<AppButton>Example in vue</AppButton>
\`\`\`
`;
const actual = chunkify(markdown, undefined, ['vue']);
expect(actual).toMatchSnapshot();
});

it('should parse undefined custom extensions without throwing', () => {
const markdown = `
Undefined extensions (default)
\`\`\`jsx
<AppButton>Example in jsx with undefined extensions</AppButton>
\`\`\`
\`\`\`pizza
<AppButton>Example in pizza with undefined extensions (test double)</AppButton>
\`\`\`
`;
const actual = chunkify(markdown, undefined, undefined);
expect(actual).toMatchSnapshot();
});
5 changes: 3 additions & 2 deletions loaders/utils/chunkify.js
Expand Up @@ -11,9 +11,10 @@ const CODE_PLACEHOLDER = '<%{#code#}%>';
*
* @param {string} markdown
* @param {Function} updateExample
* @param {Array<string>} playgroundLangs
* @returns {Array}
*/
module.exports = function chunkify(markdown, updateExample) {
module.exports = function chunkify(markdown, updateExample, playgroundLangs = PLAYGROUND_LANGS) {
const codeChunks = [];

/*
Expand All @@ -34,7 +35,7 @@ module.exports = function chunkify(markdown, updateExample) {

const lang = example.lang;
node.lang = lang;
if (!lang || (PLAYGROUND_LANGS.indexOf(lang) !== -1 && !example.settings.static)) {
if (!lang || (playgroundLangs.indexOf(lang) !== -1 && !example.settings.static)) {
codeChunks.push({
type: 'code',
content: example.content,
Expand Down

0 comments on commit d4be4f5

Please sign in to comment.