From dd0cdf27cd1dcb66b629882bb8098c28560be6d1 Mon Sep 17 00:00:00 2001 From: Maxim Karpov Date: Wed, 27 Mar 2024 18:14:44 +0300 Subject: [PATCH] feat: implement base path --- src/models.ts | 1 + src/resolvers/md2html.ts | 6 +++--- src/steps/processPages.ts | 3 +++ src/utils/markup.ts | 5 ++++- src/utils/toc.ts | 14 +++----------- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/models.ts b/src/models.ts index 4dcc1117..f10b173e 100644 --- a/src/models.ts +++ b/src/models.ts @@ -238,6 +238,7 @@ export interface ResolveMd2MdOptions { } export interface ResolverOptions { + deep: number; inputPath: string; filename: string; fileExtension: string; diff --git a/src/resolvers/md2html.ts b/src/resolvers/md2html.ts index 230283ea..30e5cb02 100644 --- a/src/resolvers/md2html.ts +++ b/src/resolvers/md2html.ts @@ -103,7 +103,7 @@ const getFileProps = async (options: ResolverOptions) => { const props = { data: { leading: inputPath.endsWith('.yaml'), - toc: transformToc(toc, pathToDir) || {}, + toc: transformToc(toc) || {}, ...meta, }, router: { @@ -117,13 +117,13 @@ const getFileProps = async (options: ResolverOptions) => { } export async function resolveMd2HTML(options: ResolverOptions): Promise { - const { outputPath, outputBundlePath, inputPath } = options; + const { outputPath, outputBundlePath, inputPath, deep } = options; const props = await getFileProps(options); const outputDir = dirname(outputPath); const relativePathToBundle: string = relative(resolve(outputDir), resolve(outputBundlePath)); - const outputFileContent = generateStaticMarkup(props, relativePathToBundle); + const outputFileContent = generateStaticMarkup(props, relativePathToBundle, deep); writeFileSync(outputPath, outputFileContent); logger.info(inputPath, PROCESSING_FINISHED); diff --git a/src/steps/processPages.ts b/src/steps/processPages.ts index 6a6d9e6e..2b365be0 100644 --- a/src/steps/processPages.ts +++ b/src/steps/processPages.ts @@ -361,6 +361,8 @@ async function processingFileToHtml( ): Promise { const {outputBundlePath, filename, fileExtension, outputPath, pathToFile} = path; + const deep = pathToFile.split('/').length - 2; // exclude lang and count slashes + return resolveMd2HTML({ inputPath: pathToFile, outputBundlePath, @@ -368,5 +370,6 @@ async function processingFileToHtml( outputPath, filename, metadata: metaDataOptions, + deep, }); } diff --git a/src/utils/markup.ts b/src/utils/markup.ts index 9943f257..ae124c43 100644 --- a/src/utils/markup.ts +++ b/src/utils/markup.ts @@ -28,6 +28,7 @@ export type Meta = TitleMeta & export function generateStaticMarkup( props: DocInnerProps, pathToBundle: string, + deep: number = 0, ): string { const {style, script, metadata, ...restYamlConfigMeta} = (props.data.meta as Meta) || {}; const {title: tocTitle} = props.data.toc; @@ -45,12 +46,14 @@ export function generateStaticMarkup( const html = staticContent ? render(props) : ''; const isRTL = RTL_LANGS.includes(props.lang); + const deepPath = deep ? Array(deep).fill('../').join('') : './'; return ` + ${getMetadata(metadata, restYamlConfigMeta)} ${title} @@ -60,7 +63,7 @@ export function generateStaticMarkup( } ${manifest.css - .filter((file) => isRTL === file.includes('.rtl.css')) + .filter((file: string) => isRTL === file.includes('.rtl.css')) .map(dst(pathToBundle)) .map((src: string) => ``) .join('\n')} diff --git a/src/utils/toc.ts b/src/utils/toc.ts index 65ef1a0f..27f572fe 100644 --- a/src/utils/toc.ts +++ b/src/utils/toc.ts @@ -1,11 +1,11 @@ -import {basename, dirname, extname, format, join, relative} from 'path'; +import {basename, dirname, extname, join} from 'path'; import {YfmToc} from '../models'; import {filterFiles} from '../services/utils'; import {isExternalHref} from './url'; import {getSinglePageAnchorId} from './singlePage'; -export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): YfmToc | null { +export function transformToc(toc: YfmToc | null): YfmToc | null { if (!toc) { return null; } @@ -23,7 +23,6 @@ export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): Y ); } - const baseTocPath: string = localToc.base || ''; const navigationItemQueue = [localToc]; while (navigationItemQueue.length) { @@ -40,17 +39,10 @@ export function transformToc(toc: YfmToc | null, pathToFileDirectory: string): Y } if (href && !isExternalHref(href)) { - /* Path to directory with toc.yaml */ - const pathToIndexDirectory: string = relative(pathToFileDirectory, baseTocPath); - const fileExtension: string = extname(href); const filename: string = basename(href, fileExtension); - const transformedFilename: string = format({ - name: filename, - ext: '.html', - }); - navigationItem.href = join(pathToIndexDirectory, dirname(href), transformedFilename); + navigationItem.href = join(dirname(href), filename); } }