Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy assets by root path #719

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/steps/processAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import walkSync from 'walk-sync';
import {load} from 'js-yaml';
import {readFileSync} from 'fs';
import shell from 'shelljs';
import {join, resolve} from 'path';
import {join, resolve, sep} from 'path';

import {ArgvService, TocService} from '../services';
import {checkPathExists, copyFiles, findAllValuesByKeys} from '../utils';
Expand All @@ -18,6 +18,7 @@ import {
YFM_CONFIG_FILENAME,
} from '../constants';
import {Resources} from '../models';
import {resolveRelativePath} from '@diplodoc/transform/lib/utilsFS';

/**
* Processes assets files (everything except .md files)
Expand Down Expand Up @@ -99,17 +100,25 @@ function processAssetsMdRun({args, tmpOutputFolder}) {
}

const contentLinks = findAllValuesByKeys(content, LINK_KEYS);
const localMediaLinks = contentLinks.filter(
(link) =>
new RegExp(/^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm).test(link) &&
isLocalUrl(link),
const localMediaLinks = contentLinks.reduce(
(acc, link) => {
const linkHasMediaExt = new RegExp(
/^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm,
).test(link);

if (linkHasMediaExt && isLocalUrl(link) && checkPathExists(link, yamlFile)) {
const linkAbsolutePath = resolveRelativePath(yamlFile, link);
const linkRootPath = linkAbsolutePath.replace(`${inputFolderPath}${sep}`, '');

acc.push(linkRootPath);
}
return acc;
},

[],
);

copyFiles(
args.input,
tmpOutputFolder,
localMediaLinks.filter((link) => checkPathExists(link, yamlFile)),
);
copyFiles(args.input, tmpOutputFolder, localMediaLinks);
});
}

Expand Down
Loading