All files / src mume.ts

44.44% Statements 28/63
9.09% Branches 3/33
18.18% Functions 2/11
43.55% Lines 27/62

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111      1x 1x 1x 1x   1x   1x 1x   1x 1x   2x     1x         1x 1x       1x     1x   1x       1x 1x 1x 1x 1x 1x   1x                                                                                                 1x 1x               1x       1x      
/**
 * The core of mume package.
 */
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as mkdirp from "mkdirp";
 
import * as utility_ from "./utility";
 
let INITIALIZED = false;
let CONFIG_CHANGE_CALLBACK: () => void = null;
 
export const utility = utility_;
export const configs = utility.configs;
export { MarkdownEngineConfig } from "./markdown-engine-config";
export { MarkdownEngine } from "./markdown-engine";
export { CodeChunkData } from "./code-chunk-data";
 
let extensionConfigPath = path.resolve(os.homedir(), "./.mume");
 
/**
 * init mume config folder at ~/.mume
 */
export async function init(configPath: string | null = null): Promise<void> {
  Iif (INITIALIZED) {
    return;
  }
 
  configPath = configPath
    ? path.resolve(configPath)
    : path.resolve(os.homedir(), "./.mume");
  extensionConfigPath = configPath;
 
  Iif (!fs.existsSync(configPath)) {
    mkdirp.sync(configPath);
  }
 
  configs.globalStyle = await utility.getGlobalStyles(configPath);
  configs.mermaidConfig = await utility.getMermaidConfig(configPath);
  configs.mathjaxConfig = await utility.getMathJaxConfig(configPath);
  configs.katexConfig = await utility.getKaTeXConfig(configPath);
  configs.parserConfig = await utility.getParserConfig(configPath);
  configs.config = await utility.getExtensionConfig(configPath);
 
  fs.watch(configPath, (eventType, fileName) => {
    if (eventType === "change") {
      if (fileName === "style.less") {
        // || fileName==='mermaid_config.js' || fileName==='mathjax_config')
        utility.getGlobalStyles(configPath).then((css) => {
          configs.globalStyle = css;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      } else if (fileName === "mermaid_config.js") {
        utility.getMermaidConfig(configPath).then((mermaidConfig) => {
          configs.mermaidConfig = mermaidConfig;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      } else if (fileName === "mathjax_config.js") {
        utility.getMathJaxConfig(configPath).then((mathjaxConfig) => {
          configs.mathjaxConfig = mathjaxConfig;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      } else if (fileName === "katex_config.js") {
        utility.getKaTeXConfig(configPath).then((katexConfig) => {
          configs.katexConfig = katexConfig;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      } else if (fileName === "parser.js") {
        utility.getParserConfig(configPath).then((parserConfig) => {
          configs.parserConfig = parserConfig;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      } else if (fileName === "config.json") {
        utility.getExtensionConfig(configPath).then((config) => {
          configs.config = config;
          if (CONFIG_CHANGE_CALLBACK) {
            CONFIG_CHANGE_CALLBACK();
          }
        });
      }
    }
  });
 
  INITIALIZED = true;
  return;
}
 
/**
 * cb will be called when global style.less like files is changed.
 * @param cb function(error, css){}
 */
 
export function onDidChangeConfigFile(cb: () => void) {
  CONFIG_CHANGE_CALLBACK = cb;
}
 
export function getExtensionConfigPath() {
  return extensionConfigPath;
}