<commit_before>import React from 'react';
import { convertFromHTML, convertToHTML } from 'draft-convert';
import { Utils } from 'meteor/vulcan:core';


export const htmlToDraft = convertFromHTML({
  htmlToEntity: (nodeName, node, createEntity) => {
    if (nodeName === 'a') {
      return createEntity(
        'LINK',
        'MUTABLE',
        {url: node.href}
      )
    }
  },
  htmlToBlock: (nodeName, node) => {
    // if (nodeName === 'blockquote') {
    //   return {
    //     type: 'blockquote',
    //     data: {}
    //   };
    // }
    if (nodeName === 'hr') { // This currently appears to be broken, sadly. TODO: Fix this
      return {
        type: 'divider',
        data: {},
        text: 'as',
        depth: 0,
        inlineStyleRanges: [ { offset: 0, length: 2, style: 'ITALIC' } ],
      }
    }
  }
})

export const draftToHTML = convertToHTML({
  styleToHTML: (style) => {
    if (style === 'STRIKETHROUGH') {
      return <span style={{textDecoration: 'line-through'}} />;
    }
  },
  entityToHTML: (entity, originalText) => {
    if (entity.type === 'image') {
      let classNames = 'draft-image '
      if (entity.data.alignment) {
        classNames = classNames + entity.data.alignment;
      }
      let style = ""
      if (entity.data.width) {
        style = "width:" + entity.data.width + "%";
      }
      return `<figure><img src="${entity.data.src}" class="${classNames}" style="${style}" /></figure>`;
    }
    if (entity.type === 'LINK') {
      return `<a href="${entity.data.url || entity.data.href}">${originalText}</a>`;
    }
    if (entity.type === 'IMG') {
      const className = 'draft-inline-image';
      return `<img src="${entity.data.src}" class="${className}" alt="${entity.data.alt}"/>`
    }
    if (entity.type === 'INLINETEX') {
      if (entity.data.html) {
        return `<span><style>${entity.data.css}</style>${entity.data.html}</span>`
      } else {
        return `<span class="draft-latex-placeholder">refresh to render LaTeX</span>`
      }
    }
    return originalText;
  },
  blockToHTML: (block) => {
     const type = block.type;
     if (type === 'atomic') {
       if (block.data && block.data.mathjax && block.data.html) {
         return `<div><style>${block.data.css}</style>${block.data.html}</div>`
       } else if (block.data && block.data.mathjax) {
         return `<div class="draft-latex-placeholder-block">refresh to render LaTeX</div>`
       } else {
         return {start: '<span>', end: '</span>'};
       }
     }
     if (type === 'blockquote') {
       return <blockquote />
     }
     if (type === 'divider') {
       return <hr className="dividerBlock" />
     }
    //  return <span/>;
   },
});

Utils.draftToHTML = draftToHTML;
Utils.htmlToDraft = htmlToDraft;
<commit_msg>Improved refresh to render LaTeX models<commit_after>import React from 'react';
import { convertFromHTML, convertToHTML } from 'draft-convert';
import { Utils } from 'meteor/vulcan:core';


export const htmlToDraft = convertFromHTML({
  htmlToEntity: (nodeName, node, createEntity) => {
    if (nodeName === 'a') {
      return createEntity(
        'LINK',
        'MUTABLE',
        {url: node.href}
      )
    }
  },
  htmlToBlock: (nodeName, node) => {
    // if (nodeName === 'blockquote') {
    //   return {
    //     type: 'blockquote',
    //     data: {}
    //   };
    // }
    if (nodeName === 'hr') { // This currently appears to be broken, sadly. TODO: Fix this
      return {
        type: 'divider',
        data: {},
        text: 'as',
        depth: 0,
        inlineStyleRanges: [ { offset: 0, length: 2, style: 'ITALIC' } ],
      }
    }
  }
})

export const draftToHTML = convertToHTML({
  styleToHTML: (style) => {
    if (style === 'STRIKETHROUGH') {
      return <span style={{textDecoration: 'line-through'}} />;
    }
  },
  entityToHTML: (entity, originalText) => {
    if (entity.type === 'image') {
      let classNames = 'draft-image '
      if (entity.data.alignment) {
        classNames = classNames + entity.data.alignment;
      }
      let style = ""
      if (entity.data.width) {
        style = "width:" + entity.data.width + "%";
      }
      return `<figure><img src="${entity.data.src}" class="${classNames}" style="${style}" /></figure>`;
    }
    if (entity.type === 'LINK') {
      return `<a href="${entity.data.url || entity.data.href}">${originalText}</a>`;
    }
    if (entity.type === 'IMG') {
      const className = 'draft-inline-image';
      return `<img src="${entity.data.src}" class="${className}" alt="${entity.data.alt}"/>`
    }
    if (entity.type === 'INLINETEX') {
      if (entity.data.html) {
        return `<span><style>${entity.data.css}</style>${entity.data.html}</span>`
      } else {
        return `<span class="draft-latex-placeholder"> &lt; refresh to render LaTeX &gt; </span>`
      }
    }
    return originalText;
  },
  blockToHTML: (block) => {
     const type = block.type;
     if (type === 'atomic') {
       if (block.data && block.data.mathjax && block.data.html) {
         return `<div><style>${block.data.css}</style>${block.data.html}</div>`
       } else if (block.data && block.data.mathjax) {
         return `<div class="draft-latex-placeholder-block"> &lt;refresh to render LaTeX&gt; </div>`
       } else {
         return {start: '<span>', end: '</span>'};
       }
     }
     if (type === 'blockquote') {
       return <blockquote />
     }
     if (type === 'divider') {
       return <hr className="dividerBlock" />
     }
    //  return <span/>;
   },
});

Utils.draftToHTML = draftToHTML;
Utils.htmlToDraft = htmlToDraft;

{'fraction_non_alphanumeric': 0.1872552509789961, 'fraction_numerical': 0.001067995728017088, 'mean_word_length': 2.401331719128329, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 61, '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': '4497527', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1825, 'n_words': 389}