Skip to content

Commit

Permalink
feat: generate html links for PC pages in md2html (#734)
Browse files Browse the repository at this point in the history
* feat: generate html links for PC pages in md2html

* value only string

* lint

* cleanup
  • Loading branch information
martyanovandrey authored Apr 10, 2024
1 parent 31f53ac commit 91b2a37
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
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: 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,
Expand Down

0 comments on commit 91b2a37

Please sign in to comment.