Skip to content

Commit

Permalink
feat: generate html links for PC pages in md2html
Browse files Browse the repository at this point in the history
  • Loading branch information
martyanovandrey committed Apr 9, 2024
1 parent 4d0df1c commit 7181844
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 20 additions & 1 deletion src/utils/markup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -188,6 +188,25 @@ export function findAllValuesByKeys(obj, keysToFind) {
});
}

export function modifyValuesByKeys(originalObj, keysToFind, modifyFn) {
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 modifyFn(value);
}
}

// Clone the object deeply with a customizer function that modifies matching keys
return cloneDeepWith(originalObj, customizer);
}

export function getLinksWithContentExtersion(link) {
return new RegExp(/^\S.*\.(md|ya?ml|html)$/gm).test(link);
}

export function getLinksWithExtension(link) {
const oneLineWithExtension = new RegExp(
/^\S.*\.(md|html|yaml|svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm,
Expand Down

0 comments on commit 7181844

Please sign in to comment.