defaults.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * apidoc
  3. * https://apidocjs.com
  4. *
  5. * Authors:
  6. * Peter Rottmann <rottmann@inveris.de>
  7. * Nicolas CARPi @ Deltablot
  8. * Copyright (c) 2013 inveris OHG
  9. * Licensed under the MIT license.
  10. */
  11. /**
  12. * Provide default values for the app
  13. */
  14. const path = require('path');
  15. const winston = require('winston');
  16. let Markdown = require('markdown-it');
  17. // Get the default logger
  18. function getLogger (options) {
  19. // default format
  20. let format = winston.format.simple();
  21. if (options.logFormat === 'json') {
  22. // remove colors for json output
  23. options.colorize = false;
  24. format = winston.format.json();
  25. }
  26. // add colors (default is true)
  27. if (options.colorize) {
  28. format = winston.format.combine(
  29. winston.format.colorize(),
  30. format,
  31. );
  32. }
  33. // console logger
  34. return winston.createLogger({
  35. transports: [
  36. new winston.transports.Console({
  37. level: options.debug ? 'debug' : options.verbose ? 'verbose' : 'info',
  38. silent: options.silent,
  39. }),
  40. ],
  41. format: format,
  42. });
  43. }
  44. function getMarkdownParser (options) {
  45. // Markdown Parser: enable / disable / use a custom parser.
  46. let markdownParser;
  47. if (options.markdown === true) {
  48. markdownParser = new Markdown({
  49. breaks: false,
  50. html: true,
  51. linkify: false,
  52. typographer: false,
  53. highlight: function (str, lang) {
  54. if (lang) {
  55. return '<pre><code class="language-' + lang + '">' + str + '</code></pre>';
  56. }
  57. return '<pre><code>' + str + '</code></pre>';
  58. },
  59. });
  60. } else if (options.markdown !== false) {
  61. // Include custom Parser @see https://github.com/apidoc/apidoc/wiki/Custom-markdown-parser and test/markdown/custom_markdown_parser.js
  62. if (options.markdown.substr(0, 2) !== '..' && ((options.markdown.substr(0, 1) !== '/' && options.markdown.substr(1, 2) !== ':/' && options.markdown.substr(1, 2) !== ':\\' && options.markdown.substr(0, 1) !== '~') || options.markdown.substr(0, 1) === '.')) { // eslint-disable-line no-extra-parens
  63. options.markdown = path.join(process.cwd(), options.markdown);
  64. }
  65. Markdown = require(options.markdown); // Overwrite default Markdown.
  66. markdownParser = new Markdown();
  67. }
  68. return markdownParser;
  69. }
  70. module.exports = {
  71. getLogger: getLogger,
  72. getMarkdownParser: getMarkdownParser,
  73. };