'use strict';

module.exports = exports = ({
  hashtag     = '/tags/',
  mention     = '/users/',
  tocLevel    = [1,2],
  containers  = [
    'container',
  ]
} = {}) => {
  const md = require('markdown-it')({
      html:        true,
      xhtmlOut:    false,
      breaks:      true,
      langPrefix:  'language-',
      linkify:     true,
      linkTarget:  '',
      typographer: true,
      quotes:      '“”‘’'
    })
    .use(require('markdown-it-abbr'))
    .use(require('markdown-it-anchor'), {
      level: 1,
      slugify: (str, md) => {
        return str.toLowerCase().replace(/\&[a-z0-9A-Z]+\;/g, '').replace(/[^\w]/g, '-');
      },
      permalink: true,
      permalinkClass: 'heading-anchor',
      permalinkSymbol: '',
      permalinkBefore: false,
    })
    .use(require('markdown-it-attrs'))
    .use(require('markdown-it-block-embed'))
    .use(require('markdown-it-block-image'), {
      outputContainer: 'div',
      containerClassName: "block-embed block-embed-image"
    })
    .use(require('markdown-it-center-text'))
    .use(require('markdown-it-checkbox'), {
      divWrap: true,
      divClass: 'checkbox',
      idPrefix: 'checkbox-'
    })
    .use(require('markdown-it-deflist'))
    .use(require('markdown-it-emoji'))
    .use(require('markdown-it-expand-tabs'), {tabWidth: 2})
    .use(require('markdown-it-footnote'))
    .use(require('markdown-it-hashmention'), {
      hashtags: {
        href: hashtag,
      },
      mentions: {
        href: mention,
      },
    })
    .use(require('markdown-it-highlighted'))
    .use(require('markdown-it-highlightjs'), {
      auto: true,
      code: true
    })
    .use(require('markdown-it-imsize'))
    .use(require('markdown-it-ins'))
    .use(require('markdown-it-mark'))
    .use(require('markdown-it-math'))
    .use(require('markdown-it-playground'))
    .use(require('markdown-it-smartarrows'))
    .use(require('markdown-it-sub'))
    .use(require('markdown-it-sup'))
    .use(require('markdown-it-table-of-contents'), {
      includeLevel: tocLevel
    })
    .use(require('markdown-it-task-checkbox'), {
      disabled: false,
      divWrap: true,
      divClass: 'checkbox',
      idPrefix: 'task-checkbox-',
      ulClass: 'task-list',
      liClass: 'task-list-item'
    })
    .use(require('markdown-it-title'), 0)
    .use(require('markdown-it-underline'))
    .use(require('markdown-it-custom-block'), {
      video(url) {
        return `<div class="block-embed block-embed-video">
          <video src="${url}" preload="auto"></video>
        </div>`;
      },
      'video-controls'(url) {
        return `<div class="block-embed block-embed-video">
          <video src="${url}" preload="auto" controls></video>
        </div>`;
      },
      audio(url) {
        return `<div class="block-embed block-embed-audio">
          <audio src="${url}" preload="auto" controls></audio>
        </div>`;
      }
    });

  for (let container of containers) {
    md.use(require('markdown-it-container'), container);
  }

  return md;
};

@@ -2904,16 +2904,1057 @@
       %7D%0A
+    %7D)%0A    .use(require('markdown-it-container'), 'custom', %7B%0A      validate: function(params) %7B%0A        return params.trim().match(/%5Ecustom/);%0A      %7D,%0A      render: function (tokens, idx) %7B%0A        const parts = tokens%5Bidx%5D.info.replace(/custom/, '').trim().split(' ');%0A        const element = parts.shift() %7C%7C 'div';%0A%0A        if (tokens%5Bidx%5D.nesting === 1) %7B%0A          let id;%0A          let classes = %5B%5D;%0A          let attributes = '';%0A%0A          for (let part of parts) %7B%0A            if (/%5E%5C#/.test(part)) %7B%0A              id = part.replace(/%5E%5C#/, '');%0A            %7D%0A            if (/%5E%5C./.test(part)) %7B%0A              classes.push(part.replace(/%5E%5C./, ''));%0A            %7D%0A          %7D%0A%0A          if (id) %7B%0A            attributes += %60 id=%22$%7Bid%7D%22%60;%0A          %7D%0A%0A          if (classes.length) %7B%0A            attributes += %60 class=%22$%7Bclasses.join(' ')%7D%22%60;%0A          %7D%0A%0A          // opening tag%0A          return %60%3C$%7Belement%7D$%7Battributes%7D%3E%5Cn%60;%0A        %7D%0A        else %7B%0A          // closing tag%0A          return %60%3C/$%7Belement%7D%3E%5Cn%60;%0A        %7D%0A      %7D%0A
     %7D);%0A

add custom container