Skip to content

Commit d4be4f5

Browse files
elevatebartsapegin
authored andcommitted
Chore: Allow chunkify to accept other extensions than js, jsx and html (styleguidist#1140)
1 parent 0e6e95a commit d4be4f5

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

loaders/examples-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function examplesLoader(source) {
3030
: undefined;
3131

3232
// Load examples
33-
const examples = chunkify(source, updateExample);
33+
const examples = chunkify(source, updateExample, query.customLangs);
3434

3535
// We're analysing the examples' source code to figure out the require statements. We do it manually with regexes,
3636
// because webpack unfortunately doesn't expose its smart logic for rewriting requires

loaders/utils/__tests__/__snapshots__/chunkify.spec.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`should even parse examples with custom extensions 1`] = `
4+
Array [
5+
Object {
6+
"content": "Custom extensions",
7+
"type": "markdown",
8+
},
9+
Object {
10+
"content":
11+
<AppButton>
12+
Example in vue
13+
</AppButton>
14+
,
15+
"settings": Object {},
16+
"type": "code",
17+
},
18+
]
19+
`;
20+
321
exports[`should parse examples settings correctly 1`] = `
422
Array [
523
Object {
@@ -54,6 +72,30 @@ Array [
5472
]
5573
`;
5674
75+
exports[`should parse undefined custom extensions without throwing 1`] = `
76+
Array [
77+
Object {
78+
"content": "Undefined extensions (default)",
79+
"type": "markdown",
80+
},
81+
Object {
82+
"content":
83+
<AppButton>
84+
Example in jsx with undefined extensions
85+
</AppButton>
86+
,
87+
"settings": Object {},
88+
"type": "code",
89+
},
90+
Object {
91+
"content": "\`\`\`pizza
92+
Unknown language: \\"pizza\\"
93+
\`\`\`",
94+
"type": "markdown",
95+
},
96+
]
97+
`;
98+
5799
exports[`should separate Markdown and component examples 1`] = `
58100
Array [
59101
Object {

loaders/utils/__tests__/chunkify.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ it('should call updateExample function for example', () => {
127127
const actual = chunkify(markdown, updateExample);
128128
expect(actual).toEqual(expected);
129129
});
130+
131+
it('should even parse examples with custom extensions', () => {
132+
const markdown = `
133+
Custom extensions
134+
135+
\`\`\`vue
136+
<AppButton>Example in vue</AppButton>
137+
\`\`\`
138+
`;
139+
const actual = chunkify(markdown, undefined, ['vue']);
140+
expect(actual).toMatchSnapshot();
141+
});
142+
143+
it('should parse undefined custom extensions without throwing', () => {
144+
const markdown = `
145+
Undefined extensions (default)
146+
147+
\`\`\`jsx
148+
<AppButton>Example in jsx with undefined extensions</AppButton>
149+
\`\`\`
150+
151+
\`\`\`pizza
152+
<AppButton>Example in pizza with undefined extensions (test double)</AppButton>
153+
\`\`\`
154+
`;
155+
const actual = chunkify(markdown, undefined, undefined);
156+
expect(actual).toMatchSnapshot();
157+
});

loaders/utils/chunkify.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const CODE_PLACEHOLDER = '<%{#code#}%>';
1111
*
1212
* @param {string} markdown
1313
* @param {Function} updateExample
14+
* @param {Array<string>} playgroundLangs
1415
* @returns {Array}
1516
*/
16-
module.exports = function chunkify(markdown, updateExample) {
17+
module.exports = function chunkify(markdown, updateExample, playgroundLangs = PLAYGROUND_LANGS) {
1718
const codeChunks = [];
1819

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

3536
const lang = example.lang;
3637
node.lang = lang;
37-
if (!lang || (PLAYGROUND_LANGS.indexOf(lang) !== -1 && !example.settings.static)) {
38+
if (!lang || (playgroundLangs.indexOf(lang) !== -1 && !example.settings.static)) {
3839
codeChunks.push({
3940
type: 'code',
4041
content: example.content,

0 commit comments

Comments
 (0)