Skip to content

Commit

Permalink
Merge pull request #644 from storybookjs/fix-multi-code-block-snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegach authored Dec 5, 2023
2 parents 42f372c + 5fc6ebe commit d716036
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ const getSnippetTabName = (path: string) => {
}
return prettifyName(name);
};

/**
* 1. ModuleComponent is (due to MDX) a React component.
* 2. That component renders one or more <pre> tags with a <code> tag inside.
* 3. The <code> tag contains the actual code snippet string.
*/
function extractContentFromModule(module) {
function extractContentFromCodeBlock(block) {
return block.props.children.props.children;
}

const containedCodeBlocks = module({ components: {} }).props.children;

return Array.isArray(containedCodeBlocks)
? containedCodeBlocks.map(extractContentFromCodeBlock).join('\n')
: extractContentFromCodeBlock(containedCodeBlocks);
}

export interface SnippetObject {
content: string;
id: string;
Expand Down Expand Up @@ -125,12 +143,7 @@ export const fetchDocsSnippets = async (
}

const [title, content] = parseSnippetContent(
/**
* 1. ModuleComponent is (due to MDX) a React component.
* 2. That component renders a <pre> tag with a <code> tag inside.
* 3. The <code> tag contains the actual code snippet string.
*/
ModuleComponent({ components: {} }).props.children.props.children.props.children,
extractContentFromModule(ModuleComponent),
isTerminal
);

Expand Down

0 comments on commit d716036

Please sign in to comment.