2022-11-09 12:28:04 +08:00
|
|
|
import assert from 'assert';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { HastRoot, UnifiedTransformer } from 'dumi';
|
|
|
|
import { unistUtilVisit } from 'dumi';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* plugin for modify hast tree when docs compiling
|
|
|
|
*/
|
|
|
|
function rehypeAntd(): UnifiedTransformer<HastRoot> {
|
|
|
|
return (tree, vFile) => {
|
2022-11-30 11:05:41 +08:00
|
|
|
const { filename } = vFile.data.frontmatter as any;
|
2022-11-21 17:39:46 +08:00
|
|
|
|
2023-08-01 10:53:55 +08:00
|
|
|
unistUtilVisit.visit(tree, 'element', (node, i, parent) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
if (node.tagName === 'DumiDemoGrid') {
|
|
|
|
// replace DumiDemoGrid to DemoWrapper, to implement demo toolbar
|
|
|
|
node.tagName = 'DemoWrapper';
|
|
|
|
} else if (node.tagName === 'ResourceCards') {
|
|
|
|
const propNames = ['title', 'cover', 'description', 'src', 'official'];
|
|
|
|
const contentNode = node.children[0];
|
|
|
|
|
|
|
|
assert(
|
|
|
|
contentNode.type === 'text',
|
2022-11-21 17:39:46 +08:00
|
|
|
`ResourceCards content must be plain text!\nat ${filename}`,
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// clear children
|
|
|
|
node.children = [];
|
|
|
|
|
|
|
|
// generate JSX props
|
|
|
|
(node as any).JSXAttributes = [
|
|
|
|
{
|
|
|
|
type: 'JSXAttribute',
|
|
|
|
name: 'resources',
|
|
|
|
value: JSON.stringify(
|
|
|
|
contentNode.value
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.reduce<any>((acc, cur) => {
|
|
|
|
// match text from ` - 桌面组件 Sketch 模板包`
|
|
|
|
const [, isProp, val] = cur.match(/(\s+)?-\s(.+)/)!;
|
|
|
|
|
|
|
|
if (!isProp) {
|
|
|
|
// create items when match title
|
|
|
|
acc.push({ [propNames[0]]: val });
|
|
|
|
} else {
|
|
|
|
// add props when match others
|
|
|
|
const prev = acc[acc.length - 1];
|
|
|
|
|
|
|
|
prev[propNames[Object.keys(prev).length]] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
2022-11-21 17:39:46 +08:00
|
|
|
} else if (
|
|
|
|
node.type === 'element' &&
|
|
|
|
node.tagName === 'Table' &&
|
|
|
|
/^components/.test(filename)
|
|
|
|
) {
|
|
|
|
if (!node.properties) return;
|
|
|
|
node.properties.className ??= [];
|
|
|
|
(node.properties.className as string[]).push('component-api-table');
|
2023-05-16 08:42:18 +08:00
|
|
|
} else if (node.type === 'element' && (node.tagName === 'Link' || node.tagName === 'a')) {
|
|
|
|
const { tagName } = node;
|
2023-09-17 21:52:01 +08:00
|
|
|
node.properties!.sourceType = tagName;
|
2023-05-16 08:42:18 +08:00
|
|
|
node.tagName = 'LocaleLink';
|
2023-07-18 18:22:47 +08:00
|
|
|
} else if (node.type === 'element' && node.tagName === 'video') {
|
|
|
|
node.tagName = 'VideoPlayer';
|
2023-08-01 10:53:55 +08:00
|
|
|
} else if (node.tagName === 'SourceCode') {
|
2023-09-17 21:52:01 +08:00
|
|
|
const { lang } = node.properties!;
|
2023-08-07 14:24:05 +08:00
|
|
|
|
2023-08-04 19:27:38 +08:00
|
|
|
if (typeof lang === 'string' && lang.startsWith('sandpack')) {
|
2023-08-07 14:24:05 +08:00
|
|
|
const code = (node.children[0] as any).value as string;
|
|
|
|
const configRegx = /^const sandpackConfig = ([\S\s]*?});/;
|
|
|
|
const [configString] = code.match(configRegx) || [];
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
const config = configString && eval(`(${configString.replace(configRegx, '$1')})`);
|
|
|
|
Object.keys(config || {}).forEach((key) => {
|
|
|
|
if (typeof config[key] === 'object') {
|
|
|
|
config[key] = JSON.stringify(config[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-01 10:53:55 +08:00
|
|
|
parent!.children.splice(i!, 1, {
|
|
|
|
type: 'element',
|
|
|
|
tagName: 'Sandpack',
|
2023-08-04 19:27:38 +08:00
|
|
|
properties: {
|
2023-08-07 14:24:05 +08:00
|
|
|
...config,
|
2023-08-04 19:27:38 +08:00
|
|
|
},
|
2023-08-01 10:53:55 +08:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
type: 'text',
|
2023-08-07 14:24:05 +08:00
|
|
|
value: code.replace(configRegx, '').trim(),
|
2023-08-01 10:53:55 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default rehypeAntd;
|