ignitionworks / draftjs_exporter

Export Draft.js content state into HTML.
MIT License
16 stars 20 forks source link

Add an ability to pass options to Nokogiri when rendering #7

Closed smaximov closed 7 years ago

smaximov commented 7 years ago

When converting DraftJS content state to HTML using draftjs_exporter, any non-latin letters are rendered as HTML entities. Consider the following example: (I omitted config for brevity):

config = # ...

content = {
  "entityMap": {},
  "blocks": [
    {
      "key": "ckf8d",
      "text": "Russian: Привет, мир!",
      "type": "unordered-list-item",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    },
    {
      "key": "fi809",
      "text": "Japanese: 曖昧さ回避",
      "type": "unordered-list-item",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    }
  ]
}

exporter = DraftjsExporter::HTML.new(config)
exporter.call(content)

This will yield

<ul class=\"unordered-list\">\n<li>Russian: &#1055;&#1088;&#1080;&#1074;&#1077;&#1090;, &#1084;&#1080;&#1088;!</li>\n<li>Japanese: &#26326;&#26151;&#12373;&#22238;&#36991;</li>\n</ul>

Though most web browsers will display this HTML without any problem (I think), this can be inconvenient in many ways, so this PR features an ability to pass additional options to Nokigiri XML renderer (you can view the whole list here) as the second (optional) parameter of DraftjsExporter::HTML.call, so it should not break backward compatibility.

Returning to my example, if I pass encoding: 'UTF-8' when rendering, this will yield the desired result:

exporter.call(content, encoding: 'UTF-8') #=> "<ul class=\"unordered-list\">\n<li>Russian: Привет, мир!</li>\n<li>Japanese: 曖昧さ回避</li>\n</ul>"
theozaurus commented 7 years ago

Merged. Thanks very much. I'll roll a new release shortly