client/src/HtmlSerializer.js
author Alexandre Segura <mex.zktk@gmail.com>
Thu, 01 Jun 2017 18:46:34 +0200
changeset 17 877d8796b86d
child 19 f1b125b95fe9
permissions -rw-r--r--
Store serialized HTML in note.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
import React from 'react'
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
import { Html } from 'slate'
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
const BLOCK_TAGS = {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
  blockquote: 'quote',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
  p: 'paragraph',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
  pre: 'code'
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
}
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
// Add a dictionary of mark tags.
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
const MARK_TAGS = {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
  em: 'italic',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
  strong: 'bold',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
  u: 'underline',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
}
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
const rules = [
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
  // Block rules
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
  {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
    deserialize(el, next) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
      const type = BLOCK_TAGS[el.tagName]
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
      if (!type) return
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
      return {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
        kind: 'block',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
        type: type,
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
        nodes: next(el.children)
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
      }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
    },
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
    serialize(object, children) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
      if (object.kind != 'block') return
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
      switch (object.type) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
        case 'code': return <pre><code>{children}</code></pre>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
        case 'paragraph':
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
        case 'line': return <p>{children}</p>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
        case 'quote': return <blockquote>{children}</blockquote>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
      }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
    }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
  },
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
  // Mark rules
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
  {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    deserialize(el, next) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
      const type = MARK_TAGS[el.tagName]
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
      if (!type) return
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
      return {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
        kind: 'mark',
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
        type: type,
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
        nodes: next(el.children)
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    48
      }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    49
    },
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
    serialize(object, children) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
      if (object.kind != 'mark') return
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
      switch (object.type) {
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
        case 'bold': return <strong>{children}</strong>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
        case 'italic': return <em>{children}</em>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
        case 'underline': return <u>{children}</u>
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
      }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
    }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
  }
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
]
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
const serializer = new Html({ rules })
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
877d8796b86d Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
export default serializer