From 91b2a374bca216947b6dd28a96dd4d33f4452c00 Mon Sep 17 00:00:00 2001 From: Martyanov Andrey <37772440+martyanovandrey@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:37:12 +0300 Subject: [PATCH] feat: generate html links for PC pages in md2html (#734) * feat: generate html links for PC pages in md2html * value only string * lint * cleanup --- src/resolvers/md2html.ts | 22 +++++++++++++++++----- src/utils/markup.ts | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/resolvers/md2html.ts b/src/resolvers/md2html.ts index 443dbae8..3ee9d693 100644 --- a/src/resolvers/md2html.ts +++ b/src/resolvers/md2html.ts @@ -10,14 +10,18 @@ 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'; +import {isString} from 'lodash'; export interface FileTransformOptions { path: string; @@ -115,12 +119,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 (isString(link) && 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..9943f257 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, 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,25 @@ export function findAllValuesByKeys(obj, keysToFind: string[]) { }); } +export function modifyValuesByKeys( + originalObj, + keysToFind: string[], + modifyFn: (value: string) => string, +) { + function customizer(value, key) { + if (keysToFind?.includes(key) && isString(value)) { + return 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,