src/processor.js
function processBlocks (blocks, contentState, options = {}) {
  // Track block context
  let context = context || []
  let currentContext = context
  let lastBlock = null
  let lastProcessed = null
  let parents = []

  // Procedurally process individual blocks
  blocks.forEach(processBlock)

  /**
   * Process an individual block
   * @param  {ContentBlock} block An individual ContentBlock instance
   * @return {Array} A abstract syntax tree node representing a block and its
   * children
   */
  function processBlock (block) {
    const type = block.getType()
    const key = block.getKey()
    const data = (block.getData) ? block.getData().toJS() : {}

    const output = [
      'block',
      [
        type,
        key,
        processBlockContent(block, contentState, options),
        data,
      ],
    ]

    // Push into context (or not) based on depth. This means either the top-level
    // context array, or the `children` of a previous block
    // This block is deeper
    if (lastBlock && block.getDepth() > lastBlock.getDepth()) {
      // Extract reference object from flat context
      // parents.push(lastProcessed) // (mutating)
      currentContext = lastProcessed[dataSchema.block.children]
    } else if (lastBlock && block.getDepth() < lastBlock.getDepth() && block.getDepth() > 0) {
      // This block is shallower (but not at the root). We want to find the last
      // block that is one level shallower than this one to append it to
      let parent = parents[block.getDepth() - 1]
      currentContext = parent[dataSchema.block.children]
    } else if (block.getDepth() === 0) {
      // Reset the parent context if we reach the top level
      parents = []
      currentContext = context
    }
    currentContext.push(output)
    lastProcessed = output[1]
    // Store a reference to the last block at any given depth
    parents[block.getDepth()] = lastProcessed
    lastBlock = block
  }

  return context
}
function processBlocks (blocks, contentState, options = {}) {
  // Track block context
  let context = context || []
  let currentContext = context
  let lastBlock = null
  let lastProcessed = null
  let parents = []

  // Procedurally process individual blocks
  blocks.forEach(processBlock)

  /**
   * Process an individual block
   * @param  {ContentBlock} block An individual ContentBlock instance
   * @return {Array} A abstract syntax tree node representing a block and its
   * children
   */
  function processBlock (block) {
    const type = block.getType()
    const key = block.getKey()
    const data = (block.getData) ? block.getData().toJS() : {}

    const output = [
      'block',
      [
        type,
        key,
        processBlockContent(block, contentState, options),
        data,
      ],
    ]

    // Push into context (or not) based on depth. This means either the top-level
    // context array, or the `children` of a previous block
    // This block is deeper
    if (lastBlock && block.getDepth() > lastBlock.getDepth()) {
      // Extract reference object from flat context
      // parents.push(lastProcessed) // (mutating)
      currentContext = lastProcessed[dataSchema.block.children]
    } else if (lastBlock && block.getDepth() < lastBlock.getDepth() && block.getDepth() > 0) {
      // This block is shallower (but not at the root). We want to find the last
      // block that is one level shallower than this one to append it to
      let parent = parents[block.getDepth() - 1]
      currentContext = parent[dataSchema.block.children]
    } else if (block.getDepth() === 0) {
      // Reset the parent context if we reach the top level
      parents = []
      currentContext = context
    }
    currentContext.push(output)
    lastProcessed = output[1]
    // Store a reference to the last block at any given depth
    parents[block.getDepth()] = lastProcessed
    lastBlock = block
  }

  return context
}
function processBlocks ( blocks , contentState , options = { } ) { // Track block context let context = context || [ ] let currentContext = context let lastBlock = null let lastProcessed = null let parents = [ ] // Procedurally process individual blocks blocks . forEach ( processBlock ) /**
   * Process an individual block
   * @param  {ContentBlock} block An individual ContentBlock instance
   * @return {Array} A abstract syntax tree node representing a block and its
   * children
   */ function processBlock ( block ) { const type = block . getType ( ) const key = block . getKey ( ) const data = ( block . getData ) ? block . getData ( ) . toJS ( ) : { } const output = [ 'block' , [ type , key , processBlockContent ( block , contentState , options ) , data , ] , ] // Push into context (or not) based on depth. This means either the top-level // context array, or the `children` of a previous block // This block is deeper if ( lastBlock && block . getDepth ( ) > lastBlock . getDepth ( ) ) { // Extract reference object from flat context // parents.push(lastProcessed) // (mutating) currentContext = lastProcessed [ dataSchema . block . children ] } else if ( lastBlock && block . getDepth ( ) < lastBlock . getDepth ( ) && block . getDepth ( ) > 0 ) { // This block is shallower (but not at the root). We want to find the last // block that is one level shallower than this one to append it to let parent = parents [ block . getDepth ( ) - 1 ] currentContext = parent [ dataSchema . block . children ] } else if ( block . getDepth ( ) === 0 ) { // Reset the parent context if we reach the top level parents = [ ] currentContext = context } currentContext . push ( output ) lastProcessed = output [ 1 ] // Store a reference to the last block at any given depth parents [ block . getDepth ( ) ] = lastProcessed lastBlock = block } return context }
Convert the content from a series of draft-js blocks into an abstract
syntax tree
@param  {Array} blocks
@param  {ContentState} contentState The draft-js ContentState object
containing the blocks
@param  {Object} options
@return {Array} An abstract syntax tree representing a draft-js content state
Convert the content from a series of draft - js blocks into an abstract syntax tree