@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
120 lines • 4.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRealPath = exports.getRelativePath = exports.getPublicPath = exports.getSinglePageAnchorId = exports.getFullIncludePath = exports.getFileTokens = exports.resolveRelativePath = exports.isFileExists = void 0;
const fs_1 = require("fs");
const escapeRegExp_1 = __importDefault(require("lodash/escapeRegExp"));
const path_1 = require("path");
const quick_lru_1 = __importDefault(require("quick-lru"));
const liquid_1 = __importDefault(require("./liquid"));
const utils_1 = require("./utils");
const preprocessors_1 = require("./preprocessors");
const filesCache = new quick_lru_1.default({ maxSize: 1000 });
function isFileExists(file) {
try {
const stats = (0, fs_1.statSync)(file);
return stats.isFile();
}
catch (e) {
return false;
}
}
exports.isFileExists = isFileExists;
function resolveRelativePath(fromPath, relativePath) {
const { dir: fromDir } = (0, path_1.parse)(fromPath);
return (0, path_1.resolve)(fromDir, relativePath);
}
exports.resolveRelativePath = resolveRelativePath;
function getFileTokens(path, state, options, content) {
const { getVarsPerFile, vars, disableLiquid, disableLint, lintMarkdown, disableTitleRefSubstitution, disableCircularError, inheritVars = true, conditionsInCode, } = options;
const builtVars = (getVarsPerFile && !inheritVars ? getVarsPerFile(path) : vars) || {};
// Read the content only if we dont have one in the args
if (!content) {
if (filesCache.has(path)) {
content = filesCache.get(path);
}
else {
content = (0, fs_1.readFileSync)(path, 'utf8');
filesCache.set(path, content);
}
}
let sourceMap;
if (!disableLiquid) {
const liquidResult = (0, liquid_1.default)(content, builtVars, path, {
withSourceMap: true,
conditionsInCode,
});
content = liquidResult.output;
sourceMap = liquidResult.sourceMap;
}
// Run preprocessor
content = (0, preprocessors_1.preprocess)(content, options);
if (!disableLint && lintMarkdown) {
lintMarkdown({
input: content,
path,
sourceMap,
});
}
const meta = state.md.meta;
const tokens = state.md.parse(content, Object.assign(Object.assign({}, state.env), { path,
disableTitleRefSubstitution,
disableCircularError }));
state.md.meta = meta;
return tokens;
}
exports.getFileTokens = getFileTokens;
const getFullIncludePath = (includePath, root, path) => {
let fullIncludePath;
if (includePath.startsWith(path_1.sep)) {
fullIncludePath = (0, path_1.join)(root, includePath);
}
else {
fullIncludePath = resolveRelativePath(path, includePath);
}
return fullIncludePath;
};
exports.getFullIncludePath = getFullIncludePath;
function getSinglePageAnchorId(args) {
const { root, currentPath, pathname, hash } = args;
let resultAnchor = currentPath;
if (pathname) {
resultAnchor = resolveRelativePath(currentPath, pathname);
}
resultAnchor = resultAnchor
.replace(root, '')
.replace(/\.(md|ya?ml|html)$/i, '')
.replace(new RegExp((0, escapeRegExp_1.default)(path_1.sep), 'gi'), '_');
if (hash) {
resultAnchor = resultAnchor + '_' + hash.slice(1);
}
return `#${resultAnchor}`;
}
exports.getSinglePageAnchorId = getSinglePageAnchorId;
function getPublicPath({ path, root, rootPublicPath, transformLink, }, input) {
const currentPath = input || path || '';
const filePath = (0, path_1.relative)((0, path_1.resolve)(root || '', rootPublicPath || ''), currentPath);
const transformer = transformLink || utils_1.defaultTransformLink;
const href = transformer(filePath);
return href;
}
exports.getPublicPath = getPublicPath;
function getRelativePath(path, toPath) {
const pathDirs = path.split(path_1.sep);
pathDirs.pop();
const parentPath = pathDirs.join(path_1.sep);
return (0, path_1.relative)(parentPath, toPath);
}
exports.getRelativePath = getRelativePath;
function getRealPath(symlinkPath) {
try {
return (0, fs_1.realpathSync)(symlinkPath);
}
catch (err) {
return symlinkPath;
}
}
exports.getRealPath = getRealPath;
//# sourceMappingURL=utilsFS.js.map