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,