All files index.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 1/1
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x 1x   1x 1x 1x 1x 1x 1x 1x   1x                                         1x 5x       5x   5x 5x   5x 1x 1x 1x     5x 5x        
import * as grayMatter from 'gray-matter'
import * as pify from 'pify'
 
const remark = require('remark')
const slug = require('remark-slug')
const hljs = require('remark-highlight.js')
const html = require('remark-html')
const emoji = require('remark-gemoji-to-emoji')
const autolinkHeadings = require('remark-autolink-headings')
const inlineLinks = require('remark-inline-links')
 
const renderer = remark()
  .use(slug)
  .use(autolinkHeadings, { behaviour: 'wrap' })
  .use(inlineLinks)
  .use(emoji)
  .use([hljs, html], { sanitize: false })
 
/**
 * Interface for standard options.
 *
 * @interface IOptions
 */
export interface IOptions {
  /**
   * Whether or not to try to parse YML frontmatter in
   * the file.
   * @default false
   */
  frontmatter: boolean
}
 
export async function markdowner(markdownString: string, opts?: IOptions): Promise<any> {
  const defaults: IOptions = {
    frontmatter: false
  }
 
  opts = Object.assign(defaults, opts)
 
  let data = {}
  let content = markdownString
 
  if (opts.frontmatter) {
    const parsed = grayMatter(markdownString)
    data = parsed.data
    content = parsed.content
  }
 
  const md = await pify(renderer.process)(content)
  return Promise.resolve<any>(
    Object.assign(data as any, { content: md.contents as any })
  )
}