<commit_before>import Revisions from './collection'
import { draftToHTML, htmlToDraft } from '../../editor/utils';
import { convertFromRaw, convertToRaw } from 'draft-js';
import { htmlToMarkdown, markdownToHtml } from '../../../server/editor/make_editable_callbacks'
import { highlightFromHTML } from '../../editor/ellipsize';
import { JSDOM } from 'jsdom'
import htmlToText from 'html-to-text'

const PLAINTEXT_DESCRIPTION_LENGTH = 500



function domBuilder(html) {
  const jsdom = new JSDOM(html)
  const document = jsdom.window.document;
  const bodyEl = document.body; // implicitly created
  return bodyEl
}


export function dataToMarkdown(data, type) {
  if (!data) return ""
  switch (type) {
    case "markdown": {
      return data
    }
    case "html": {
      return htmlToMarkdown(data)
    }
    case "draftJS": {
      const contentState = convertFromRaw(data);
      const html = draftToHTML(contentState)
      return htmlToMarkdown(html)
    }
  }
}

export function dataToDraftJS(data, type) {
  switch (type) {
    case "draftJS": {
      return data
    }
    case "html": {
      // We have to add this type definition to the global object to allow draft-convert to properly work on the server
      global.HTMLElement = new JSDOM().window.HTMLElement
      // And alas, it looks like we have to add this global. This seems quite bad, and I am not fully sure what to do about it.
      global.document = new JSDOM().window.document
      const draftJSContentState = htmlToDraft(data, {}, domBuilder)
      // We do however at least remove it right afterwards
      delete global.document
      delete global.HTMLElement
      return convertToRaw(draftJSContentState)  // On the server have to parse in a JS-DOM implementation to make htmlToDraft work
    }
    case "markdown": {
      const html = markdownToHtml(data)
      const draftJSContentState = htmlToDraft(html, {}, domBuilder) // On the server have to parse in a JS-DOM implementation to make htmlToDraft work<commit_msg>Defensive programming around draftJS stuff<commit_after>import Revisions from './collection'
import { draftToHTML, htmlToDraft } from '../../editor/utils';
import { convertFromRaw, convertToRaw } from 'draft-js';
import { htmlToMarkdown, markdownToHtml } from '../../../server/editor/make_editable_callbacks'
import { highlightFromHTML } from '../../editor/ellipsize';
import { JSDOM } from 'jsdom'
import htmlToText from 'html-to-text'

const PLAINTEXT_DESCRIPTION_LENGTH = 500



function domBuilder(html) {
  const jsdom = new JSDOM(html)
  const document = jsdom.window.document;
  const bodyEl = document.body; // implicitly created
  return bodyEl
}


export function dataToMarkdown(data, type) {
  if (!data) return ""
  switch (type) {
    case "markdown": {
      return data
    }
    case "html": {
      return htmlToMarkdown(data)
    }
    case "draftJS": {
      if (data) {
        try {
          const contentState = convertFromRaw(data);
          const html = draftToHTML(contentState)
          return htmlToMarkdown(html)  
        } catch(e) {
          // eslint-disable-next-line no-console
          console.error(e)
        }
        
      } 
      return ""
    }
  }
}

export function dataToDraftJS(data, type) {
  switch (type) {
    case "draftJS": {
      return data
    }
    case "html": {
      // We have to add this type definition to the global object to allow draft-convert to properly work on the server
      global.HTMLElement = new JSDOM().window.HTMLElement
      // And alas, it looks like we have to add this global. This seems quite bad, and I am not fully sure what to do about it.
      global.document = new JSDOM().window.document
      const draftJSContentState = htmlToDraft(data, {}, domBuilder)
      // We do however at least remove it right afterwards
      delete global.document
      delete global.HTMLElement
      return convertToRaw(draftJSContentState)  // On the server have to parse in a JS-DOM implementation to make htmlToDraft work
    }
    case "markdown": {
      const html = markdownToHtml(data)
      const draftJSContentState = htmlToDraft(html, {}, domBuilder) // On the server have to parse in a JS-DOM implementation to make htmlToDraft work
{'fraction_non_alphanumeric': 0.09527186761229314, 'fraction_numerical': 0.0014184397163120568, 'mean_word_length': 2.991509433962264, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6269083', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1216, 'n_words': 405}