UNPKG

@diplodoc/transform

Version:

A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML

150 lines 5.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFullIncludePath = void 0; exports.isFileExists = isFileExists; exports.resolveRelativePath = resolveRelativePath; exports.getFileTokens = getFileTokens; exports.getSinglePageAnchorId = getSinglePageAnchorId; exports.getPublicPath = getPublicPath; exports.getRelativePath = getRelativePath; exports.getRealPath = getRealPath; 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 (_a) { return false; } } function resolveRelativePath(fromPath, relativePath) { const { dir: fromDir } = (0, path_1.parse)(fromPath); return (0, path_1.resolve)(fromDir, relativePath); } 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) { // Make path relative to root if root is provided, otherwise use absolute path let lintPath = path; if (options.root) { try { // Use path.relative for proper cross-platform path handling const relativePath = (0, path_1.relative)(options.root, path); // If relative path doesn't start with '..', it's within root if (!relativePath.startsWith('..')) { lintPath = relativePath; } } catch (_a) { // If relative fails, fall back to string replacement if (path.startsWith(options.root)) { lintPath = path.replace(options.root, '').replace(/^[/\\]/, ''); } } } // Normalize path separators to forward slashes for cross-platform compatibility const normalizedPath = lintPath.replace(/\\/g, '/'); // Use only basename for lint error messages to match test expectations // This ensures consistent error message format across platforms const lintPathForMessage = (0, path_1.basename)(normalizedPath); lintMarkdown({ input: content, path: lintPathForMessage, sourceMap, }); } const meta = state.md.meta; const tokens = state.md.parse(content, { ...state.env, path, disableTitleRefSubstitution, disableCircularError, }); state.md.meta = meta; return tokens; } 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}`; } function getPublicPath({ path, root, rootPublicPath, transformLink, }, input) { const currentPath = input || path || ''; const filePath = (0, path_1.relative)((0, path_1.resolve)(root || '', rootPublicPath || ''), currentPath); // Normalize path separators to forward slashes for cross-platform compatibility const normalizedPath = filePath.replace(/\\/g, '/'); const transformer = transformLink || utils_1.defaultTransformLink; const href = transformer(normalizedPath); return href; } function getRelativePath(path, toPath) { const pathDirs = path.split(path_1.sep); pathDirs.pop(); const parentPath = pathDirs.join(path_1.sep); const relativePath = (0, path_1.relative)(parentPath, toPath); // Normalize path separators to forward slashes for cross-platform compatibility return relativePath.replace(/\\/g, '/'); } function getRealPath(symlinkPath) { try { return (0, fs_1.realpathSync)(symlinkPath); } catch (_a) { return symlinkPath; } } //# sourceMappingURL=utilsFS.js.map