{"id":13758291,"url":"https://github.com/jpuri/draftjs-to-html","last_synced_at":"2025-05-15T10:07:40.192Z","repository":{"id":9568953,"uuid":"62619906","full_name":"jpuri/draftjs-to-html","owner":"jpuri","description":"Library for converting Draftjs editor content state to HTML","archived":false,"fork":false,"pushed_at":"2024-02-09T13:33:58.000Z","size":3949,"stargazers_count":324,"open_issues_count":63,"forks_count":144,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-04T21:36:07.422Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jpuri.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-07-05T08:37:13.000Z","updated_at":"2025-02-23T11:37:40.000Z","dependencies_parsed_at":"2024-04-09T19:58:16.287Z","dependency_job_id":"3de6c86e-20be-4d61-b176-5d7278e274a5","html_url":"https://github.com/jpuri/draftjs-to-html","commit_stats":{"total_commits":96,"total_committers":18,"mean_commits":5.333333333333333,"dds":0.21875,"last_synced_commit":"c02452e3ce6719f84f38d39f8f9b0a9e32bf871f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpuri%2Fdraftjs-to-html","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpuri%2Fdraftjs-to-html/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpuri%2Fdraftjs-to-html/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpuri%2Fdraftjs-to-html/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpuri","download_url":"https://codeload.github.com/jpuri/draftjs-to-html/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319720,"owners_count":22051073,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T13:00:24.768Z","updated_at":"2025-05-15T10:07:35.181Z","avatar_url":"https://github.com/jpuri.png","language":"JavaScript","readme":"# DraftJS TO HTML\n\nA library for converting DraftJS Editor content to plain HTML.\n\nThis is draft to HTML library I wrote for one of my projects. I am open-sourcing it so that others can also be benefitted from my work.\n\n## Installation\n\n`npm install draftjs-to-html`\n\n## Usage\n\n```js\nimport { convertToRaw } from 'draft-js';\nimport draftToHtml from 'draftjs-to-html';\n\nconst rawContentState = convertToRaw(editorState.getCurrentContent());\n\nconst markup = draftToHtml(\n  rawContentState, \n  hashtagConfig, \n  directional, \n  customEntityTransform\n);\n```\nThe function parameters are:\n\n1. **contentState**: Its instance of  [RawDraftContentState](https://facebook.github.io/draft-js/docs/api-reference-data-conversion.html#content)\n\n2. **hashConfig**: Its configuration object for hashtag, its required only if hashtags are used. If the object is not defined hashtags will be output as simple text in the markdown.\n    ```js\n    hashConfig = {\n      trigger: '#',\n      separator: ' ',\n    }\n    ```\n    Here trigger is character that marks starting of hashtag (default '#') and separator is character that separates characters (default ' '). These fields in hastag object are optional.\n\n3. **directional**: Boolean, if directional is true text is aligned according to bidi algorithm. This is also optional.\n\n4. **customEntityTransform**: Its function to render custom defined entities by user, its also optional.\n\n   **editorState** is instance of DraftJS               [EditorState](https://draftjs.org/docs/api-reference-editor-state.html#content).\n\n## Supported conversions\nFollowing is the list of conversions it supports:\n\n1. Convert block types to corresponding HTML tags:\n\n    || Block Type | HTML Tag |\n    | -------- | -------- | -------- |\n    | 1 | header-one | h1 |\n    | 2 | header-two | h2 |\n    | 3 | header-three | h3 |\n    | 4 | header-four | h4 |\n    | 5 | header-five | h5 |\n    | 6 | header-six | h6 |\n    | 7 | unordered-list-item | ul |\n    | 8 | ordered-list-item | ol |\n    | 9 | blockquote | blockquote |\n    | 10 | code | pre |\n    | 11 | unstyled | p |\n\n    It performs these additional changes to text of blocks:\n    - replace blank space in beginning and end of block with `\u0026nbsp;`\n    - replace `\\n` with `\u003cbr\u003e`\n    - replace `\u003c` with `\u0026lt;`\n    - replace `\u003e` with `\u0026gt;`\n\n\n2. Converts ordered and unordered list blocks with depths to nested structure of `\u003cul\u003e, \u003col\u003e` and `\u003cli\u003e`.\n\n3. Converts inline styles BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, CODE, SUPERSCRIPT, SUBSCRIPT to corresponding HTML tags: `\u003cstrong\u003e, \u003cem\u003e, \u003cins\u003e, \u003ccode\u003e, \u003csup\u003e, \u003csub\u003e`.\n\n4. Converts inline styles color, background-color, font-size, font-family to a span tag with inline style details:\n`\u003cspan style=\"color:xyz;font-size:xx\"\u003e`. (The inline styles in JSON object should start with strings `color` or `font-size` like `color-red`, `color-green` or `fontsize-12`, `fontsize-20`).\n\n5. Converts entity range of type link to anchor tag using entity data url for href, targetOption for target: `\u003ca href=\"url\" target=\"_self\"\u003etext\u003c/a\u003e`. Default target is `_self`.\n\n6. Converts entity range of type mention to anchor tag using entity data url for href and value for data-value, it also adds class to it: `\u003ca href=\"url\" class=\"wysiwyg-mention\" data-mention data-value=\"value\"\u003etext\u003c/a\u003e`.\n\n7. Converts atomic entity image to image tag using entity data src for image source, and if present alt, alignment, height, width also: `\u003cimg src=\"src\" alt=\"alt_text\" style=\"float: left, height: 50px; width: 50px\"/\u003e`.\n\n8. Converts embedded links to iFrames, using width, height and src from entity data. `\u003ciframe width=\"width\" height=\"height\" src=\"src\" frameBorder=\"0\"\u003e\u003c/iframe\u003e`\n\n9. Converts hashtags to anchor tag: `\u003ca href=\"#tag\" class=\"wysiwyg-hashtag\"\u003e#tag\u003c/a\u003e`.\n\n9. `customEntityTransform` can be used for transformation of a custom entity block to html. If present its call to generate html for entity. It can take 2 parameter:\n   1. `entity` ( object with { type, mutalibity, data})\n   2. `text` text present in the block.\n\n10. Adding style property to block tag for block level styles like text-align: `\u003cp style=\"text-align: right\"\u003etext\u003c/p\u003e`.\n\n11. RTL, if directional function parameter is true, generated blocks have property `dir = \"auto\"` thus they get aligned according to bidi algorithm.\n\n## License\nMIT.\n","funding_links":[],"categories":["Common Utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpuri%2Fdraftjs-to-html","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpuri%2Fdraftjs-to-html","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpuri%2Fdraftjs-to-html/lists"}