From 0a0a5b5fb205b5c6510b01726d89c18b17389f10 Mon Sep 17 00:00:00 2001 From: martyanov-av Date: Tue, 9 Apr 2024 20:08:51 +0300 Subject: [PATCH] feat: generate html links for PC pages in md2html --- src/resolvers/md2html.ts | 21 ++++++++++++++++----- src/utils/markup.ts | 25 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/resolvers/md2html.ts b/src/resolvers/md2html.ts index 443dbae8..420c3690 100644 --- a/src/resolvers/md2html.ts +++ b/src/resolvers/md2html.ts @@ -10,14 +10,17 @@ import {LeadingPage, ResolveMd2HTMLResult, ResolverOptions, YfmToc} from '../mod import {ArgvService, PluginService, TocService} from '../services'; import { generateStaticMarkup, + getLinksWithContentExtersion, getVarsPerFile, getVarsPerRelativeFile, logger, + modifyValuesByKeys, transformToc, } from '../utils'; import {Lang, PROCESSING_FINISHED} from '../constants'; import {getAssetsPublicPath, getVCSMetadata} from '../services/metadata'; import {MarkdownItPluginCb} from '@diplodoc/transform/lib/plugins/typings'; +import {LINK_KEYS} from '@diplodoc/client/ssr'; export interface FileTransformOptions { path: string; @@ -115,12 +118,20 @@ function YamlFileTransformer(content: string): Object { }; } - const links = data?.links?.map((link) => - link.href ? {...link, href: link.href.replace(/.md$/gmu, '.html')} : link, - ); + if (Object.prototype.hasOwnProperty.call(data, 'blocks')) { + data = modifyValuesByKeys(data, LINK_KEYS, (link) => { + if (getLinksWithContentExtersion(link)) { + return link.replace(/.(md|yaml)$/gmu, '.html'); + } + }); + } else { + const links = data?.links?.map((link) => + link.href ? {...link, href: link.href.replace(/.md$/gmu, '.html')} : link, + ); - if (links) { - data.links = links; + if (links) { + data.links = links; + } } return { diff --git a/src/utils/markup.ts b/src/utils/markup.ts index 80d31607..b5087447 100644 --- a/src/utils/markup.ts +++ b/src/utils/markup.ts @@ -1,6 +1,6 @@ import {join} from 'path'; import {platform} from 'process'; -import {flatMapDeep, isArray, isObject, isString} from 'lodash'; +import {cloneDeepWith, every, flatMapDeep, isArray, isObject, isString} from 'lodash'; import {CUSTOM_STYLE, Platforms, RTL_LANGS} from '../constants'; import {LeadingPage, Resources, SinglePageResult, TextItems, VarsMetadata} from '../models'; @@ -188,6 +188,29 @@ export function findAllValuesByKeys(obj, keysToFind: string[]) { }); } +export function modifyValuesByKeys( + originalObj, + keysToFind: string[], + modifyFn: () => string | string[], +) { + function customizer(value, key) { + // Apply the modification function if the key matches and it's a string or an array of strings + if ( + keysToFind?.includes(key) && + (isString(value) || (isArray(value) && every(value, isString))) + ) { + return isArray(value) ? value.map(modifyFn) : modifyFn(value); + } + } + + // Clone the object deeply with a customizer function that modifies matching keys + return cloneDeepWith(originalObj, customizer); +} + +export function getLinksWithContentExtersion(link: string) { + return new RegExp(/^\S.*\.(md|ya?ml|html)$/gm).test(link); +} + export function getLinksWithExtension(link: string) { const oneLineWithExtension = new RegExp( /^\S.*\.(md|html|yaml|svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm,