Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

readme.md 4.2 KiB

3 anni fa
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # DraftJS TO HTML
  2. A library for converting DraftJS Editor content to plain HTML.
  3. This 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.
  4. ## Installation
  5. `npm install draftjs-to-html`
  6. ## Usage
  7. ```
  8. import { convertToRaw } from 'draft-js';
  9. import draftToHtml from 'draftjs-to-html';
  10. const rawContentState = convertToRaw(editorState.getCurrentContent());
  11. const markup = draftToHtml(
  12. rawContentState,
  13. hashtagConfig,
  14. directional,
  15. customEntityTransform
  16. );
  17. ```
  18. The function parameters are:
  19. 1. **contentState**: Its instance of [RawDraftContentState](https://facebook.github.io/draft-js/docs/api-reference-data-conversion.html#content)
  20. 2. **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.
  21. ```
  22. hashConfig = {
  23. trigger: '#',
  24. separator: ' ',
  25. }
  26. ```
  27. 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.
  28. 3. **directional**: Boolean, if directional is true text is aligned according to bidi algorithm. This is also optional.
  29. 4. **customEntityTransform**: Its function to render custom defined entities by user, its also optional.
  30. **editorState** is instance of DraftJS [EditorState](https://draftjs.org/docs/api-reference-editor-state.html#content).
  31. ## Supported conversions
  32. Following is the list of conversions it supports:
  33. 1. Convert block types to corresponding HTML tags:
  34. || Block Type | HTML Tag |
  35. | -------- | -------- | -------- |
  36. | 1 | header-one | h1 |
  37. | 2 | header-two | h2 |
  38. | 3 | header-three | h3 |
  39. | 4 | header-four | h4 |
  40. | 5 | header-five | h5 |
  41. | 6 | header-six | h6 |
  42. | 7 | unordered-list-item | ul |
  43. | 8 | ordered-list-item | ol |
  44. | 9 | blockquote | blockquote |
  45. | 10 | code | pre |
  46. | 11 | unstyled | p |
  47. It performs these additional changes to text of blocks:
  48. - replace blank space in beginning and end of block with ` `
  49. - replace `\n` with `<br>`
  50. - replace `<` with `&lt;`
  51. - replace `>` with `&gt;`
  52. 2. Converts ordered and unordered list blocks with depths to nested structure of `<ul>, <ol>` and `<li>`.
  53. 3. Converts inline styles BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, CODE, SUPERSCRIPT, SUBSCRIPT to corresponding HTML tags: `<strong>, <em>, <ins>, <code>, <sup>, <sub>`.
  54. 4. Converts inline styles color, background-color, font-size, font-family to a span tag with inline style details:
  55. `<span style="color:xyz;font-size:xx">`. (The inline styles in JSON object should start with strings `color` or `font-size` like `color-red`, `color-green` or `fontsize-12`, `fontsize-20`).
  56. 5. Converts entity range of type link to anchor tag using entity data url for href, targetOption for target: `<a href="url" target="_self">text</a>`. Default target is `_self`.
  57. 6. 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: `<a href="url" class="wysiwyg-mention" data-mention data-value="value">text</a>`.
  58. 7. Converts atomic entity image to image tag using entity data src for image source, and if present alt, alignment, height, width also: `<img src="src" alt="alt_text" style="float: left, height: 50px; width: 50px"/>`.
  59. 8. Converts embedded links to iFrames, using width, height and src from entity data. `<iframe width="width" height="height" src="src" frameBorder="0"></iframe>`
  60. 9. Converts hashtags to anchor tag: `<a href="#tag" class="wysiwyg-hashtag">#tag</a>`.
  61. 9. `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:
  62. 1. `entity` ( object with { type, mutalibity, data})
  63. 2. `text` text present in the block.
  64. 10. Adding style property to block tag for block level styles like text-align: `<p style="text-align: right">text</p>`.
  65. 11. RTL, if directional function parameter is true, generated blocks have property `dir = "auto"` thus they get aligned according to bidi algorithm.
  66. ## License
  67. MIT.