UNPKG

@diplodoc/transform

Version:

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

82 lines 3.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const chain_1 = __importDefault(require("lodash/chain")); const path_1 = require("path"); const utilsFS_1 = require("../../utilsFS"); const utils_1 = require("../../utils"); const utils_2 = require("../utils"); const constants_1 = require("./constants"); const slugify = require('slugify'); /* eslint-disable max-len */ /* If the singlePage option is passed in the options: * - replace the default anchor and custom anchors with anchors with file at the beginning * - increase the level of headers, * - add page anchor to the first matched header * Example for file index.md: * Before: # Title {#CustomAnchor} {#custom-anchor} * ## Subtitle * After: ## Title {data-original-article=/index} {#_index} {#_index_title} {#_index_CustomAnchor} {#_index_custom-anchor} * ## Subtitle {#_index_subtitle} * */ const collect = (input, options) => { const { root, path, singlePage } = options; if (!singlePage || path.includes(`_includes${path_1.sep}`)) { return; } const currentPath = (0, utilsFS_1.resolveRelativePath)(root, path); const pageId = (0, utilsFS_1.getSinglePageAnchorId)({ root, currentPath }); let needToSetPageId = true; let needToSetOriginalPathAttr = true; const lines = input.split(utils_2.сarriage); let i = 0; const HEADER_LINK_REGEXP = /^(?<headerLevel>#+)\s(?<headerContent>.+)$/i; while (i < lines.length) { const line = lines[i]; const headerMatch = line.match(HEADER_LINK_REGEXP); if (!headerMatch) { i++; continue; } const { headerLevel, headerContent } = headerMatch.groups; let newHeaderContent = headerContent; const newCustomHeaders = []; while (constants_1.CUSTOM_ID_REGEXP.test(newHeaderContent)) { newHeaderContent = newHeaderContent.replace(constants_1.CUSTOM_ID_REGEXP, (match, customId) => { if (match === constants_1.CUSTOM_ID_EXCEPTION) { return match; } newCustomHeaders.push(`${pageId}_${customId}`); return ''; }); } newHeaderContent = newHeaderContent.trim(); const slugifiedTitle = slugify(newHeaderContent, { lower: true }); newCustomHeaders.push(`${pageId}_${slugifiedTitle}`); if (needToSetPageId) { newCustomHeaders.push(pageId); needToSetPageId = false; } const newCustomHeadersStr = (0, chain_1.default)(newCustomHeaders) .uniq() .map((id) => { return `{${id}}`; }) .value() .join(' '); const baseHeaderSyntax = `${headerLevel}# ${newHeaderContent}`; lines[i] = `${baseHeaderSyntax} ${newCustomHeadersStr}`; if (needToSetOriginalPathAttr) { const originalArticleHref = (0, utils_1.transformLinkToOriginalArticle)({ root, currentPath }); lines[i] = `${baseHeaderSyntax} {data-original-article=${originalArticleHref}} ${newCustomHeadersStr}`; needToSetOriginalPathAttr = false; } i++; } // eslint-disable-next-line consistent-return return lines.join(utils_2.сarriage); }; module.exports = collect; //# sourceMappingURL=collect.js.map