Skip to content

Commit db568f4

Browse files
committed
refactor: reimplement containerSyntax
1 parent 1b63d32 commit db568f4

File tree

8 files changed

+437
-541
lines changed

8 files changed

+437
-541
lines changed

packages/core/src/node/mdx/remarkPlugins/__snapshots__/containerSyntax.test.ts.snap

Lines changed: 356 additions & 497 deletions
Large diffs are not rendered by default.

packages/core/src/node/mdx/remarkPlugins/builtin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path';
22
import type { Root } from 'mdast';
33
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
44
import type { Plugin } from 'unified';
5-
import { getASTNodeImport } from '../../utils';
5+
import { getDefaultImportAstNode } from '../../utils';
66

77
/**
88
* A remark plugin to import all builtin components.
@@ -16,7 +16,7 @@ export const remarkBuiltin: Plugin<[{ globalComponents: string[] }], Root> = ({
1616
const demos: MdxjsEsm[] = globalComponents.map(componentPath => {
1717
const filename = path.parse(componentPath).name;
1818
const componentName = filename[0].toUpperCase() + filename.slice(1);
19-
return getASTNodeImport(componentName, componentPath);
19+
return getDefaultImportAstNode(componentName, componentPath);
2020
});
2121

2222
tree.children.unshift(...demos);

packages/core/src/node/mdx/remarkPlugins/containerSyntax.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
} from 'mdast';
2424
import type { ContainerDirective } from 'mdast-util-directive';
2525
import type { Plugin } from 'unified';
26+
import { getNamedImportAstNode } from '../../utils';
2627

2728
export const DIRECTIVE_TYPES = [
2829
'tip',
@@ -39,6 +40,8 @@ export const REGEX_GH_BEGIN = /^\s*\s*\[!(\w+)\]\s*(.*)?/;
3940
export const TITLE_REGEX_IN_MD = /{\s*title=["']?(.+)}\s*/;
4041
export const TITLE_REGEX_IN_MDX = /\s*title=["']?(.+)\s*/;
4142

43+
const CALLOUT_COMPONENT = '$$$Callout$$$';
44+
4245
export type DirectiveType = (typeof DIRECTIVE_TYPES)[number];
4346

4447
const trimTailingQuote = (str: string) => str.replace(/['"]$/g, '');
@@ -79,13 +82,13 @@ const createContainer = (
7982
): ContainerDirective => {
8083
return {
8184
type: 'containerDirective',
82-
name: 'Callout',
85+
name: CALLOUT_COMPONENT,
8386
attributes: {
8487
type: type,
8588
title: title || getTypeName(type),
8689
},
8790
data: {
88-
hName: 'Callout',
91+
hName: CALLOUT_COMPONENT,
8992
hProperties: {
9093
type: type,
9194
title: title || getTypeName(type),
@@ -355,5 +358,10 @@ function transformer(tree: Parent) {
355358
}
356359

357360
export const remarkContainerSyntax: Plugin<[], Root> = () => {
358-
return transformer;
361+
return tree => {
362+
transformer(tree);
363+
tree.children.unshift(
364+
getNamedImportAstNode('Callout', CALLOUT_COMPONENT, '@theme'),
365+
);
366+
};
359367
};

packages/core/src/node/mdx/remarkPlugins/image.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Root } from 'mdast';
44
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
55
import type { Plugin } from 'unified';
66
import { visit } from 'unist-util-visit';
7-
import { getASTNodeImport } from '../../utils';
7+
import { getDefaultImportAstNode } from '../../utils';
88

99
const normalizeImageUrl = (imageUrl: string): string => {
1010
if (isExternalUrl(imageUrl) || imageUrl.startsWith('/')) {
@@ -69,7 +69,7 @@ export const remarkImage: Plugin<[], Root> = () => (tree, _file) => {
6969
].filter(Boolean),
7070
});
7171

72-
images.push(getASTNodeImport(tempVariableName, imagePath));
72+
images.push(getDefaultImportAstNode(tempVariableName, imagePath));
7373
});
7474

7575
visit(tree, node => {
@@ -98,7 +98,7 @@ export const remarkImage: Plugin<[], Root> = () => (tree, _file) => {
9898

9999
Object.assign(srcAttr, getMdxSrcAttribute(tempVariableName));
100100

101-
images.push(getASTNodeImport(tempVariableName, imagePath));
101+
images.push(getDefaultImportAstNode(tempVariableName, imagePath));
102102
});
103103

104104
tree.children.unshift(...images);

packages/core/src/node/utils/getASTNodeImport.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
2+
3+
// Construct import statement for AST
4+
// Such as: import image1 from './test.png'
5+
export const getDefaultImportAstNode = (name: string, from: string) =>
6+
({
7+
type: 'mdxjsEsm',
8+
value: `import ${name} from ${JSON.stringify(from)}`,
9+
data: {
10+
estree: {
11+
type: 'Program',
12+
sourceType: 'module',
13+
body: [
14+
{
15+
type: 'ImportDeclaration',
16+
specifiers: [
17+
{
18+
type: 'ImportDefaultSpecifier',
19+
local: { type: 'Identifier', name },
20+
},
21+
],
22+
source: {
23+
type: 'Literal',
24+
value: from,
25+
raw: `${JSON.stringify(from)}`,
26+
},
27+
},
28+
],
29+
},
30+
},
31+
}) as MdxjsEsm;
32+
33+
export const getNamedImportAstNode = (
34+
name: string,
35+
alias: string,
36+
from: string,
37+
) =>
38+
({
39+
type: 'mdxjsEsm',
40+
value: `import { ${name} as ${alias} } from ${JSON.stringify(from)}`,
41+
data: {
42+
estree: {
43+
type: 'Program',
44+
sourceType: 'module',
45+
body: [
46+
{
47+
type: 'ImportDeclaration',
48+
specifiers: [
49+
{
50+
type: 'ImportSpecifier',
51+
imported: { type: 'Identifier', name },
52+
local: { type: 'Identifier', name: alias },
53+
},
54+
],
55+
source: {
56+
type: 'Literal',
57+
value: from,
58+
raw: `${JSON.stringify(from)}`,
59+
},
60+
},
61+
],
62+
},
63+
},
64+
}) as MdxjsEsm;

packages/core/src/node/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export * from './detectReactVersion';
44
export * from './escapeHeadingIds';
55
export * from './flattenMdxContent';
66
export * from './fs';
7-
export * from './getASTNodeImport';
7+
export * from './getImportAstNode';
88
export * from './getPageKey';
99
export * from './normalizePath';

packages/theme-default/src/components/DocContent/docComponents/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Callout } from '@theme';
21
import { A } from './a';
32
import { Code } from './codeblock/code';
43
import { PreWithCodeButtonGroup } from './codeblock/pre';
@@ -32,8 +31,5 @@ export function getCustomMDXComponent() {
3231
code: Code,
3332
pre: PreWithCodeButtonGroup,
3433
img: Img,
35-
36-
// custom components can be added here
37-
Callout,
3834
};
3935
}

0 commit comments

Comments
 (0)