Config files
- added support for `mdconf.json`config file - added markdown-it plugins - added mathjax evaluation script - deactivated math stylingdevelop
parent
62d75c3a27
commit
21fd8356c3
@ -0,0 +1,52 @@
|
||||
import * as fsx from 'fs-extra';
|
||||
import * as path from 'path';
|
||||
import {getMarkdownPlugin} from "./utils";
|
||||
|
||||
const confName = 'mdconfig.json';
|
||||
|
||||
/**
|
||||
* Markdown config class for easyer access on the configuration.
|
||||
*/
|
||||
export class MarkdownConfig {
|
||||
public plugins: Set<any> = new Set<any>();
|
||||
public stylesheets: Set<string> = new Set<string>();
|
||||
public format: string = 'A4';
|
||||
public bundle: boolean = false;
|
||||
private readonly filename: string;
|
||||
|
||||
/**
|
||||
* Creates a new config with the given directory or the processes work directory.
|
||||
* @param dirname
|
||||
*/
|
||||
constructor(dirname?: string) {
|
||||
dirname = dirname || process.cwd();
|
||||
let confFile: string = path.join(dirname, confName);
|
||||
|
||||
if (fsx.pathExistsSync(confFile)) {
|
||||
this.filename = confFile;
|
||||
this.loadData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the data from a config file.
|
||||
*/
|
||||
private loadData() {
|
||||
let configData = fsx.readJsonSync(this.filename);
|
||||
this.format = configData.format || this.format;
|
||||
this.bundle = configData.bundle;
|
||||
if (configData.plugins)
|
||||
this.plugins = new Set<string>(configData.plugins.map(getMarkdownPlugin));
|
||||
if (configData.stylesheets)
|
||||
this.stylesheets = new Set<string>(configData.stylesheets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the config file to a config file.
|
||||
* @param filename
|
||||
*/
|
||||
public save(filename?: string) {
|
||||
let confFile = filename || this.filename;
|
||||
fsx.writeJsonSync(confFile, this);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import * as winston from "winston";
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
format: winston.format.json(),
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.simple()
|
||||
),
|
||||
level: process.env.NODE_ENV === 'production'? 'info' : 'silly'
|
||||
})
|
||||
]
|
||||
});
|
@ -0,0 +1,83 @@
|
||||
import {JSDOM} from "jsdom";
|
||||
import * as path from "path";
|
||||
import * as fsx from "fs-extra";
|
||||
import fetch from "node-fetch";
|
||||
import {logger} from "./logger";
|
||||
import {markdownPlugins} from "./plugins";
|
||||
|
||||
const mathJaxUrl = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML';
|
||||
|
||||
/**
|
||||
* Bundles all images in the image tags.
|
||||
* @param dom
|
||||
* @param mainfile
|
||||
*/
|
||||
export async function bundleImages(dom: JSDOM, mainfile: string): Promise<JSDOM> {
|
||||
let document = dom.window.document;
|
||||
let mainFolder = path.dirname(mainfile);
|
||||
let imgs = document.querySelectorAll('img');
|
||||
logger.debug(`Bundling ${imgs.length} images.`);
|
||||
for (let img of imgs) {
|
||||
let source = img.src;
|
||||
let filepath = source;
|
||||
let base64Url = source;
|
||||
if (!path.isAbsolute(filepath))
|
||||
filepath = path.join(mainFolder, filepath);
|
||||
if (await fsx.pathExists(filepath)) {
|
||||
let type = path.extname(source).slice(1);
|
||||
base64Url = `data:image/${type};base64,`;
|
||||
base64Url += (await fsx.readFile(filepath)).toString('base64');
|
||||
} else {
|
||||
try {
|
||||
let response = await fetch(source);
|
||||
base64Url = `data:${response.headers.get('content-type')};base64,`;
|
||||
base64Url += (await response.buffer()).toString('base64');
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
}
|
||||
}
|
||||
img.src = base64Url;
|
||||
}
|
||||
return dom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Includes mathjax in the dom.
|
||||
* @param dom
|
||||
*/
|
||||
export function includeMathJax(dom: JSDOM): JSDOM {
|
||||
let document = dom.window.document;
|
||||
let scriptTag = document.createElement('script');
|
||||
scriptTag.src = mathJaxUrl;
|
||||
document.head.appendChild(scriptTag);
|
||||
return dom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the markdown plugin associated with the pluginName
|
||||
* @param pluginName
|
||||
*/
|
||||
export function getMarkdownPlugin(pluginName: string) {
|
||||
logger.debug(`Trying to resolve plugin ${pluginName}`);
|
||||
if (markdownPlugins[pluginName]) {
|
||||
return require(markdownPlugins[pluginName]);
|
||||
} else {
|
||||
try {
|
||||
let plugin = require(pluginName);
|
||||
if (plugin)
|
||||
return plugin;
|
||||
} catch (err) {
|
||||
console.error(`Module ${pluginName} not found.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronous elay function
|
||||
* @param milliseconds
|
||||
*/
|
||||
export function delay(milliseconds: number) {
|
||||
return new Promise(function(resolve) {
|
||||
setTimeout(resolve, milliseconds)
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue