Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce markdown renderer #480

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# rich-text

Monorepo with Typescript libraries for handling and rendering Contentful Rich
Text documents.
Monorepo with Typescript libraries for handling and rendering Contentful Rich Text documents.

## Packages

Expand All @@ -21,6 +20,8 @@ Text documents.
- Parses a Contentful Rich Text document to HTML in Gatsby
- [`rich-text-react-renderer`](https://github.com/contentful/rich-text/tree/master/packages/rich-text-react-renderer)
- Parses a Contentful Rich Text document to React components
- [`rich-text-markdown-renderer`](https://github.com/contentful/rich-text/tree/master/packages/rich-text-markdown-renderer)
- Parses a Contentful Rich Text document to Markdown

### Community made

Expand Down
4 changes: 4 additions & 0 deletions packages/rich-text-markdown-renderer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
69 changes: 69 additions & 0 deletions packages/rich-text-markdown-renderer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# rich-text-markdown-renderer

A library to convert Contentful Rich Text Document to Markdown in [Github Markdown Format (gmf)](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax).

## Installation

Using [npm](http://npmjs.org/):

```sh
npm install @contentful/rich-text-markdown-renderer
```

Using [yarn](https://yarnpkg.com/):

```sh
yarn add @contentful/rich-text-markdown-renderer
```

## Features

- Support for ordered lists 🔢
- Extend default behavior with custom renderers 🔧

## Usage

### Basic

```javascript
import { documentToMarkdown } from '@contentful/rich-text-markdown-renderer';

const document = {
nodeType: 'document',
data: {},
content: [
{
nodeType: 'paragraph',
data: {},
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ type: 'bold' }],
data: {},
},
{
nodeType: 'text',
value: ' world!',
marks: [{ type: 'italic' }],
data: {},
},
],
},
],
};

documentToMarkdown(document)); // -> Hello world!
```

### Advanced

```javascript
documentToMarkdown(document, {
renderNode: {
[BLOCKS.EMBEDDED_ASSET]: (node) => {
return `![${node.data.target.fields.title}](${node.data.target.fields.file.url})\n`;
},
},
});
```
3 changes: 3 additions & 0 deletions packages/rich-text-markdown-renderer/bin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../../bin/tsconfig.json"
}
3 changes: 3 additions & 0 deletions packages/rich-text-markdown-renderer/bin/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../../bin/tslint.json"
}
7 changes: 7 additions & 0 deletions packages/rich-text-markdown-renderer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getBaseConfig = require('../../baseJestConfig');
const package = require('./package.json');
const packageName = package.name.split('@contentful/')[1];

module.exports = {
...getBaseConfig(packageName),
};
46 changes: 46 additions & 0 deletions packages/rich-text-markdown-renderer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@contentful/rich-text-markdown-renderer",
"version": "16.0.5",
"main": "dist/rich-text-markdown-renderer.es5.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/contentful/rich-text.git"
},
"license": "MIT",
"engines": {
"node": ">=6.0.0"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc --module commonjs && rollup -c rollup.config.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for now but we should really migrate all the builds in this repo to SWC

"prebuild": "rimraf dist",
"start": "tsc && rollup -c rollup.config.js -w",
"lint": "tslint -t codeFrame '@(src|bin)/*.ts'",
"test": "jest"
},
"dependencies": {
"@contentful/rich-text-html-renderer": "^16.1.0",
"@contentful/rich-text-types": "^16.2.0",
"he": "^1.2.0"
},
"devDependencies": {
"@types/he": "^1.2.0",
"jest": "^27.1.0",
"rimraf": "^2.6.3",
"rollup": "^1.32.1",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^4.2.4",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^27.0.5",
"tslint": "^6.1.3",
"typescript": "^4.4.2"
}
}
4 changes: 4 additions & 0 deletions packages/rich-text-markdown-renderer/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import config from '../../rollup.config';
import { main as outputFile } from './package.json';

export default config(outputFile);
Loading