const dox = require('dox')
const fs = require('fs')
const hljs = require('highlight.js')
const {minify} = require('html-minifier')
const CleanCSS = require('clean-css')

const css = fs.readFileSync('./docsSrc/index.css')

const src = fs.readFileSync('./index.js', 'utf-8')
const obj = dox.parseComments(src)

const docEntry = o => {
  const name = o.ctx.name
  const example = o.tags.find(tag => tag.type === 'example').string
  const seeObj = o.tags.find(tag => tag.type === 'see')

  const seeAlso = seeObj
    ? `<p class="card__see">See also ${seeObj.string.split(' ').map(s => `<a class="link" href="#${s}">${s}</a>`).join(' ')}</p>`
    : ''

  return `
    <div class="card" id="${name}">
      <h2 class="card__title">
        <div class="card__title-left">
          <a class="card__title-text" href="#${name}">${name}</a>
          <div class="card__sig">${(o.tags.find(tag => tag.type === 'sig')).string}</div>
        </div>
        <a
          class="card__src link"
          href="https://github.com/benji6/imlazy/blob/master/index.js#L${o.line}"
          rel="noopener"
          target="_blank"
        >src</a>
      </h2>
      ${o.description.full.slice(0, 2)} class="card__description"${o.description.full.slice(2)}
      <pre class="hljs">${hljs.highlight('js', example).value}</pre>
      ${seeAlso}
    </div>
  `
}

const page = children => `
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>imlazy docs</title>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>
    <header class="header">
      <h1 class="header__title">imlazy</h1>
      <a class="link" href="https://github.com/benji6/imlazy">GitHub</a>
    </header>
    <main>
      ${children}
    </main>
  </body>
  </html>
`

fs.writeFileSync('docs/index.html', minify(page(obj.map(docEntry).join('')), {collapseWhitespace: true}))
fs.writeFileSync('docs/index.css', new CleanCSS({}).minify(css).styles)

@@ -159,24 +159,49 @@
 lean-css')%0A%0A
+const buildDir = 'docs'%0A%0A
 const css = 
@@ -235,25 +235,24 @@
 index.css')%0A
-%0A
 const src = 
@@ -1851,16 +1851,70 @@
 tml%3E%0A%60%0A%0A
+if (!fs.existsSync(buildDir)) fs.mkdirSync(buildDir)%0A%0A
 fs.write
@@ -1918,29 +1918,36 @@
 iteFileSync(
-'docs
+%60$%7BbuildDir%7D
 /index.html'
@@ -1945,17 +1945,17 @@
 dex.html
-'
+%60
 , minify
@@ -2035,21 +2035,28 @@
 ileSync(
-'docs
+%60$%7BbuildDir%7D
 /index.c
@@ -2057,17 +2057,17 @@
 ndex.css
-'
+%60
 , new Cl

fix build script