Skip to content

Commit

Permalink
fix: upgrade getAssetsPublicPath
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm authored and 3y3 committed Apr 23, 2024
1 parent d7fbe36 commit 9200f11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const FileTransformer: Record<string, Function> = {
'.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();

Expand All @@ -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 = [];
Expand Down
15 changes: 9 additions & 6 deletions src/services/metadata.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/__snapshots__/load-custom-resources.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,18 @@ exports[`Allow load custom resources md2html single page with custom resources 4
>
<link rel="stylesheet"
type="text/css"
href="../_assets/style/test.css"
href="_assets/style/test.css"
id="custom-style"
>
<script src="../_assets/script/test1.js">
<script src="_assets/script/test1.js">
</script>
</head>
<body class="g-root g-root_theme_light">
<div id="root">
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"leading":false,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":"."},"meta":{"metadata":[],"style":["../_assets/style/test.css"],"script":["../_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
window.__DATA__ = {"data":{"leading":false,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":"."},"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n"},"router":{"pathname":"project/config.html"},"lang":"ru","langs":["ru"]};
</script>
Expand Down Expand Up @@ -426,18 +426,18 @@ exports[`Allow load custom resources md2html with custom resources 4`] = `
>
<link rel="stylesheet"
type="text/css"
href="../_assets/style/test.css"
href="_assets/style/test.css"
id="custom-style"
>
<script src="../_assets/script/test1.js">
<script src="_assets/script/test1.js">
</script>
</head>
<body class="g-root g-root_theme_light">
<div id="root">
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"leading":false,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":"."},"meta":{"metadata":[],"style":["../_assets/style/test.css"],"script":["../_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
window.__DATA__ = {"data":{"leading":false,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"},{"name":"Config","href":"project/config.html","id":"Config-RANDOM"}],"base":"."},"meta":{"metadata":[],"style":["_assets/style/test.css"],"script":["_assets/script/test1.js"]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>Lorem
</p>/n"},"router":{"pathname":"project/config.html"},"lang":"ru","langs":["ru"]};
</script>
Expand Down

0 comments on commit 9200f11

Please sign in to comment.