All files / src/commands HtmlCommand.ts

88.24% Statements 30/34
78.13% Branches 25/32
50% Functions 1/2
90.91% Lines 30/33

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 9224x       24x 24x 24x                                                                           24x   4x     4x   4x     4x       4x 4x   4x 4x 2x     4x 4x 4x 4x 4x 4x   4x 4x   4x 2x     4x   4x 2x   4x 2x     4x    
import { argdown, IFileRequest, ISaveAsRequest, ILogParserErrorsRequest } from "@argdown/node";
import { Arguments } from "yargs";
import { IHtmlRequest } from "@argdown/core";
 
export const command = "html [inputGlob] [outputDir]";
export const desc = "export Argdown input as HTML files";
export const builder = {
  logParserErrors: {
    alias: "e",
    describe: "Log parser errors to console",
    type: "boolean",
    default: true
  },
  headless: {
    alias: "hl",
    describe: "Export without Html, Head and Body elements",
    type: "boolean"
  },
  head: {
    alias: "h",
    describe: "Allows you to prepend a custom head element to the html (has to include doctype and html opening tag)",
    type: "string"
  },
  css: {
    alias: "c",
    describe: "path to custom CSS file to include in the default HTML head",
    type: "string"
  },
  title: {
    alias: "t",
    describe: "Title for HTML document (default: H1 element content)",
    type: "string"
  },
  lang: {
    alias: "l",
    describe: "Language of HTML document",
    type: "string"
  },
  charset: {
    alias: "cs",
    describe: "Charset of HTML document",
    type: "string"
  }
};
export const handler = async function(argv: Arguments) {
  let config = <IFileRequest & IHtmlRequest & ISaveAsRequest & ILogParserErrorsRequest>(
    await argdown.loadConfig(argv.config)
  );
 
  config.html = config.html || {};
 
  Iif (argv.headless) {
    config.html.headless = true;
  }
  Iif (argv.title) {
    config.html.title = argv.title;
  }
 
  Eif (argv.inputGlob) {
    config.inputPath = argv.inputGlob;
  }
  config.saveAs = config.saveAs || {};
  if (argv.outputDir) {
    config.saveAs.outputDir = argv.outputDir;
  }
 
  config.logLevel = argv.verbose ? "verbose" : config.logLevel;
  config.watch = argv.watch || config.watch;
  config.process = ["load-file", "parse-input"];
  config.logParserErrors = argv.logParserErrors || config.logParserErrors;
  Eif (config.logParserErrors) {
    config.process.push("log-parser-errors");
  }
  config.process.push("build-model");
  config.process.push("export-html");
 
  if (!argv.stdout || argv.outputDir) {
    config.process.push("save-as-html");
  }
 
  Iif (argv.css) {
    config.html.css = argv.css;
  } else if (!argv.stdout || argv.outputDir) {
    config.process.push("copy-default-css");
  }
  if (argv.stdout) {
    config.process.push("stdout-html");
  }
 
  await argdown.load(config).catch((e: Error) => console.log(e.message));
};