Skip to content

Commit

Permalink
Merge pull request #40 from contentstack/feat_CMA-7199
Browse files Browse the repository at this point in the history
feat: added json to markdown serializer.
  • Loading branch information
Ashwini-Rathod authored Jun 7, 2024
2 parents 4a005ce + 0f1409c commit 64d5136
Show file tree
Hide file tree
Showing 8 changed files with 1,009 additions and 6 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. [Read more](https://www.contentstack.com/docs/).

The JSON RTE Serializer package helps you convert the data inside your JSON Rich Text Editor field from JSON to HTML format and vice versa.
The JSON RTE Serializer package assists in converting the content within your JSON Rich Text Editor field between JSON and HTML formats. This means you can easily change your data from JSON format to HTML format for display purposes, and vice versa, for data storage or processing.
If you need to convert JSON to Markdown format, we offer the Markdown Serializer function. This function is specifically designed to transform your JSON data into Markdown, making it easier to handle text formatting for platforms that use Markdown.

# Installation

Expand Down Expand Up @@ -102,6 +103,45 @@ The resulting JSON-formatted data will look as follows:
}
```

### JSON to Markdown Conversion Code

You can use the following JSON RTE Serializer code to convert your JSON RTE field data into Markdown format.

```javascript
import { jsonToMarkdown } from "@contentstack/json-rte-serializer";

const markdownValue = jsonToHtml({
type: "doc",
attrs: {},
uid: "547a479c68824767ce1d9725852f042b",
children: [
{
uid: "767a479c6882471d9725852f042b67ce",
type: "p",
attrs: {},
children: [
{ text: "This is Markdown-formatted content which has some " },
{ text: "BOLD", bold: true },
{ text: " text and some "},
{ text: "Italic", italic: true },
{ text: " text."}
]
},
],
});

console.log(markdownValue);
```

### Result of Conversion

The resulting Markdown data will look as follows:

```MARKDOWN

This is Markdown-formatted content which has some **BOLD** text and some *Italic* text.
```

## Custom Conversion

For customized conversion scenarios, you can customize your JSON RTE Serializer code to allow the support for additional tags or element types in the JSON Rich Text Editor field. Pass an `options` field (optional) within the `jsonToHtml` or `htmlToJson` method to manipulate the working of the JSON RTE Serializer package as per your requirements.
Expand Down
65 changes: 61 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"dependencies": {
"array-flat-polyfill": "^1.0.1",
"lodash": "^4.17.21",
"lodash.clonedeep": "^4.5.0",
"lodash.flatten": "^4.4.0",
"lodash.isempty": "^4.4.0",
Expand All @@ -57,6 +58,7 @@
"lodash.isplainobject": "^4.0.6",
"lodash.isundefined": "^3.0.1",
"lodash.kebabcase": "^4.1.1",
"slate": "^0.103.0",
"uuid": "^8.3.2"
},
"files": [
Expand Down
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "array-flat-polyfill"
import { fromRedactor } from "./fromRedactor"
import { toRedactor } from "./toRedactor"
import {jsonToMarkdownSerializer} from './jsonToMarkdown'
export * from "./types"
export { fromRedactor as htmlToJson, toRedactor as jsonToHtml }
export { fromRedactor as htmlToJson, toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown }
Loading

0 comments on commit 64d5136

Please sign in to comment.