<commit_before>_ = require 'underscore'

MATH_MARKER_BLOCK  = '\u200c\u200c\u200c' # zero-width non-joiner
MATH_MARKER_INLINE = '\u200b\u200b\u200b' # zero-width space

MATH_RENDERED_CLASS = 'math-rendered'
MATH_DATA_SELECTOR = "[data-math]:not(.#{MATH_RENDERED_CLASS})"
MATH_ML_SELECTOR   = "math:not(.#{MATH_RENDERED_CLASS})"
COMBINED_MATH_SELECTOR = "#{MATH_DATA_SELECTOR}, #{MATH_ML_SELECTOR}"

# Search document for math and [data-math] elements and then typeset them
typesetDocument = (windowImpl) ->
  latexNodes = []
  console.log 'typesetting', windowImpl.document.querySelectorAll(MATH_DATA_SELECTOR)
  for node in windowImpl.document.querySelectorAll(MATH_DATA_SELECTOR)
    formula = node.getAttribute('data-math')
    # divs should be rendered as a block, others inline
    if node.tagName.toLowerCase() is 'div'
      node.textContent = "#{MATH_MARKER_BLOCK}#{formula}#{MATH_MARKER_BLOCK}"
    else
      node.textContent = "#{MATH_MARKER_INLINE}#{formula}#{MATH_MARKER_INLINE}"
    latexNodes.push(node)

  unless _.isEmpty(latexNodes)
    windowImpl.MathJax.Hub.Typeset(latexNodes)

  mathMLNodes = _.toArray(windowImpl.document.querySelectorAll(MATH_ML_SELECTOR))
  console.log "MATML", mathMLNodes, windowImpl.document.innerHTML
  unless _.isEmpty(mathMLNodes)
    # style the entire document because mathjax is unable to style individual math elements
    windowImpl.MathJax.Hub.Typeset( windowImpl.document )

  windowImpl.MathJax.Hub.Queue ->
    # Queue a call to mark the found nodes as rendered so are ignored if typesetting is called repeatedly
    # uses className += instead of classList because IE
    for node in latexNodes.concat(mathMLNodes)
      node.className += " #{MATH_RENDERED_CLASS}"

# Install a debounce around typesetting function so that it will only run once
# every Xms even if called multiple times in that period
typesetDocument = _.debounce( typesetDocument, 100)

# typesetMath is the main exported function.
# It's called by components like HTML after they're rendered
typesetMath = (root, windowImpl = window) ->
  # schedule a Mathjax pass if there is at least one [data-math] or <math> element present
  console.log 'render math', root.innerHTML, root.querySelector('math:not(.hild)')
  debugger

  if windowImpl.MathJax?.Hub?.Queue? and root.querySelector(COMBINED_MATH_SELECTOR)
    typesetDocument(windowImpl)


# The following should be called once and configures MathJax.
# Assumes the script to load MathJax is of the form:
# `...MathJax.js?config=TeX-MML-AM_HTMLorMML-full&amp;delayStartupUntil=configured`
startMathJax = ->
  MATHJAX_CONFIG =
    showProcessingMessages: false
    tex2jax:
      displayMath: [[MATH_MARKER_BLOCK, MATH_MARKER_BLOCK]]
      inlineMath:  [[MATH_MARKER_INLINE, MATH_MARKER_INLINE]]
    styles:
      '#MathJax_Message':    visibility: 'hidden', left: '', right: 0
      '#MathJax_MSIE_Frame': visibility: 'hidden', left: '', right: 0

  configuredCallback = ->
    window.MathJax.Hub.Configured()
<commit_msg>kill mathjax console.log<commit_after>_ = require 'underscore'

MATH_MARKER_BLOCK  = '\u200c\u200c\u200c' # zero-width non-joiner
MATH_MARKER_INLINE = '\u200b\u200b\u200b' # zero-width space

MATH_RENDERED_CLASS = 'math-rendered'
MATH_DATA_SELECTOR = "[data-math]:not(.#{MATH_RENDERED_CLASS})"
MATH_ML_SELECTOR   = "math:not(.#{MATH_RENDERED_CLASS})"
COMBINED_MATH_SELECTOR = "#{MATH_DATA_SELECTOR}, #{MATH_ML_SELECTOR}"

# Search document for math and [data-math] elements and then typeset them
typesetDocument = (windowImpl) ->
  latexNodes = []
  for node in windowImpl.document.querySelectorAll(MATH_DATA_SELECTOR)
    formula = node.getAttribute('data-math')
    # divs should be rendered as a block, others inline
    if node.tagName.toLowerCase() is 'div'
      node.textContent = "#{MATH_MARKER_BLOCK}#{formula}#{MATH_MARKER_BLOCK}"
    else
      node.textContent = "#{MATH_MARKER_INLINE}#{formula}#{MATH_MARKER_INLINE}"
    latexNodes.push(node)

  unless _.isEmpty(latexNodes)
    windowImpl.MathJax.Hub.Typeset(latexNodes)

  mathMLNodes = _.toArray(windowImpl.document.querySelectorAll(MATH_ML_SELECTOR))
  unless _.isEmpty(mathMLNodes)
    # style the entire document because mathjax is unable to style individual math elements
    windowImpl.MathJax.Hub.Typeset( windowImpl.document )

  windowImpl.MathJax.Hub.Queue ->
    # Queue a call to mark the found nodes as rendered so are ignored if typesetting is called repeatedly
    # uses className += instead of classList because IE
    for node in latexNodes.concat(mathMLNodes)
      node.className += " #{MATH_RENDERED_CLASS}"

# Install a debounce around typesetting function so that it will only run once
# every Xms even if called multiple times in that period
typesetDocument = _.debounce( typesetDocument, 100)

# typesetMath is the main exported function.
# It's called by components like HTML after they're rendered
typesetMath = (root, windowImpl = window) ->
  # schedule a Mathjax pass if there is at least one [data-math] or <math> element present

  if windowImpl.MathJax?.Hub?.Queue? and root.querySelector(COMBINED_MATH_SELECTOR)
    typesetDocument(windowImpl)


# The following should be called once and configures MathJax.
# Assumes the script to load MathJax is of the form:
# `...MathJax.js?config=TeX-MML-AM_HTMLorMML-full&amp;delayStartupUntil=configured`
startMathJax = ->
  MATHJAX_CONFIG =
    showProcessingMessages: false
    tex2jax:
      displayMath: [[MATH_MARKER_BLOCK, MATH_MARKER_BLOCK]]
      inlineMath:  [[MATH_MARKER_INLINE, MATH_MARKER_INLINE]]
    styles:
      '#MathJax_Message':    visibility: 'hidden', left: '', right: 0
      '#MathJax_MSIE_Frame': visibility: 'hidden', left: '', right: 0

  configuredCallback = ->
    window.MathJax.Hub.Configured()

{'fraction_non_alphanumeric': 0.10043365134431917, 'fraction_numerical': 0.008326105810928014, 'mean_word_length': 4.806646525679758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '2470776', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1874, 'n_words': 465}