diff --git a/src/resolvers/md2html.ts b/src/resolvers/md2html.ts index 9c1282a5..86f455f6 100644 --- a/src/resolvers/md2html.ts +++ b/src/resolvers/md2html.ts @@ -34,6 +34,10 @@ const FileTransformer: Record = { '.md': MdFileTransformer, }; +const fixRelativePath = (relativeTo: string) => (path: string) => { + return join(getAssetsPublicPath(relativeTo), path); +}; + const getFileMeta = async ({fileExtension, metadata, inputPath}: ResolverOptions) => { const {input, allowCustomResources} = ArgvService.getConfig(); @@ -57,9 +61,10 @@ const getFileMeta = async ({fileExtension, metadata, inputPath}: ResolverOptions if (allowCustomResources) { const {script, style} = metadata?.resources || {}; - fileMeta.style = (fileMeta.style || []).concat(style || []); + fileMeta.style = (fileMeta.style || []).concat(style || []).map(fixRelativePath(inputPath)); fileMeta.script = (fileMeta.script || []) - .concat(script || []); + .concat(script || []) + .map(fixRelativePath(inputPath)); } else { fileMeta.style = []; fileMeta.script = []; diff --git a/src/services/metadata.ts b/src/services/metadata.ts index 535ebc26..529bd379 100644 --- a/src/services/metadata.ts +++ b/src/services/metadata.ts @@ -1,7 +1,7 @@ import {dump, load} from 'js-yaml'; import {VCSConnector} from '../vcs-connector/connector-models'; -import {MetaDataOptions, Metadata, Resources, VarsMetadata} from '../models'; +import {MetaDataOptions, Metadata, Resources, VarsMetadata, YfmToc} from '../models'; import { getAuthorDetails, updateAuthorMetadataStringByAuthorLogin, @@ -15,8 +15,8 @@ import { import {isObject} from './utils'; import {сarriage} from '../utils'; import {REGEXP_AUTHOR, metadataBorder} from '../constants'; -import {dirname, relative, resolve} from 'path'; -import {ArgvService, TocService} from './index'; +import {sep} from 'path'; +import {TocService} from './index'; async function getContentWithUpdatedMetadata( fileContent: string, @@ -327,11 +327,14 @@ function getSystemVarsMetadataString(systemVars: object) { } function getAssetsPublicPath(filePath: string) { - const {input} = ArgvService.getConfig(); - const path: string = resolve(input, filePath); + const toc: YfmToc | null = TocService.getForPath(filePath) || null; + + const basePath = toc?.base?.split(sep)?.filter((str) => str !== '.') || []; + const deepBase = basePath.length; + const deepBasePath = deepBase > 0 ? Array(deepBase).fill('../').join('') : './'; /* Relative path from folder of .md file to root of user' output folder */ - return relative(dirname(path), resolve(input)); + return deepBasePath; } export { diff --git a/tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap b/tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap index bac5f018..20c73a88 100644 --- a/tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap +++ b/tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap @@ -164,10 +164,10 @@ exports[`Allow load custom resources md2html single page with custom resources 4 > - @@ -175,7 +175,7 @@ exports[`Allow load custom resources md2html single page with custom resources 4 @@ -426,10 +426,10 @@ exports[`Allow load custom resources md2html with custom resources 4`] = ` > - @@ -437,7 +437,7 @@ exports[`Allow load custom resources md2html with custom resources 4`] = `