<commit_before>Baby = require('babyparse')
path = require 'path'
fs = require 'fs'

_2DArrayToMarkdownTable = (_2DArr)->
  output = "  \n"
  _2DArr.forEach (arr, offset)->
    i = 0
    output += '|'
    while i < arr.length
      output += (arr[i] + '|')
      i += 1
    output += '  \n'
    if offset == 0
      output += '|'
      i = 0
      while i < arr.length
        output += ('---|')
        i += 1
      output += '  \n'

  output += '  \n'
  console.log(output)
  output

docImports = (inputString, {filesCache, rootDirectoryPath, projectDirectoryPath, useAbsoluteImagePath})->
  inputString = inputString.replace /(^|\s)import(\s+)\"([^\"]+)\"/g, (whole, _g1, _g2, filePath, offset)->

    if filePath.match(/^(http|https|file)\:\/\//)
      absoluteFilePath = filePath
    else if filePath.startsWith('/')
      absoluteFilePath = path.resolve(projectDirectoryPath, '.' + filePath)
    else
      absoluteFilePath = path.resolve(rootDirectoryPath, filePath)

    if filesCache?[absoluteFilePath] # already in cache
      return filesCache[absoluteFilePath]

    extname = path.extname(filePath)
    output = ''
    if extname in ['.jpeg', '.gif', '.png', '.apng', '.svg', '.bmp'] # image
      if filePath.match(/^(http|https|file)\:\/\//)
        output = "![](#{filePath + '?' + Math.random()})"
      else if useAbsoluteImagePath
        output = "![]('/' + #{path.relative(projectDirectoryPath, absoluteFilePath) + '?' + Math.random()})" # TODO: project relative path?
      else
        output = "![](#{path.relative(rootDirectoryPath, absoluteFilePath) + '?' + Math.random()})" # TODO: project relative path?

      filesCache?[absoluteFilePath] = output

    else if extname in ['.md', '.markdown', '.mmark', '.rmd'] # TODO: use config markdown-preview-enhanced.fileExtension
      output = ''
    else if extname in ['.csv']
      try
        csvContent = fs.readFileSync(absoluteFilePath, {encoding: 'utf-8'})
        console.log(csvContent)
        parseResult = Baby.parse(csvContent)
        console.log(parseResult)
        if parseResult.errors.length
          output = "<pre>#{parseResult.errors[0]}</pre>"
        else
          # format csv to markdown table
          output = _2DArrayToMarkdownTable(parseResult.data)
          filesCache?[absoluteFilePath] = output
      catch e
        output = "<pre>#{e.toString()}</pre>"
    else # codeblock
      output = ''

    return output

module.exports = docImports<commit_msg>now support import other type files for code block<commit_after>Baby = require('babyparse')
path = require 'path'
fs = require 'fs'

_2DArrayToMarkdownTable = (_2DArr)->
  output = "  \n"
  _2DArr.forEach (arr, offset)->
    i = 0
    output += '|'
    while i < arr.length
      output += (arr[i] + '|')
      i += 1
    output += '  \n'
    if offset == 0
      output += '|'
      i = 0
      while i < arr.length
        output += ('---|')
        i += 1
      output += '  \n'

  output += '  \n'
  console.log(output)
  output

docImports = (inputString, {filesCache, rootDirectoryPath, projectDirectoryPath, useAbsoluteImagePath})->
  inputString = inputString.replace /(^|\s)import(\s+)\"([^\"]+)\"/g, (whole, _g1, _g2, filePath, offset)->

    if filePath.match(/^(http|https|file)\:\/\//)
      absoluteFilePath = filePath
    else if filePath.startsWith('/')
      absoluteFilePath = path.resolve(projectDirectoryPath, '.' + filePath)
    else
      absoluteFilePath = path.resolve(rootDirectoryPath, filePath)

    if filesCache?[absoluteFilePath] # already in cache
      return filesCache[absoluteFilePath]

    extname = path.extname(filePath)
    output = ''
    if extname in ['.jpeg', '.gif', '.png', '.apng', '.svg', '.bmp'] # image
      if filePath.match(/^(http|https|file)\:\/\//)
        output = "![](#{filePath + '?' + Math.random()})"
      else if useAbsoluteImagePath
        output = "![]('/' + #{path.relative(projectDirectoryPath, absoluteFilePath) + '?' + Math.random()})" # TODO: project relative path?
      else
        output = "![](#{path.relative(rootDirectoryPath, absoluteFilePath) + '?' + Math.random()})" # TODO: project relative path?

      filesCache?[absoluteFilePath] = output

    else if extname in ['.md', '.markdown', '.mmark', '.rmd'] # TODO: use config markdown-preview-enhanced.fileExtension
      output = ''
    else if extname in ['.csv']
      try
        csvContent = fs.readFileSync(absoluteFilePath, {encoding: 'utf-8'})
        console.log(csvContent)
        parseResult = Baby.parse(csvContent)
        console.log(parseResult)
        if parseResult.errors.length
          output = "<pre>#{parseResult.errors[0]}</pre>"
        else
          # format csv to markdown table
          output = _2DArrayToMarkdownTable(parseResult.data)
          filesCache?[absoluteFilePath] = output
      catch e
        output = "<pre>#{e.toString()}</pre>"
    else # codeblock
      try
        fileContent = fs.readFileSync(absoluteFilePath, {encoding: 'utf-8'})
        output = """
```#{extname.slice(1, extname.length)}
#{fileContent}
```
"""
        filesCache?[absoluteFilePath] = output
      catch e
        output = "<pre>#{e.toString()}</pre>"

    return output

module.exports = docImports
{'fraction_non_alphanumeric': 0.1633435582822086, 'fraction_numerical': 0.005368098159509203, 'mean_word_length': 2.7804347826086957, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '7354034', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1605, 'n_words': 365}