@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
78 lines • 3.21 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const markdownlint_1 = require("markdownlint");
const merge_1 = __importDefault(require("lodash/merge"));
const union_1 = __importDefault(require("lodash/union"));
const markdown_it_attrs_1 = __importDefault(require("markdown-it-attrs"));
const preprocessors_1 = require("../preprocessors");
const yfmlint_1 = __importDefault(require("./yfmlint"));
const markdownlint_custom_rule_1 = require("./markdownlint-custom-rule");
const utils_1 = require("./utils");
const yfm009_1 = require("./markdownlint-custom-rule/yfm009");
const defaultLintRules = [markdownlint_custom_rule_1.yfm001, markdownlint_custom_rule_1.yfm002, markdownlint_custom_rule_1.yfm003, markdownlint_custom_rule_1.yfm004, markdownlint_custom_rule_1.yfm005, markdownlint_custom_rule_1.yfm006, markdownlint_custom_rule_1.yfm007, markdownlint_custom_rule_1.yfm008, yfm009_1.yfm009];
const lintCache = new Set();
function yfmlint(opts) {
let { input } = opts;
const { plugins: customPlugins, pluginOptions, customLintRules, sourceMap } = opts;
const { path = 'input', log } = pluginOptions;
pluginOptions.isLintRun = true;
const { LogLevels: { ERROR, WARN, DISABLED }, } = log;
if (lintCache.has(path)) {
return;
}
lintCache.add(path);
const defaultLintConfig = opts.defaultLintConfig || yfmlint_1.default;
let lintConfig = defaultLintConfig;
if (opts.lintConfig) {
lintConfig = (0, merge_1.default)({}, defaultLintConfig, opts.lintConfig);
}
let lintRules = defaultLintRules;
if (customLintRules) {
lintRules = (0, union_1.default)(lintRules, customLintRules);
}
// TODO: set to false in next major
const { enableMarkdownAttrs = true } = opts;
const plugins = customPlugins && [...(enableMarkdownAttrs ? [markdown_it_attrs_1.default] : []), ...customPlugins];
const preparedPlugins = plugins && plugins.map((plugin) => [plugin, pluginOptions]);
// Run preprocessor
input = (0, preprocessors_1.preprocess)(input, pluginOptions, opts);
let result;
try {
result = (0, markdownlint_1.sync)({
strings: { [path]: input },
markdownItPlugins: preparedPlugins,
handleRuleFailures: true,
config: lintConfig,
customRules: lintRules,
});
}
catch (_a) { }
const errors = result && result[path];
if (!errors) {
return;
}
const logLevelsConfig = lintConfig['log-levels'];
for (const error of errors) {
const message = (0, utils_1.errorToString)(path, error, sourceMap);
const logLevel = (0, utils_1.getLogLevel)({
logLevelsConfig,
ruleNames: error.ruleNames,
defaultLevel: WARN,
});
switch (logLevel) {
case ERROR:
log.error(message);
break;
case WARN:
log.warn(message);
break;
case DISABLED:
default:
break;
}
}
}
module.exports = yfmlint;
//# sourceMappingURL=index.js.map