<commit_before>import Format from "schema/v2/input_fields/format"
import { GraphQLString, GraphQLFieldConfig } from "graphql"
import { isExisty } from "lib/helpers"
import marked from "marked"

export function formatMarkdownValue(value, format) {
  if (format === "html" || format === "markdown") {
    const renderer = new marked.Renderer()
    marked.setOptions({
      renderer,
      gfm: true,
      tables: true,
      breaks: true,
      pedantic: false,
      sanitize: false,
      smartypants: false,
    })
    return marked(value)
  }

  return value
}

export function markdown(fn?: any): GraphQLFieldConfig<any, any> {
  return {
    type: GraphQLString,
    args: {
      format: Format,
    },
    resolve: (obj, { format }, _, { fieldName }) => {
      const value = fn ? fn(obj) : obj[fieldName]

      if (!isExisty(value)) return null
      return formatMarkdownValue(value, format)
    },
  }
}<commit_msg>Adds types to markdown field<commit_after>import Format from "schema/v2/input_fields/format"
import { GraphQLString, GraphQLFieldConfig } from "graphql"
import { isExisty } from "lib/helpers"
import marked from "marked"
import { ResolverContext } from "types/graphql"

type Format = "html" | "markdown"
type Value = string | null | undefined

export const formatMarkdownValue = (value: string, format: Format): string => {
  if (format === "html" || format === "markdown") {
    const renderer = new marked.Renderer()

    marked.setOptions({
      renderer,
      gfm: true,
      breaks: true,
      pedantic: false,
      sanitize: false,
      smartypants: false,
      tables: true,
    })

    return marked(value)
  }

  return value
}

export const markdown = <T>(
  fn?: (response: T) => Value
): GraphQLFieldConfig<T, ResolverContext> => {
  return {
    type: GraphQLString,
    args: {
      format: Format,
    },
    resolve: (obj, { format }, _, { fieldName }) => {
      const value: Value = fn ? fn(obj) : obj[fieldName]

      if (!isExisty(value) || typeof value !== "string") return null

      return formatMarkdownValue(value, format)
    },
  }
}
{'fraction_non_alphanumeric': 0.12786259541984732, 'fraction_numerical': 0.0009541984732824427, 'mean_word_length': 3.144268774703557, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, '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': '2209656', 'n_tokens_mistral': 661, 'n_tokens_neox': 649, 'n_words': 181}