Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 89205f2

Browse files
committed
feat(mdx): add parser and modify classes
1 parent 8737a6f commit 89205f2

File tree

4 files changed

+160
-14
lines changed

4 files changed

+160
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`fusuma-loader: commentParser should return aaa 1`] = `
4+
Object {
5+
"prefix": "zzzzz",
6+
"valueArr": Array [
7+
"test1",
8+
"test2",
9+
],
10+
"valueStr": "test1
11+
test2",
12+
}
13+
`;
14+
15+
exports[`fusuma-loader: commentParser should return key case 1`] = `
16+
Object {
17+
"prefix": "key",
18+
"valueArr": Array [],
19+
"valueStr": "",
20+
}
21+
`;
22+
23+
exports[`fusuma-loader: commentParser should return key case 2`] = `
24+
Object {
25+
"prefix": "key",
26+
"valueArr": Array [],
27+
"valueStr": "",
28+
}
29+
`;
30+
31+
exports[`fusuma-loader: commentParser should return key: foo,bar,baz case 1`] = `
32+
Object {
33+
"prefix": "key",
34+
"valueArr": Array [
35+
"foo",
36+
"bar",
37+
"baz",
38+
],
39+
"valueStr": "foo,bar,baz",
40+
}
41+
`;
42+
43+
exports[`fusuma-loader: commentParser should return key: foo,bar,baz case 2`] = `
44+
Object {
45+
"prefix": "key",
46+
"valueArr": Array [
47+
"foo",
48+
"bar",
49+
"baz",
50+
],
51+
"valueStr": "foo,bar,baz",
52+
}
53+
`;
54+
55+
exports[`fusuma-loader: commentParser should return key: value case 1`] = `
56+
Object {
57+
"prefix": "key",
58+
"valueArr": Array [
59+
"value",
60+
],
61+
"valueStr": "value",
62+
}
63+
`;
64+
65+
exports[`fusuma-loader: commentParser should return key: value case 2`] = `
66+
Object {
67+
"prefix": "key",
68+
"valueArr": Array [
69+
"value",
70+
],
71+
"valueStr": "value",
72+
}
73+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const commentParser = require('../src/commentParser');
4+
5+
describe('fusuma-loader: commentParser', () => {
6+
test('should return key: value case', () => {
7+
{
8+
const input = 'key: value';
9+
10+
expect(commentParser(input)).toMatchSnapshot();
11+
}
12+
13+
{
14+
const input = 'key:value';
15+
16+
expect(commentParser(input)).toMatchSnapshot();
17+
}
18+
});
19+
20+
test('should return key: foo,bar,baz case', () => {
21+
{
22+
const input = 'key: foo,bar,baz';
23+
24+
expect(commentParser(input)).toMatchSnapshot();
25+
}
26+
27+
{
28+
const input = 'key:foo, bar, baz';
29+
30+
expect(commentParser(input)).toMatchSnapshot();
31+
}
32+
});
33+
34+
test('should return key case', () => {
35+
{
36+
const input = 'key';
37+
38+
expect(commentParser(input)).toMatchSnapshot();
39+
}
40+
41+
{
42+
const input = ' key ';
43+
44+
expect(commentParser(input)).toMatchSnapshot();
45+
}
46+
});
47+
48+
test('should return aaa', () => {
49+
const input = `zzzzz
50+
test1
51+
test2`;
52+
53+
expect(commentParser(input)).toMatchSnapshot();
54+
});
55+
});
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
function commentParser(text) {
4+
const [p, ...rest] = text.trim().split(':');
5+
const [prefix, ...context] = p.split('\n');
6+
const attr = rest.map((r) => r.trim());
7+
const res = {
8+
prefix,
9+
valueArr: attr,
10+
valueStr: attr.join(''),
11+
};
12+
13+
// e.g. note
14+
if (context.length) {
15+
res.valueArr = context;
16+
res.valueStr = context.join('\n');
17+
} else if (attr.length && attr[0].includes(',')) {
18+
res.valueArr = attr[0].split(',').map((v) => v.trim());
19+
res.valueStr = res.valueArr.join(',');
20+
}
21+
22+
return res;
23+
}
24+
25+
module.exports = commentParser;

packages/mdx-loader/src/mdxPlugin.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const transformChartToJSX = require('./transformers/transformChartToJSX');
99
const transformMarkdownImageNodeToJSX = require('./transformers/transformMarkdownImageNodeToJSX');
1010
const transformExecJSCodeButtonToJSX = require('./transformers/transformExecJSCodeButtonToJSX');
1111
const escapeMap = require('./escapeMap');
12+
const commentParser = require('./commentParser');
1213

1314
function mdxPlugin() {
1415
return (tree) => {
@@ -33,40 +34,32 @@ function mdxPlugin() {
3334
}
3435

3536
if (type === 'comment') {
36-
const [p, ...rest] = value.trim().split(':');
37-
const prefix = p.split('\n')[0];
38-
const attr = rest.map((r) => r.trim());
37+
const { prefix, valueStr, valueArr } = commentParser(value);
3938

4039
if (prefix === 'background') {
41-
background = attr[0].includes('/') ? `require(${attr[0]})` : `'${attr[0]}'`;
40+
background = valueStr.includes('/') ? `require(${valueStr})` : `'${valueStr}'`;
4241
return;
4342
}
4443
if (prefix === 'section-title') {
45-
props.sectionTitle = attr.join('');
44+
props.sectionTitle = valueStr;
4645
return;
4746
}
4847
if (prefix === 'classes') {
49-
props.classes = attr.join(' ');
48+
props.classes = valueArr;
5049
return;
5150
}
5251
if (prefix === 'contents') {
5352
props.contents = true;
5453
return;
5554
}
5655
if (prefix === 'note') {
57-
const [, ...note] = p.split('\n');
58-
59-
props.note = note
60-
.join('')
61-
.replace(/[&<>"']/gim, (m) => escapeMap[m])
62-
.replace(/\n/g, '\\n');
63-
56+
props.note = valueStr.replace(/[&<>"']/gim, (m) => escapeMap[m]).replace(/\n/g, '\\n');
6457
return;
6558
}
6659
if (prefix === 'qr') {
6760
slide.push({
6861
...n,
69-
...transformQrToJSX(attr),
62+
...transformQrToJSX(valueArr),
7063
});
7164
return;
7265
}

0 commit comments

Comments
 (0)