convert markdown to rich text
npm install @contentful/rich-text-from-markdownA library to convert markdown to Contentful Rich Text document format.
Using npm:
``sh`
npm install @contentful/rich-text-from-markdown
Using yarn:
`sh`
yarn add @contentful/rich-text-from-markdown
`js
const { richTextFromMarkdown } = require('@contentful/rich-text-from-markdown');
const document = await richTextFromMarkdown('# Hello World');
`
The library will convert automatically the following markdown nodes:
- paragraphheading
- text
- emphasis
- strong
- delete
- inlineCode
- link
- thematicBreak
- blockquote
- list
- listItem
- table
- tableRow
- tableCell
-
If the markdown content has unsupported nodes like image !image you can add a callback as a second argument
and it will get called everytime an unsupported node is found. The return value of the callback will be the rich text representation
of that node.
#### Example:
`js
const { richTextFromMarkdown } = require('@contentful/rich-text-from-markdown');
// define your own type for unsupported nodes like asset
const document = await richTextFromMarkdown(
"!image",
(node) => ({
nodeType: 'embedded-[entry|asset]-[block|inline]',
content: [],
data: {
target: {
sys: {
type: 'Link',
linkType: 'Entry|Asset',
id: '.........',
},
},
},
}),
);
``