Skip to content

Commit

Permalink
feat: mdx|md transclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 23, 2020
1 parent 11e0386 commit bf3a68b
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 65 deletions.
40 changes: 21 additions & 19 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,28 @@ export const extractStoreComponent = async (
) => {
if (store.doc) {
const doc: StoriesDoc = store.doc;
const componentNames = Object.keys(doc.components);
if (componentNames) {
for (const componentName of componentNames) {
const { component, componentPackage } = await extractComponent(
componentName,
filePath,
source,
options,
initialAST,
);
if (component) {
if (componentPackage) {
store.packages[componentPackage.fileHash] = componentPackage;
component.package = componentPackage.fileHash;
}
const componentKey = hashStoreId(
`${component.request ?? filePath}-${componentName}`,
if (doc.components) {
const componentNames = Object.keys(doc.components);
if (componentNames) {
for (const componentName of componentNames) {
const { component, componentPackage } = await extractComponent(
componentName,
filePath,
source,
options,
initialAST,
);
store.components[componentKey] = component;
doc.components[componentName] = componentKey;
if (component) {
if (componentPackage) {
store.packages[componentPackage.fileHash] = componentPackage;
component.package = componentPackage.fileHash;
}
const componentKey = hashStoreId(
`${component.request ?? filePath}-${componentName}`,
);
store.components[componentKey] = component;
doc.components[componentName] = componentKey;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const extractMDXStories = (props: any) => (
ast: File,
_options: Required<InstrumentOptions>,
{ source, filePath }: { source: string; filePath: string },
): ParseStorieReturnType => {
): ParseStorieReturnType | undefined => {
const collectAttributes = (node: any): StoryParameters => {
return node.attributes.reduce((acc: StoryParameters, attribute: any) => {
if (attribute.value.type === 'StringLiteral') {
Expand Down Expand Up @@ -197,8 +197,8 @@ export const extractMDXStories = (props: any) => (
if (store.doc && store.doc.title) {
//@ts-ignore
store.doc.components = components;
return store;
} else {
throw new Error(`MDX documenation pages should have at least a title`);
return undefined;
}
return store;
};
13 changes: 9 additions & 4 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type TraverseFn = (
ast: File,
options: Required<InstrumentOptions>,
more: { source: string; filePath: string },
) => ParseStorieReturnType;
) => ParseStorieReturnType | undefined;

/**
* Instrument a source file, with options
Expand All @@ -53,11 +53,14 @@ const parseSource = async (
originalSource: string,
filePath: string,
options: Required<InstrumentOptions>,
): Promise<ParseStorieReturnType> => {
): Promise<ParseStorieReturnType | undefined> => {
const source = await prettify(code, filePath, options.prettier);
const ast = parser.parse(source, options.parser);

const store = traverseFn(ast, options, { source, filePath });
if (!store) {
return undefined;
}
if (store.doc) {
const doc = store.doc;
if (options.stories.storeSourceFile) {
Expand Down Expand Up @@ -117,7 +120,9 @@ const parseSource = async (
};

export { LoadingDocStore };
export type ParseStoriesReturnType = { transformed: string } & LoadingDocStore;
export type ParseStoriesReturnType = { transformed: string } & Partial<
LoadingDocStore
>;
/**
* Parse and instrument a javascript, typescript or MDX file of stories
* @param source Source of the file to be instrumented
Expand Down Expand Up @@ -178,7 +183,7 @@ export const parseStories = async (
filePath,
mergedOptions,
);
const { stories, doc, components, exports, packages } = store;
const { stories, doc, components, exports, packages } = store || {};
const exportsSource = extractStoryExports(exports);
let transformed = `
Expand Down
17 changes: 13 additions & 4 deletions core/instrument/src/misc/mdx-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ export const extractStoryExports = (exports?: MDXExportTypes): string => {
if (exports.default) {
const expCode = mdxPropertiesExport(exports.default);
defaultExportCode = `
const dmsPageExport = MDXContent;
dmsPageExport.MDXPage = MDXContent;
export default { ${
expCode ? `...${expCode},` : ''
} MDXPage: MDXContent };`;
export default ${
expCode
? `Object.assign(dmsPageExport, ${expCode});`
: 'dmsPageExport;'
}
`;
}

let storiesExports: string[] = [];
Expand All @@ -45,5 +50,9 @@ export const extractStoryExports = (exports?: MDXExportTypes): string => {
return `${defaultExportCode}\n${storiesExports.join('\n')}`;
}
}
return 'export default { MDXPage: () => <MDXContent /> };';
return `
const dmsPageExport = MDXContent;
dmsPageExport.MDXPage = MDXContent;
export default dmsPageExport;
`;
};
2 changes: 1 addition & 1 deletion core/instrument/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { mdx } from '@mdx-js/react';
`;

export const defaultMDXOptions: MDXOptions = {
test: /\.mdx$/,
test: /\.(mdx|md)$/i,
renderer: DEFAULT_MDX_RENDERER,
};

Expand Down
20 changes: 11 additions & 9 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ module.exports.pitch = async function() {
if (store) {
if (store.doc) {
console.log(chalk.bgRgb(244, 147, 66)('@loaded: '), filePath);
addStoriesDoc(filePath, {
stories: store.stories,
components: store.components,
packages: store.packages,
doc: {
...store.doc,
fileName: filePath,
},
});
if (store.stories && store.components && store.packages) {
addStoriesDoc(filePath, {
stories: store.stories,
components: store.components,
packages: store.packages,
doc: {
...store.doc,
fileName: filePath,
},
});
}
}
}
return transformed;
Expand Down
24 changes: 7 additions & 17 deletions core/webpack-configs/src/instrument/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const instrument: Configuration = {
],
},
{
test: /\.(stories|story).mdx$/,
test: /\.(md|mdx)$/,
exclude: [/node_modules/],
use: [
{
loader: 'babel-loader',
Expand All @@ -26,32 +27,21 @@ export const instrument: Configuration = {
],
},
},
],
},
{
test: /\.mdx$/,
exclude: /\.(stories|story).mdx$/,
use: [
{
loader: 'babel-loader',
loader: '@component-controls/loader/loader',
options: {
presets: [
[require.resolve('@babel/preset-env'), { modules: 'commonjs' }],
],
mdx: {
transformMDX: true,
},
},
},
],
},
{
test: /\.(story|stories).(js|jsx|ts|tsx|mdx)$/,
test: /\.(story|stories).(js|jsx|ts|tsx)$/,
loader: '@component-controls/loader/loader',
exclude: [/node_modules/],
enforce: 'pre',
options: {
mdx: {
transformMDX: true,
},
},
},
],
},
Expand Down
13 changes: 6 additions & 7 deletions core/webpack-configs/src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export const react: Configuration = {
],
},
{
test: /\.mdx$/,
test: /\.txt$/i,
use: require.resolve('raw-loader'),
},
{
test: /\.(md|mdx)$/i,
exclude: [/node_modules/],
use: [
{
Expand All @@ -64,14 +68,9 @@ export const react: Configuration = {
],
},
{
test: /\.(story|stories).(js|jsx|ts|tsx)$/,
test: /\.(story|stories|doc|docs).(js|jsx|ts|tsx)$/,
loader: '@component-controls/loader/loader',
exclude: [/node_modules/],
options: {
mdx: {
transformMDX: true,
},
},
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions examples/stories/src/sections/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test for uppercase .MD markdown file

2 changes: 2 additions & 0 deletions examples/stories/src/sections/transclusion.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Transclusion sample section

11 changes: 11 additions & 0 deletions examples/stories/src/tutorial/write-mdx-stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Introduction/Writing mdx Stories
type: tutorial
---

import Transclusion from '../sections/transclusion.mdx';
import Readme from '../sections/README.MD';

<Transclusion />

<Readme />
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,7 @@
mkdirp "^0.5.1"
rimraf "^2.5.2"

"@mdx-js/loader@^1.5.1", "@mdx-js/loader@^1.5.5":
"@mdx-js/loader@^1.5.1", "@mdx-js/loader@^1.5.5", "@mdx-js/loader@^1.6.6":
version "1.6.6"
resolved "https://registry.yarnpkg.com/@mdx-js/loader/-/loader-1.6.6.tgz#de7438f3b18b097d66380f8698123a91c587a2f9"
integrity sha512-9jTmXGf5DsVM3XFEX6eYuB8DzIRg2Qo13DHMS2uOBYjrU3w1AUa06Tzz/CnDYQcPSPxHCuRy+m0tMXmG2rQ4Xg==
Expand Down

0 comments on commit bf3a68b

Please sign in to comment.