From a6131b57eb5e88aab4a4254cf897dc0c1c54d5eb Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Thu, 26 Oct 2023 14:41:26 +0200 Subject: [PATCH] feat(#16): introduce config --- src/parser/config.ts | 26 ++++++++++++++++++++++++++ src/parser/parser.ts | 10 ++-------- 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/parser/config.ts diff --git a/src/parser/config.ts b/src/parser/config.ts new file mode 100644 index 00000000..07153923 --- /dev/null +++ b/src/parser/config.ts @@ -0,0 +1,26 @@ +import fs from 'fs'; +import { homedir } from 'os'; +import path from 'path'; + +type Config = { + styles?: string; + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + katexOptions?: any; + pageTitle?: string; +}; +let config: Config = {}; + +const configPaths = [ + path.join(homedir(), '.vivify', 'config.json'), + path.join(homedir(), '.vivify.json'), +]; + +for (const cp of configPaths) { + if (!fs.existsSync(cp)) continue; + try { + config = JSON.parse(fs.readFileSync(cp, 'utf8')); + break; + } catch {} +} + +export default config; diff --git a/src/parser/parser.ts b/src/parser/parser.ts index 7f01d9ca..adfbefa1 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -1,16 +1,10 @@ import { homedir } from 'os'; -import fs from 'fs'; import MarkdownIt from 'markdown-it'; import anchor from 'markdown-it-anchor'; import highlight from './highlight'; import graphviz from './dot'; - -const katexConfigPath = `${homedir()}/.vivify/katex_config.json`; -let katexConfig = {}; -if (fs.existsSync(katexConfigPath)) { - katexConfig = JSON.parse(fs.readFileSync(katexConfigPath, 'utf8')); -} +import config from './config'; const mdit = new MarkdownIt({ html: true, @@ -29,7 +23,7 @@ mdit.use(require('markdown-it-inject-linenumbers')); mdit.use(require('markdown-it-texmath'), { engine: require('katex'), delimiters: 'dollars', - katexOptions: katexConfig, + katexOptions: config.katexOptions, }); /* eslint-enable @typescript-eslint/no-var-requires */ mdit.use(graphviz);