@stoplight/markdown-viewer
A Stoplight Markdown viewer React component
Features
Installation
Supported in modern browsers.
yarn add @stoplight/markdown-viewer @stoplight/mosaic @stoplight/mosaic-code-viewer react react-dom
Basic Usage
import { DefaultSMDComponents, MarkdownViewer } from '@stoplight/markdown-viewer';
import { JSONSchemaViewer } from '@stoplight/json-schema-viewer';
const markdown = `
### Welcome
Hi there.
```;
<MarkdownViewer
markdown={markdown}
includeToc
parseOptions={{
components: {
// Example of overriding the default code renderer for jsonSchema blocks
code: props => {
if (props.jsonSchema) {
return <JSONSchemaViewer value={JSON.parse(props.children)} />;
}
// Fallback to the default component mapping
const DefaultCodeViewer = DefaultSMDComponents.code;
return <DefaultCodeViewer {...props} />;
},
},
}}
/>;
MarkdownViewerProvider
When overriding components it is often easier to do so once, globally, in the consuming application. To do so just use
the MarkdownViewerProvider
component.
import { MarkdownViewerProvider, MarkdownViewer } from '@stoplight/markdown-viewer';
const markdown = `[my link](http://hello.com)`;
<MarkdownViewerProvider
components={{
a: props => <a {...props} data-test="CUSTOM-1" />,
p: props => <p {...props} data-test="CUSTOM-1" />,
}}
>
// somewhere deeper in your application... the custom a and p components will be used when rendering the markdown
<MarkdownViewer markdown={markdown} />
</MarkdownViewerProvider>;
More examples can be find in the Storybook stories.
Contributing
- Clone repo.
- Create / checkout
feature/{name}
, chore/{name}
, or fix/{name}
branch. - Install deps:
yarn
. - Make your changes.
- Run tests:
yarn test.prod
. - Stage relevant files to git.
- Commit:
yarn commit
. NOTE: Commits that don't follow the
conventional format will be
rejected. yarn commit
creates this format for you, or you can put it together manually and then do a regular
git commit
. - Push:
git push
. - Open PR targeting the
develop
branch.